Elasticsearch:如何实现短语建议 - phrase suggester

词组建议者(phrase suggester)是术语建议者(term suggester)的高级版本。 短语建议者使用的功能是选择整个校正后的短语而不是单个单词。 这是基于ngram语言建模的,短语建议者可以基于频率和并发性更好地选择token。

在本教程中,我们将向您展示如何使用短语建议者来纠正短语中的拼写,这在Elasticsearch中提供了“您的意思是”的搜索功能。

 

准备数据

为了能够更好地说明phrase suggester,首先让我们来建立一个简单的示例数据。我们创建一个具有四个文档的索引phrase-suggester。我们使用bulk API来把数据导入到Elasticsearch中去:

POST _bulk
{ "index" : { "_index" : "phrase-suggester", "_id": 1 }}
{ "tagline": "The misty windshield and the dash"}
{ "index" : { "_index" : "phrase-suggester", "_id": 2 }}
{ "tagline": "The misty windshield and the dash"}
{ "index" : { "_index" : "phrase-suggester", "_id": 3 }}
{ "tagline": "windhshield was broken and moist"}
{ "index" : { "_index" : "phrase-suggester", "_id": 4 }}
{ "tagline": "days of misty windshield"}

Phrase suggester 示例

让我们看一下phrase suggesster的工作原理。 让我们搜索一个包含三个单词和两个错别字的短语。 这个短语是“windsheild got mitsy”。 您会看到我们在第一个单词和第三个单词中都有错别字。 使用词组提示器查询索引,看看返回了什么:

GET phrase-suggester/_search
{
  "suggest": {
    "text": "windsheild got mitsy",
    "my-phrase_suggestion": {
      "phrase": {
        "field": "tagline"
      }
    }
  }
}

如您所见,上面的查询与我们在上一篇文章中使用的term suggester查询类似,只是“ term”参数被“phrase”参数代替。上述查询返回的结果是:

  "suggest" : {
    "my-phrase_suggestion" : [
      {
        "text" : "windsheild got mitsy",
        "offset" : 0,
        "length" : 20,
        "options" : [
          {
            "text" : "windshield got misty",
            "score" : 0.030922418
          },
          {
            "text" : "windhshield got misty",
            "score" : 0.021743154
          },
          {
            "text" : "windshield got mitsy",
            "score" : 0.018247992
          },
          {
            "text" : "windsheild got misty",
            "score" : 0.017338034
          },
          {
            "text" : "windhshield got mitsy",
            "score" : 0.012831108
          }
        ]
      }
    ]
  }

与term suggester非常相似,这些建议在响应的“options”数组下列出。 在响应中,我们可以看到选项列表的第一个元素是我们正在寻找的具有最高匹配分数的正确短语。 对于选项列表中的第二个元素,仅纠正了一个错字,而选项列表中的最后一个元素则按原样返回了搜索到的短语。

带有选项的phrase suggester

在上面的示例中,我们看到了词组建议程序查询的最基本用法。 但是,查询可以配置许多设置,例如突出显示,置信度,整理等,这些设置旨在微调我们的搜索结果。 让我们探索一些最常见的选项。

考虑以下查询以获取短语建议:

GET phrase-suggester/_search
{
  "size": 0,
  "suggest": {
    "text": "windsheild got mitsy",
    "phrase-suggestion-demo-01": {
      "phrase": {
        "field": "tagline",
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "confidence": 0,
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        },
        "collate": {
          "query": {
            "inline": {
              "match": {
                "": ""
              }
            }
          },
          "params": {
            "field_name": "tagline"
          },
          "prune": true
        }
      }
    }
  }
}

我们探讨一下:

  1. real_word_error_likelihood-此选项的默认值为0.95。该选项告诉Elasticsearch索引中5%的术语拼写错误。这意味着随着此参数的值降低,Elasticsearch会将越来越多的存在于索引中的术语视为拼写错误,即使它们是正确的。
  2. max_errors-为了形成更正而最多被视为拼写错误的术语的最大百分比。预设值为1。
  3. confidence-默认值是1.0,最大值也是。该值用作与建议得分相关的阈值。仅显示分数超过此值的那些建议。例如,置信度为1.0只会返回得分高于输入短语的建议。
  4. highlight-高亮功能是最有用的搜索功能之一。也可以在短语建议程序中启用它。更正的单词将使用此关键字突出显示。如以上查询所示,我们还可以采用哪个标签来突出显示(这里我们使用了<em>标签)。
  5. collat​​e-告诉Elasticsearch根据指定的查询来检查每个建议,以删除索引中不存在匹配文档的建议。在这种情况下,它是一个匹配查询。由于此查询是模板查询,因此搜索查询是当前建议,位于查询的参数下。可以在查询下的“params”对象中添加更多字段。同样,当参数“prune”设置为true时,响应中将有一个附加字段“collat​​e_match”,指示建议结果中是否所有校正后的关键字都匹配。

上述查询的返回结果为:

  "suggest" : {
    "phrase-suggestion-demo-01" : [
      {
        "text" : "windsheild got mitsy",
        "offset" : 0,
        "length" : 20,
        "options" : [
          {
            "text" : "windshield got misty",
            "highlighted" : "<em>windshield</em> got <em>misty</em>",
            "score" : 0.030922418,
            "collate_match" : false
          },
          {
            "text" : "windhshield got misty",
            "highlighted" : "<em>windhshield</em> got <em>misty</em>",
            "score" : 0.021743154,
            "collate_match" : false
          },
          {
            "text" : "windshield got mitsy",
            "highlighted" : "<em>windshield</em> got mitsy",
            "score" : 0.018247992,
            "collate_match" : false
          },
          {
            "text" : "windsheild got misty",
            "highlighted" : "windsheild got <em>misty</em>",
            "score" : 0.017338034,
            "collate_match" : false
          },
          {
            "text" : "windhshield got mitsy",
            "highlighted" : "<em>windhshield</em> got mitsy",
            "score" : 0.012831108,
            "collate_match" : false
          }
        ]
      }
    ]
  }

在上面的回复中,我们可以看到突出显示的文本,以提出建议的更正。 还有collate_match,用于响应查询中的“prune”参数。 您会看到一个没有两个更正关键字的结果的值为false。

您可以更改“ confidence”,“ real_word_error_likelihood”和“ max_errors”参数的值,并比较更改以更好地理解这些参数。

 

总结

与术语建议者不同,短语建议者可以将查询与整个短语匹配,并返回最相关的结果。

参考

【1】https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-suggesters.html#phrase-suggester

发布了489 篇原创文章 · 获赞 107 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/UbuntuTouch/article/details/103952092
今日推荐