简体   繁体   中英

java : Case insensitive search in Elasticsearch

I'm trying to find out the documents in the index regardless of whether if it's field values are lowercase or uppercase in the index.

This is the index structure, I have designed with the custom analyzer. I'm new to analyzers and I might be wrong. This is how it looks :

POST arempris/emptagnames
{
  "settings": {
      "analyzer": {
        "lowercase_keyword": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      }
  },
  "mappings" : {
    "emptags":{
          "properties": {
                "employeeid": {
                  "type":"integer"
                },
                "tagName": {
                  "type": "text",
                  "fielddata": true,
                  "analyzer": "lowercase_keyword"
                }
            }    
        }
    }
}

In the java back-end, I'm using BoolQueryBuilder to find tagnames using employeeids first. This is what I've coded to fetch the values :

BoolQueryBuilder query = new BoolQueryBuilder();
            query.must(new WildcardQueryBuilder("tagName", "*June*"));
            query.must(new TermQueryBuilder("employeeid", 358));

            SearchResponse response12 = esclient.prepareSearch(index).setTypes("emptagnames")
                    .setQuery(query)
                    .execute().actionGet();

            SearchHit[] hits2 = response12.getHits().getHits();
            System.out.println(hits2.length);

            for (SearchHit hit : hits2) {
                Map map = hit.getSource();
                System.out.println((String) map.get("tagName"));
            }

It works fine when I specify the tag to be searched as "june" in lowercase, but when I specify it as "June" in the WildCardQueryBuilder with an uppercase for an alphabet, I'm not getting any match.

Let me know where have I committed the mistake. Would greatly appreciate your help and thanks in advance.

There are two type of queries in elasticsearch

The rules for full text queries is

  • First it looks for search_analyzer in query
  • If not mentioned then it uses index time analyzer for that field for searching.

So in this case you need to change your query to this

BoolQueryBuilder query = new BoolQueryBuilder();
        query.must(new QueryStringQueryBuilder("tagName:*June*"));
        query.must(new TermQueryBuilder("employeeid", 358));

        SearchResponse response12 = esclient.prepareSearch(index).setTypes("emptagnames")
                .setQuery(query)
                .execute().actionGet();

        SearchHit[] hits2 = response12.getHits().getHits();
        System.out.println(hits2.length);

        for (SearchHit hit : hits2) {
            Map map = hit.getSource();
            System.out.println((String) map.get("tagName"));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM