简体   繁体   English

java:Elasticsearch中不区分大小写的搜索

[英]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. 在Java后端中,我使用BoolQueryBuilder首先使用employeeids查找标记名。 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. 当我以小写形式指定要搜索的标签为“ june”时,它可以正常工作,但是在WildCardQueryBuilder中以大写字母形式将其指定为“ June”时,我找不到任何匹配项。

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 Elasticsearch有两种查询类型

The rules for full text queries is 全文查询的规则是

  • First it looks for search_analyzer in query 首先,它在查询中查找search_analyzer
  • 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"));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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