ElasticSearch完成建议标准分析器无法正常工作

时间:2016-08-29 15:31:01

标签: elasticsearch autocomplete autosuggest

我们将ElasticSearch completion suggesterStandard Analyzer一起使用,但似乎文本未被标记化。

e.g。

文字:“第一个例子”,“第二个例子”

搜索:“Fi”返回“第一个示例”

虽然

搜索:“Ex”不会返回任何结果返回“First Example”

2 个答案:

答案 0 :(得分:3)

作为关于完成建议者的弹性文档:Completion Suggester

  

完成建议器是一个所谓的前缀建议器。

因此,当您发送关键字时,它会查找您的文字的前缀。

E.g:

搜索:“Fi”=> “第一个例子”

搜索:“秒”=> “第二个例子”

但是如果你给Elastic“Ex”,它什么也不返回,因为它找不到以“Ex”开头的文本。

您可以尝试其他一些建议,例如:Term Suggester

答案 1 :(得分:0)

一个很好的解决方法是自己标记字符串并将其放在单独的标记字段中。 然后,您可以在建议查询中使用2条建议来搜索这两个字段。

示例:

PUT /example
{
    "mappings": {
        "doc": {
            "properties": {
                "full": {
                    "type": "completion"
                },
                "tokens": {
                    "type": "completion"
                }
            }
        }
    }
}

POST /example/doc/_bulk
{ "index":{} }
{"full": {"input": "First Example"}, "tokens": {"input": ["First", "Example"]}}
{ "index":{} }
{"full": {"input": "Second Example"}, "tokens": {"input": ["Second", "Example"]}}

POST /example/_search
{
    "suggest": {
        "full-suggestion": {
            "prefix" : "Ex", 
            "completion" : { 
                "field" : "full",
                "fuzzy": true
            }
        },
        "token-suggestion": {
            "prefix": "Ex",
            "completion" : { 
                "field" : "tokens",
                "fuzzy": true
            }
        }
    }
}

搜索结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "suggest": {
    "full-suggestion": [
      {
        "text": "Ex",
        "offset": 0,
        "length": 2,
        "options": []
      }
    ],
    "token-suggestion": [
      {
        "text": "Ex",
        "offset": 0,
        "length": 2,
        "options": [
          {
            "text": "Example",
            "_index": "example",
            "_type": "doc",
            "_id": "Ikvk62ABd4o_n4U8G5yF",
            "_score": 2,
            "_source": {
              "full": {
                "input": "First Example"
              },
              "tokens": {
                "input": [
                  "First",
                  "Example"
                ]
              }
            }
          },
          {
            "text": "Example",
            "_index": "example",
            "_type": "doc",
            "_id": "I0vk62ABd4o_n4U8G5yF",
            "_score": 2,
            "_source": {
              "full": {
                "input": "Second Example"
              },
              "tokens": {
                "input": [
                  "Second",
                  "Example"
                ]
              }
            }
          }
        ]
      }
    ]
  }
}