Noshaf
Noshaf

Reputation: 254

ElasticSearch Make Field non-searchable from java

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

Answers (2)

Amit
Amit

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);

Important points

  • I've used only 2 fields for illustration purpose, one of which is 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.
  • I've created index mapping using the Map method which is available in this official ES doc.
  • you can check the mapping created by this code, using mapping REST API.
  • Below is the mapping generated for this code in my system and you can see, index: false is added in the address field.
{
  "employee": {
    "mappings": {
      "properties": {
        "address": {
          "type": "text",
          "index": false
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}
  • You can just use the same search JSON mentioned in the previous answer, to test that it's not searchable.

Upvotes: 1

Amit
Amit

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.

Mapping

{
    "mappings": {
        "properties": {
            "id": {
                "type": "long"
            },
            "name": {
                "type": "text"
            },
            "designation": {
                "type": "text"
            },
            "address": {
                "type": "text",
                "index" : false --> made `index` to false
            }
        }
    }
}

Index few docs

{
    "address" : "USA",
    "name"  : "Noshaf",
    "id" :  234567892,
    "designation" : "software engineer"
}

{
    "address" : "USA california state",
    "name"  : "opster",
    "id" :  234567890,
    "designation" : "software engineer"
}

A simple match search query in JSON format on address field

{
    "query": {
        "match" : {
            "address" : "USA"
        }
    }
}

Exception from Elasticsearch clearly mention, it's not searchable

"caused_by": { "type": "illegal_argument_exception", "reason": "Cannot search on field [address] since it is not indexed." }

Upvotes: 1

Related Questions