Search code examples
javaelasticsearchcase-sensitivecase-insensitiveelasticsearch-6

Not able to insert "index":"not_analyzed" in mapping of elasticsearch 6.1.2


I am trying to find a match a name with case sensitive. When I tried below code it worked as expected for case insensitive.

        HashMap<String, String> data = new HashMap<String, String>();
        data.put("GroupId", "3");
        data.put("GroupName", "testGroup three");
        data.put("CreatedDateTime", "20180115130026757+0000");
        data.put("UpdatedDateTime", "20180115130026757+0000");
        data.put("Createdby", "3");
        data.put("GroupUser",  "{1,2,3,4}");
        data.put("status", "active");

        String mapping = {"typename":{"properties":{
        "GroupName":{"type":"text","index":"not_analyzed"},
        "Createdby":{"type":"text","index":"not_analyzed"},
        "GroupUser":{"type":"text","index":"not_analyzed"},
        "UpdatedDateTime":{"type":"text","index":"not_analyzed"},
        "CreatedDateTime":{"type":"text","index":"not_analyzed"},
        "GroupId":{"type":"text","index":"not_analyzed"},
        "status":{"type":"text","index":"not_analyzed"}}}}

        client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(new InetSocketAddress(ipaddress, port)));

        //inserting record
        IndexResponse response = client.prepareIndex(indexName, typeName).setSource(data).get();

        //inserting mapping
        client.admin().indices().preparePutMapping(indexName)
        .setType("typeName")
        .setSource(mapping, XContentType.JSON)
        .get();

To find the case sensitive value, I found that to use the index as not_analyzed. I tried the following

        HashMap<String, String> data = new HashMap<String, String>();
        data.put("GroupId", "3");
        data.put("GroupName", "testGroup three");
        data.put("CreatedDateTime", "20180115130026757+0000");
        data.put("UpdatedDateTime", "20180115130026757+0000");
        data.put("Createdby", "3");
        data.put("GroupUser",  "{1,2,3,4}");
        data.put("status", "active");

        String mapping = {"typename":{"properties":{
        "GroupName":{"type":"text","index":"not_analyzed"},
        "Createdby":{"type":"text","index":"not_analyzed"},
        "GroupUser":{"type":"text","index":"not_analyzed"},
        "UpdatedDateTime":{"type":"text","index":"not_analyzed"},
        "CreatedDateTime":{"type":"text","index":"not_analyzed"},
        "GroupId":{"type":"text","index":"not_analyzed"},
        "status":{"type":"text","index":"not_analyzed"}}}}

        client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(new InetSocketAddress(ipaddress, port)));

        //inserting record
        IndexResponse response = client.prepareIndex(indexName, typeName).setSource(data).get();

        //inserting mapping
        client.admin().indices().preparePutMapping(indexName)
        .setType("typeName")
        .setSource(mapping, XContentType.JSON)
        .get();

I am getting an exception as shown below

 java.lang.IllegalArgumentException: Could not convert [GroupName.index] to boolean

I want to accomplish to two scenarios below:

 1. Find by case sensitive
 2. Find by case insensitive.

Elastic search version is 6.1.2.

Any help is really appreciated.

UPDATE-1

As per @Val, I have changed the code to:

    HashMap<String, String> data = new HashMap<String, String>();
    data.put("GroupId", "3");
    data.put("GroupName", "testGroup three");
    data.put("CreatedDateTime", "20180115130026757+0000");
    data.put("UpdatedDateTime", "20180115130026757+0000");
    data.put("Createdby", "3");
    data.put("GroupUser",  "{1,2,3,4}");
    data.put("status", "active");

    String mapping = {"typename":{"properties":{
    "GroupName":{"type":"keyword"},
    "Createdby":{"type":"keyword"},
    "GroupUser":{"type":"keyword"},
    "UpdatedDateTime":{"keyword":"text"},
    "CreatedDateTime":{"keyword":"text"},
    "GroupId":{"type":"keyword"},
    "status":{"type":"keyword"}}}}

    client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(new InetSocketAddress(ipaddress, port)));

   client.admin().indices().prepareCreate("indexName").get(); 

    //inserting mapping
    client.admin().indices().preparePutMapping(indexName)
    .setType("typeName")
    .setSource(mapping, XContentType.JSON)
    .get();

    //inserting record
    IndexResponse response = client.prepareIndex(indexName, typeName).setSource(data).get();

Now I am able to insert. But when I search for the a value in GroupName, the result is empty.


Solution

  • not_analyzed is deprecated, you need to use keyword instead.

    Try this mapping below, instead:

    String mapping = {
      "typename": {
        "properties": {
          "GroupName": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "Createdby": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "GroupUser": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "UpdatedDateTime": {
            "type": "date",
            "format": "yyyyMMddHHmmssSSSZ"
          },
          "CreatedDateTime": {
            "type": "date",
            "format": "yyyyMMddHHmmssSSSZ"
          },
          "GroupId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "status": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    

    Also make sure to put the mapping before inserting the data, i.e.

    execute:

    //inserting mapping
    

    before:

    //inserting record