【问题标题】:Elasticseach searching partial textElasticsearch 搜索部分文本
【发布时间】:2016-04-20 18:50:26
【问题描述】:

我正在尝试使用以下映射创建索引: createIndexRequestBuilder = client.admin().indices().prepareCreate(document.indexName());

         XContentBuilder mappingBuilder = jsonBuilder()
                .startObject()
                    .startObject("my_type")
                        .startObject("properties")
                            .startObject("nombre")
                                .field("type", "string")
                                .field("index", "not_analyzed")
                            .endObject()                                
                            .startObject("codigo")
                                .field("type", "string")
                                .field("index", "not_analyzed")
                            .endObject()                                
                        .endObject()
                    .endObject()
                .endObject();

         createIndexRequestBuilder.addMapping("my_type", mappingBuilder);

然后,如果我尝试以下搜索:

GET /hogan/tipos-documento/_search?pretty
{ 
     "query": {
        "bool": {
            "must": [
               {"wildcard": {
                  "nombre": {
                     "value": "*Type 2*"
                  }
               }}
            ] 
        }
     }
}

没有问题。但是如果我尝试 "value" : "type 2 没有结果,因为 "wildcard" 区分大小写。

有什么方法可以搜索不敏感的案例吗?也许通过以另一种方式进行索引...

简化,我想要这样的实体:

public class Entity{
    private Long id;
    private String name;
}

并且有一个搜索表单,用户可以输入“姓名”的部分文本(大写或小写)进行搜索

提前致谢

【问题讨论】:

    标签: java elasticsearch


    【解决方案1】:

    您的nombre 字段不应为index: not_analyzed,而应使用keywordlowercase 分析器进行分析。像这样的:

    {
      "settings": {
        "index": {
          "analysis": {
            "analyzer": {
              "keyword_lowercase": {
                "type": "custom",
                "tokenizer": "keyword",
                "filter": [
                  "lowercase"
                ]
              }
    ....
    

    然后,在映射中:

                "nombre": {
                  "type": "string",
                  "analyzer": "keyword_lowercase"
                }
    

    【讨论】:

    • 我已尝试执行此配置,但执行此操作时仍然无法找到结果:“value”:“type 2”,它正在使用:“value”: “类型”。现在我可以不区分大小写进行搜索,但不能使用两个词(类型和 2)
    • 您确定映射已正确应用。索引文档后,检查映射GET /some_index/_mapping
    • 是的,我确定。我检查了 /hogan/_mapping 和 /hogan。使用 /hogan 我可以看到不同的映射。要创建索引,我使用了答案中的代码:stackoverflow.com/a/36745584/6161679
    • analisis 拼写错误。应该是analysis
    猜你喜欢
    • 2022-10-15
    • 2017-04-17
    • 2016-08-11
    • 2016-01-07
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多