0

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.

Raviteja Gannoju
  • 117
  • 3
  • 16

1 Answers1

1

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
Val
  • 207,596
  • 13
  • 358
  • 360
  • Please check my updated question. When I tried type as keyword. I see different error saying `mapper [CreatedDateTime] of different type, current_type [text], merged_type [keyword]` – Raviteja Gannoju Feb 20 '18 at 10:27
  • make sure to delete your index first – Val Feb 20 '18 at 10:27
  • I did, its the same – Raviteja Gannoju Feb 20 '18 at 10:29
  • The mapping for your date fields is not correct. See my updated answer – Val Feb 20 '18 at 10:35
  • If I do mapping before indexing, I am getting `no such index` error. – Raviteja Gannoju Feb 20 '18 at 10:39
  • Call `client.admin().indices().prepareCreate("indexName").get();` before `putMapping`. Or you can do it in one shot [like this](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-admin-indices.html#java-admin-indices-put-mapping) – Val Feb 20 '18 at 10:40
  • Insertion is successful, but when I search work in groupname, it is not returning the values. For a groupname for example Groupname is "General Group" and when I search for "General" it returned as 0 for match query. – Raviteja Gannoju Feb 20 '18 at 10:50
  • That's a big step forward. now show your query please – Val Feb 20 '18 at 12:06
  • { "bool" : { "must" : [ { "match" : { "GroupName" : { "query" : "General" } } } ], } } – Raviteja Gannoju Feb 20 '18 at 12:53
  • Since you want exact search you must search for `General Group` (with the same casing) otherwise it won't work, that's the whole purpose of the `keyword` type, the value is not analyzed at all (i.e. not tokenized, not lowercase, etc). – Val Feb 20 '18 at 13:06
  • but our requirement is to find both 1. Find by case sensitive 2. Find by case insensitive. along with full match and partial match of values – Raviteja Gannoju Feb 21 '18 at 06:24
  • It wasn't clear from your question. Then the mapping is a bit different, see my updated answer. You can then run case-insensitive searches on the `GroupName` field and case-sensitive searches on the `GroupName.keyword` field. – Val Feb 21 '18 at 06:45
  • Can you suggest me how to check an index exist before creating the index using rest high client in presently I had using elasticsearch 6.1.3 – Raviteja Gannoju Feb 22 '18 at 10:35
  • For those interested, the new question/answer is here: https://stackoverflow.com/questions/48927002/how-to-find-index-exists-in-elasticsearch-6-2-1/48927070#48927070 – Val Feb 22 '18 at 13:05