Reputation: 254
I am currently working on elastic search through my java Application . I know how to index the Java pojo using RestHighLevelClient. How i can make search only on new fields not the complete pojo.?
public class Employee{
private long id;
private String name;
private String designation;
private String address; //want to index but not searchable in elastic search
}
My Code for indexing is below which is working fine:
public String saveToEs(Employee employee) throws IOException {
Map<String, Object> map = objectMapper.convertValue(employee, Map.class);
IndexRequest indexRequest =
new IndexRequest(INDEX, TYPE, employee.getId().toString()).source(map, XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
I need to do this in java .Any help please or good link ?
Upvotes: 1
Views: 848
Reputation: 32386
Writing another answer for RestHighLevelClient
As another answer is useful for people not using the Rest client and adding this in the first answer makes it too long.
Note: you are passing the type
which is deprecated in ES 7.X and I am using the ES 7.X version, so my code is according to 7.X.
CreateIndexRequest request = new CreateIndexRequest("employee");
Map<String, Object> name = new HashMap<>();
name.put("type", "text");
Map<String, Object> address = new HashMap<>();
address.put("type", "text");
address.put("index", false);
Map<String, Object> properties = new HashMap<>();
properties.put("name", name);
properties.put("address", address);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
address
field which is not searchable, and to do that I used, address.put("index", false);
, while name
is searchable field and there this option isn't present.index mapping
using the Map method which is available in this official ES doc.index: false
is added in the address field.{ "employee": { "mappings": { "properties": { "address": { "type": "text", "index": false }, "name": { "type": "text" } } } } }
Upvotes: 1
Reputation: 32386
Use the index option as false on the address field, which is by default true to make it unsearchable. As mention in the same official ES link:
The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.
Let me show you how can you test it using the REST API and then the java code(using rest-high level client) to accomplish it.
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text"
},
"designation": {
"type": "text"
},
"address": {
"type": "text",
"index" : false --> made `index` to false
}
}
}
}
{
"address" : "USA",
"name" : "Noshaf",
"id" : 234567892,
"designation" : "software engineer"
}
{
"address" : "USA california state",
"name" : "opster",
"id" : 234567890,
"designation" : "software engineer"
}
address
field{
"query": {
"match" : {
"address" : "USA"
}
}
}
"caused_by": { "type": "illegal_argument_exception", "reason": "Cannot search on field [address] since it is not indexed." }
Upvotes: 1