Q:

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 ?

A:

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.

