Elasticsearch 完成建议使用多词输入进行搜索

Elasticsearch completion suggest search with multiple-word inputs

使用 Elasticsearch 完成建议器,我在返回匹配单词查询的多词输入建议时遇到问题。

示例结构:

PUT /test_index/
{
   "mappings": {
      "item": {
         "properties": {
            "test_suggest": {
               "type": "completion",
               "index_analyzer": "whitespace",
               "search_analyzer": "whitespace",
               "payloads": false
            }
         }
      }
   }
}

PUT /test_index/item/1
{
   "test_suggest": {
      "input": [
         "cat dog",
         "elephant"
      ]
   }
}

工作查询:

POST /test_index/_suggest
{
    "test_suggest":{
        "text":"cat",
        "completion": {
            "field" : "test_suggest"
        }
    }
}

结果

{
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "test_suggest": [
      {
         "text": "cat",
         "offset": 0,
         "length": 3,
         "options": [
            {
               "text": "cat dog",
               "score": 1
            }
         ]
      }
   ]
}

查询失败:

POST /test_index/_suggest
{
    "test_suggest":{
        "text":"dog",
        "completion": {
            "field" : "test_suggest"
        }
    }
}

结果

{
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "test_suggest": [
      {
         "text": "dog",
         "offset": 0,
         "length": 3,
         "options": []
      }
   ]
}

我希望得到与工作查询相同的结果,匹配 'cat dog'。有什么建议是什么问题以及如何使失败的查询正常工作?使用标准分析器而不是空白分析器时,我得到了相同的结果。我想在每个输入字符串中使用多个单词,如上例所示。

完成建议器是 prefix suggester,这意味着它会尝试将您的查询与所提供输入的前几个字符相匹配。如果您希望发布的文档与文本 "dog" 匹配,则需要指定 "dog" 作为输入。

PUT /test_index/item/1
{
   "test_suggest": {
      "input": [
         "cat dog",
         "elephant",
         "dog"
      ]
   }
}

根据我的经验,必须指定要匹配的输入的限制使得完成建议不如其他实现前缀匹配的方法有用。我喜欢edge ngrams for this purpose. I recently wrote a blog post about using ngrams that you might find helpful: http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch

举个简单的例子,这是您可以使用的映射

PUT /test_index
{
   "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": {
      "item": {
         "properties": {
            "text_field": {
               "type": "string",
               "index_analyzer": "edge_ngram_analyzer",
               "search_analyzer": "standard"
            }
         }
      }
   }
}

然后像这样索引文档:

PUT /test_index/item/1
{
   "text_field": [
      "cat dog",
      "elephant"
   ]
}

这些查询中的任何一个都会 return 它:

POST /test_index/_search
{
    "query": {
        "match": {
           "text_field": "dog"
        }
    }
}

POST /test_index/_search
{
    "query": {
        "match": {
           "text_field": "ele"
        }
    }
}

POST /test_index/_search
{
    "query": {
        "match": {
           "text_field": "ca"
        }
    }
}

这里是所有的代码:

http://sense.qbox.io/gist/4a08fbb6e42c34ff8904badfaaeecc01139f96cf