如何为短语建议者添加权重(或预过滤器)?

时间:2017-05-12 18:35:51

标签: elasticsearch elasticsearch-5

我是短语建议者的新手,并且一直在阅读文档。完成建议器可以添加权重,以便对结果进行评分。除非我失踪,否则建议者这个短语没有这样的选择。我有:

POST test/_search
{
  "suggest": {
    "text": "noble prize",
    "simple_phrase": {
      "phrase": {
        "field": "title.trigram",
        "size": 1,
        "gram_size": 3,
        "direct_generator": [ {
          "field": "title.trigram",
          "suggest_mode": "always"
        } ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

我想提出一些更重要/更不重要的建议和/或排除一些建议。上面的图片带有&#34;诺贝尔奖&#34;但是我想排除这个建议或者手动加权它不太重要。这可能吗?

该示例直接来自第一个示例found here

EDIT 由于我的用户传入的所有短语都保存在elasticsearch中,因此上述代码不会提示更好的短语。例如,如果&#34;贵族奖&#34;和&#34;诺贝尔奖&#34;在索引和用户类型&#34;贵宾奖&#34;我想建议&#34;诺贝尔奖&#34;但它没有赢。诺贝尔奖的大多数用户类型都是正确的,因此我可以在每次使用查询时递增,因此建议使用正确的查询。要有意义。

另一个编辑:很奇怪他们认为你的索引不会成长,并且你不想像完成建议者那样优先处理短语。

一个完整的例子:

PUT test
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "analysis": {
        "analyzer": {
          "trigram": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["standard", "shingle"]
          },
          "reverse": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["standard", "reverse"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "title": {
          "type": "text",
          "fields": {
            "trigram": {
              "type": "text",
              "analyzer": "trigram"
            },
            "reverse": {
              "type": "text",
              "analyzer": "reverse"
            }
          }
        }
      }
    }
  }
}
POST test/test
{"title": "noble prize", "weight": 1}
POST test/test
{"title": "nobel prize", "weight": 2}

查询:

GET /test/_search
{
   "suggest": {
      "text": "noble prize",
      "simple_phrase": {
         "phrase": {
            "field": "title.trigram",
            "size": 1,
            "gram_size": 3,
            "direct_generator": [
               {
                  "field": "title.trigram",
                  "suggest_mode": "always"
               }
            ],
             "collate": {
               "query": {
                  "inline": {
                     "match": {
                        "title": "{{suggestion}}"
                     }
                  }
               },
               "prune": true
            },
            "highlight": {
               "pre_tag": "<em>",
               "post_tag": "</em>"
            }
         }
      }
   }
}

导致(错误地)&#34;贵族奖品&#34;:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "suggest": {
    "simple_phrase": [
      {
        "text": "noble prize",
        "offset": 0,
        "length": 11,
        "options": []
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

您要实施哪些功能? Did You MeanAutocomplete?在这种情况下,我可以给你其他建议。

要修剪您的建议结果,您可以使用collate,如下所示:

{
   "suggest": {
      "text": "iphon",
      "simple_phrase": {
         "phrase": {
            "field": "title.trigram",
            "size": 1,
            "gram_size": 3,
            "direct_generator": [
               {
                  "field": "title.trigram",
                  "suggest_mode": "always"
               }
            ],
             "collate": {
               "query": {
                  "inline": {
                     "match": {
                        "title": "{{suggestion}}"
                     }
                  }
               },
               "prune": true
            },
            "highlight": {
               "pre_tag": "<em>",
               "post_tag": "</em>"
            }
         }
      }
   }
}

在结果中,您将有一个"collate_match": true/false,表示是否有任何建议与您的整理查询匹配。