Elasticsearch全文自动完成

时间:2014-10-24 11:31:32

标签: python elasticsearch python-requests

我正在通过python requests库使用Elasticsearch。我已经设置了我的分析器:

"analysis" : {
        "analyzer": {
            "my_basic_search": {
                "type": "standard",
                "stopwords": []
            },
            "my_autocomplete": {
                "type": "custom",
                "tokenizer": "keyword",
                "filter": ["lowercase", "autocomplete"]
            }
        },
        "filter": {
            "autocomplete": {
                "type": "edge_ngram",
                "min_gram": 1,
                "max_gram": 20,
            }
        }
    }

我有一个我想使用自动填充搜索的艺术家列表:我目前的测试用例是'bill w',它应该匹配'bill withers'等 - artist映射看起来像这(这是GET http://localhost:9200/my_index/artist/_mapping)的输出:

{
  "my_index" : {
    "mappings" : {
      "artist" : {
        "properties" : {
          "clean_artist_name" : {
            "type" : "string",
            "analyzer" : "my_basic_search",
            "fields" : {
              "autocomplete" : {
                "type" : "string",
                "index_analyzer" : "my_autocomplete",
                "search_analyzer" : "my_basic_search"
              }
            }
          },
          "submitted_date" : {
            "type" : "date",
            "format" : "basic_date_time"
          },
          "total_count" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}

...然后我运行此查询来执行自动完成:

"query": {
        "function_score": {
            "query": {
                "bool": {
                    "must" : { "match": { "clean_artist_name.autocomplete": "bill w" } },
                    "should" : { "match": { "clean_artist_name": "bill w" } },
                }
            },
            "functions": [
            {
                "script_score": {
                    "script": "artist-score"
                }
            }
            ]
        }
    }

这似乎与包含'bill'或'w'以及'bill withers'的艺术家相匹配:我只想匹配包含该字符串的艺术家。分析器似乎工作正常,这是http://localhost:9200/my_index/_analyze?analyzer=my_autocomplete&text=bill%20w的输出:

{
  "tokens" : [ {
    "token" : "b",
    "start_offset" : 0,
    "end_offset" : 6,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "bi",
    "start_offset" : 0,
    "end_offset" : 6,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "bil",
    "start_offset" : 0,
    "end_offset" : 6,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "bill",
    "start_offset" : 0,
    "end_offset" : 6,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "bill ",
    "start_offset" : 0,
    "end_offset" : 6,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "bill w",
    "start_offset" : 0,
    "end_offset" : 6,
    "type" : "word",
    "position" : 1
  } ]
}

那么为什么这里不排除只有'bill'或'w'的匹配?我的查询中是否存在允许仅与my_basic_search分析器匹配的结果?

1 个答案:

答案 0 :(得分:1)

我相信你需要一个“术语”过滤器,而不是“必须”的“匹配”过滤器。您已经在ngrams中拆分了您的艺术家名称,因此您的搜索文本应该与ngrams中的一个完全匹配。为此,您需要一个与ngrams完全匹配的“术语”:

"query": {
    "function_score": {
        "query": {
            "bool": {
                "must" : { "term": { "clean_artist_name.autocomplete": "bill w" } },
                "should" : { "match": { "clean_artist_name": "bill w" } },
            }
        },
        "functions": [
        {
            "script_score": {
                "script": "artist-score"
            }
        }
        ]
    }
}