Elasticsearch ngram查询不起作用

时间:2017-02-19 13:29:35

标签: elasticsearch

我从elasticsearch 2.0转到了5.2,ngram搜索现在已经破了!

弹性搜索设置就在下面,它只是标题和摘要字段的简单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返回文档,其中包含"示例"在里面。作为普通查询。

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

我在设置中找不到错误。这是一个API中断吗?

1 个答案:

答案 0 :(得分:3)

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

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

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

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