【问题标题】:Custom analyzer not working in elasticsearch自定义分析器在弹性搜索中不起作用
【发布时间】:2017-06-29 04:36:08
【问题描述】:

运行弹性版本 1.6

我正在尝试为我在 elasticsearch 中的索引设置自定义分析器。我的索引 / 有一些属性,其中包含一些重音和特殊字符。

就像我的一个属性名称具有这样的值,"name" => "Está loca"。 所以我想要实现的是,每当我试图通过这种方式搜索时, http://localhost:9200/tutorial/helloworld/_search?q=esta

我应该得到 "Está loca" 的结果。我已经浏览了以下链接并配置了必要的分析器,该分析器在链接中进行了说明。 https://www.elastic.co/guide/en/elasticsearch/guide/current/asciifolding-token-filter.html

curl -XPUT 'localhost:9200/tutorial?pretty' -H 'Content-Type: application/json' -d'
{
"mappings":{
  "helloworld":{
  "properties": {
    "name": { 
      "type":           "string",
      "analyzer":       "standard",
      "fields": {
        "folded": { 
          "type":       "string",
          "analyzer":   "folding"
        }
      }
    }
  }
}
},
"settings": {
    "analysis": {
      "analyzer": {
        "folding": {
          "tokenizer": "standard",
          "filter":  [ "lowercase", "asciifolding" ]
        }
      }
    }
  }
}'

我在创建索引时进行了配置,并制作了一些这样的条目进行测试,

curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "name": "Está loca!" }'
curl -X POST 'http://localhost:9200/tutorial/helloworld/2' -d '{ "name": "Está locá!" }'

但是在这样搜索时, http://localhost:9200/tutorial/helloworld/_search?q=esta 什么都没有发生。我只想每当用户以任何语言(例如英语)搜索时,它都应该得到相同的结果。请任何人都可以提供帮助,我如何才能在过去 1 周内实现这一目标。

【问题讨论】:

    标签: curl elasticsearch diacritics elasticsearch-analyzers


    【解决方案1】:

    您将无法在 _all 字段中搜索 esta 关键字。默认情况下,elasticsearch 仅在构造 _all field 时应用标准分析器。

    所以您的以下查询

    GET folding_index1/helloworld/_search?q=esta
    

    在弹性 dsl 中生成以下匹配查询。

    GET folding_index1/helloworld/_search
    {
      "query": {
        "match": {
          "_all": "esta"
        }
      }
    }
    

    针对 _all 字段进行搜索,因此找不到名称的折叠标记。

    您可以执行以下操作,但即使在多字段中提到了include_in_all,它仍然会为 _all 字段应用标准分析器。

    PUT folding_index1
    {
        "mappings": {
            "helloworld": {
                "properties": {
                    "name": {
                        "type": "string",
                        "analyzer": "standard",
                        "fields": {
                            "folded": {
                                "type": "string",
                                "analyzer": "folding",
                                "include_in_all": true
                            }
                        }
                    }
                }
            }
        },
        "settings": {
            "analysis": {
                "analyzer": {
                    "folding": {
                        "tokenizer": "standard",
                        "filter": ["lowercase", "asciifolding"]
                    }
                }
            }
        }
    }
    

    如下查询可以为您工作。更多关于_all field analyzer

    POST folding_index1/_search?q=name.folded:esta
    

    【讨论】:

    • folding_index1
    • 在 curl 中创建索引时出现以下错误,{"error":"MapperParsingException[mapping [helloworld]];nested: ElasticsearchParseException[fielddata should be a hash but was of type: class java .lang.Boolean]; ","status":400}
    • 是的,这就是我用作参考的名称。我更改了映射,看起来您正在运行旧版本的弹性。我为调试标记器添加了 fielddata boolean。现在应该可以工作了
    • 是的,我使用的是旧版本,我认为是 1.6,我会再次尝试更改
    • 你的救命恩人,真的是最衷心的感谢。我从上两周开始就一直在努力,压力很大。非常感谢
    【解决方案2】:

    这个链接也帮了我很多,为我的场景提供了精确的分析器。

    https://vanwilgenburg.wordpress.com/2013/08/03/diacritics-in-elasticsearch/

    【讨论】:

    • 这对我来说在 es 版本 5.4 上失败了。
    • 是的,你的权利@user3775217 但是链接建议的设置和映射非常适合我的场景。你能帮我把链接的设置和映射的json转换成ES 5.4或以上支持的版本吗?我还为此stackoverflow.com/questions/44844765/… 询问了关于stackoverflow 的一个问题
    • 是的,即使官方弹性链接上提到的 shay 也表示,它不支持 _all 的更改分析器。我在 5.4 上,所以我使用自定义分析器将 _all 字段复制到了一个新字段。谢谢
    • 您能否从该链接为我提供 curl 格式的设置和映射的 json,以便我可以在这里应用它,我已经问了一个问题,这是链接 stackoverflow.com/questions/44844765/…。我真的很努力。
    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 2015-12-10
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多