【问题标题】:Can't query an edge_ngram field in _all无法查询 _all 中的 edge_ngram 字段
【发布时间】:2016-06-22 03:25:24
【问题描述】:

所以我正在设置一个索引,并且我想要一个单一的搜索来执行部分单词edge_ngram 搜索一个字段并更正常地搜索其余字段。据我了解,这应该很容易通过匹配_all 来完成。但是我似乎无法让它发挥作用。

我已经能够从 bool 查询中获得所需的结果,该查询分别搜索 _all 和特定的 ngram 字段,但这看起来很老套,我猜我只是缺少一些简单的东西。

这里只是一个简单的例子来说明我在做什么以及它对我不起作用。

这是索引设置:

curl -XPUT "http://localhost:9200/test_index?pretty=true" -d'
{
   "settings": {
      "analysis": {
         "filter": {
            "edge_ngram_filter": {
               "type": "edge_ngram",
               "min_gram": 2,
               "max_gram": 20
            }
         },
         "analyzer": {
            "edge_ngram_analyzer": {
               "type": "custom",
               "tokenizer": "standard",
               "filter": [
                  "lowercase",
                  "edge_ngram_filter"
               ]
            }
         }
      }
   },
   "mappings": {
      "doc": {
         "properties": {
            "text_field": {
               "type": "string",
               "analyzer": "edge_ngram_analyzer",
               "search_analyzer": "standard"
            }
         }
      }
   }
}'

并添加一个简单的文档:

curl -XPUT "http://localhost:9200/test_index/doc/1?pretty=true" -d'
{
    "text_field": "Hello, World!"
}'

_all 部分搜索不起作用。它返回一个空结果。

curl -XPOST "http://localhost:9200/test_index/_search?pretty=true" -d'
{
    "query": {
        "match": {
            "_all": "hell"
        }
    }
}'

_虽然所有全词搜索都有效

curl -XPOST "http://localhost:9200/test_index/_search?pretty=true" -d'
{
    "query": {
        "match": {
            "_all": "hello"
        }
    }
}'

并且对特定字段进行部分搜索

curl -XPOST "http://localhost:9200/test_index/_search?pretty=true" -d'
{
    "query": {
        "match": {
            "text_field": "hell"
        }
    }
}'

词向量看起来也不错

curl -XGET "http://localhost:9200/test_index/doc/1/_termvector?fields=text_field&pretty=true"

我真的无法弄清楚我在这里做错了什么。任何帮助将不胜感激。

以下是有关我的环境的一些详细信息。

  • Elasticsearch 版本:Version: 2.3.3, Build: 218bdf1/2016-05-17T15:40:04Z, JVM: 1.8.0_92
  • Linux 操作系统:Arch Linux
  • 内核版本:4.4.3-1-custom

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    _all 字段将所有字段的原始值组合为一个字符串,而不是为每个字段生成的术语。因此,在您的情况下,它不包含edge_ngram_analyzer 生成的术语,仅包含text_field 字段中的文本。它就像任何其他文本字段一样,您可以为其指定分析器等。在您的示例中,它使用默认分析器。

    【讨论】:

    • 感谢您的回答。您是否知道如何更改 _all 字段的分析器?当我尝试将其添加到映射时,我得到Failed to parse mapping [doc]: Field [_all] is defined twice in [doc]
    • 没关系。我想我是通过添加这个来解决的:{mappings: {doc: {_all: {analyzer: "edge_ngram_analyzer"}}}}
    • 是的,就是这样。 _all 映射需要在类型下,而不是在属性根下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多