【问题标题】:Elasticsearch ngram query doesn't workElasticsearch ngram 查询不起作用
【发布时间】:2017-07-08 17:49:02
【问题描述】:

我从 elasticsearch 2.0 迁移到 5.2,现在 ngram 搜索被破坏了!

elasticsearch 设置就在下面,它只是一个简单的用于标题和摘要字段的 ngram 标记器。

settings = {
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0,
        "analysis": {
            "filter": {
                "ngram_filter": {
                    "type": "nGram",
                    "min_gram": 3,
                    "max_gram": 10
                }
            },
            "analyzer": {
                "search_ngram_analyzer": {
                    "tokenizer": "standard",
                    "type": "custom",
                    "filter": ["standard", "lowercase", "stop", "asciifolding"]
                },

                "index_ngram_analyzer": {
                    "tokenizer": "standard",
                    "type": "custom",
                    "filter": ["standard", "lowercase", "stop", "asciifolding", "ngram_filter"]
                }
            }
        },

    },
    "mappings": {
        "docs": {
            "properties": {
                'title': {
                    'boost': 100.0,
                    'search_analyzer': 'search_ngram_analyzer',
                    'analyzer': 'index_ngram_analyzer',
                    'type': 'text',
                },
                'summary': {
                    'boost': 20.0,
                    'search_analyzer': 'search_ngram_analyzer',
                    'analyzer': 'index_ngram_analyzer',
                    'type': 'text',
                }
            }
        }

    }
}

http://localhost:9200/my_index/_search?q=example 返回包含单词“example”的文档。作为普通查询。

然而, http://localhost:9200/my_index/_search?q=exampl(以“e”为例)返回一个空对象!

我在我的设置中没有发现错误。这是 API 中断吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您确定这在以前的版本中有效吗?

    如果您使用 URI 搜索并且未指定字段(就像您在 http://localhost:9200/my_index/_search?q=exampl 中所做的那样),则将使用 _all 字段。它使用标准分析器,因此没有 ngram。您要使用的查询是/my_index/_search?q=title:exampl

    为了重现性,这里是控制台整个示例的转储:

    PUT /my_index
    {
      "settings": {
        "analysis": {
          "filter": {
            "ngram_filter": {
              "type": "nGram",
              "min_gram": 3,
              "max_gram": 10
            }
          },
          "analyzer": {
            "search_ngram_analyzer": {
              "tokenizer": "standard",
              "type": "custom",
              "filter": [
                "standard",
                "lowercase",
                "stop",
                "asciifolding"
              ]
            },
            "index_ngram_analyzer": {
              "tokenizer": "standard",
              "type": "custom",
              "filter": [
                "standard",
                "lowercase",
                "stop",
                "asciifolding",
                "ngram_filter"
              ]
            }
          }
        }
      },
      "mappings": {
        "docs": {
          "properties": {
            "title": {
              "boost": 100,
              "search_analyzer": "search_ngram_analyzer",
              "analyzer": "index_ngram_analyzer",
              "type": "text"
            },
            "summary": {
              "boost": 20,
              "search_analyzer": "search_ngram_analyzer",
              "analyzer": "index_ngram_analyzer",
              "type": "text"
            }
          }
        }
      }
    }
    
    
    GET /my_index/_analyze
    {
      "analyzer": "index_ngram_analyzer",
      "text": "example exampl"
    }
    GET /my_index/_analyze
    {
      "analyzer": "search_ngram_analyzer",
      "text": "example exampl"
    }
    
    POST /my_index/docs
    {
      "title": "This is an example",
      "summary": "Some more text"
    }
    
    GET /my_index/_search?q=example
    GET /my_index/_search?q=exampl
    GET /my_index/_search?q=title:exampl
    
    DELETE /my_index
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 2017-02-11
      • 2016-08-11
      • 2018-04-10
      • 2015-01-05
      • 2018-11-09
      • 1970-01-01
      相关资源
      最近更新 更多