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

How do I add a weight (or prefilter) for a phrase suggester?

我是短语建议者的新手,一直在阅读文档。 completion suggester 有一种方法可以增加权重,以便对结果进行评分。除非我失踪,否则短语 suggester 没有这样的选择。我有:

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>"
        }
      }
    }
  }
}

我想提出一些建议 more/less 重要 and/or 排除一些。上面的图像返回 "nobel prize" 但我想排除该建议或手动将其加权为不那么重要。这可能吗?

例子直接来自第一个例子found here

编辑 由于我的用户输入的所有短语都保存在 elasticsearch 中,因此上面的代码不会建议更好的短语。例如,如果 "noble prize" 和 "nobel prize" 都在索引和用户类型中 "noble prize" 我想建议 "nobel prize" 但它不会。大多数用户正确输入诺贝尔奖,因此我可以在每次使用查询时递增,从而建议正确的。讲道理。

另一个编辑:奇怪的是,他们假设您的索引不会增长,并且您不想像完成建议器允许您那样对短语进行优先级排序。

一个完整的例子:

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>"
            }
         }
      }
   }
}

结果(错误地)"noble prize":

{
  "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": []
      }
    ]
  }
}

您想实现哪个功能? Did You Mean 还是 Autocomplete?在这种情况下我可以给你其他的建议。

要修剪您的建议结果,您可以像这样使用 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 指示是否有任何建议与您的整理查询相匹配。