Elasticsearch短语建议collat​​e返回false,即使查询返回结果

时间:2015-11-05 11:04:58

标签: elasticsearch

所以我有以下短语建议模板:

PUT /_search/template/DidYouMean
{
  "template": {
    "size": 0,
    "suggest": {
      "DidYouMean": {
        "text": "{{SearchPhrase}}",
        "phrase": {
          "field": "_all",
          "analyzer": "simple",
          "size": 50,
          "real_word_error_likelihood": 0.9,
          "max_errors": 0.5,
          "gram_size": 3,
          "direct_generator": [
            {
              "field": "_all",
              "suggest_mode": "always",
              "min_word_length": 3
            }
          ],
          "collate": {
            "prune": true,
            "query": {
              "match": {
                "_all": {
                  "query": "{{suggestion}}"
                }
              }
            }
          }
        }
      }
    }
  }
}

我使用此查询运行它:

POST /my_index/_search/template
{
  "template": {
    "id": "DidYouMean"
  },
  "params": {
    "SearchPhrase": "unileve"
  }
}

我希望"collate_match": true得到联合利华(一家公司)的结果,然而,它会带回false。我得到了这个,即使我期待联合利华真的来到这里:

{
   "took": 25,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 220692,
      "max_score": 0,
      "hits": []
   },
   "suggest": {
      "DidYouMean": [
         {
            "text": "unileve",
            "offset": 0,
            "length": 7,
            "options": [
               {
                  "text": "unilever",
                  "score": 0.016954133,
                  "collate_match": false
               },
               {
                  "text": "unilevers",
                  "score": 0.005391976,
                  "collate_match": false
               },
               {
                  "text": "unilet",
                  "score": 0.00062763924,
                  "collate_match": false
               },
               {
                  "text": "uniline",
                  "score": 0.00062518474,
                  "collate_match": false
               },
               {
                  "text": "unilerver",
                  "score": 0.00055350363,
                  "collate_match": false
               },
               {
                  "text": "uniliver",
                  "score": 0.00055350363,
                  "collate_match": false
               },
               {
                  "text": "unielever",
                  "score": 0.0005243993,
                  "collate_match": false
               }
            ]
         }
      ]
   }
}

现在当我自己运行相同的查询时,它会带来结果。

POST /my_index/_search
{
  "query": {
    "match": {
      "_all": {
        "query": "unilever"
      }
    }
  }
}

它确实带来了结果。

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4305,
      "max_score": 3.1709287,
      "hits": [
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "754743",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever Iran Co"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "751585",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever UK Ltd"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "10363",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever (Schweiz) AG"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "647311",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever Taiwan Ltd"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "651158",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever Gulf FZE"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "654498",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever China Ltd"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "654664",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever NV"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "650307",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever Canada Ltd"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "710125",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever Ltd"
            }
         },
         {
            "_index": "my_index",
            "_type": "companies",
            "_id": "722327",
            "_score": 3.1709287,
            "_source": {
               "CompanyName": "Unilever Croatia"
            }
         }
      ]
   }
}

我是否误解了整理的目的?或者我只是在做一些完全错误的事情。请指教。

Elasticsearch版本:1.7.3

1 个答案:

答案 0 :(得分:0)

我能够自己解决这个问题。

的问题:

  1. 我正在创建一个模板。
      预期
    • {{suggestion}}作为参数,未提供
    • 所以我明白它是在没有它的情况下渲染而且匹配查询是针对没有
    • 运行的
  2. 我是如何解决的?

    查询模板需要以某种方式呈现{{suggestion}},所以我不得不逃避这些字符。见:

    PUT /_search/template/DidYouMean
    {
      "template": {
        "size": 0,
        "suggest": {
          "DidYouMean": {
            "text": "{{SearchPhrase}}",
            "phrase": {
              "field": "_all",
              "analyzer": "simple",
              "size": 50,
              "real_word_error_likelihood": 0.9,
              "max_errors": 0.5,
              "gram_size": 3,
              "direct_generator": [
                {
                  "field": "_all",
                  "suggest_mode": "always",
                  "min_word_length": 3
                }
              ],
              "collate": {
                "prune": true,
                "query": {
                  "match": {
                    "_all": {
                      "query": "{{=<% %>=}}{{suggestion}}<%={{ }}=%>"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    How does one use a literal {{ in a Mustache template?回答了如何做到这一点。

    现在它将此作为参数呈现,而不是期望在

    中传递某些内容