Suggester之Term Suggester

概念

补全分类主要分为四类:

  • Term Suggester(纠错补全,输入错误的情况下补全正确的单词)
  • Phrase Suggester(自动补全短语,输入一个单词补全整个短语)
  • Completion Suggester(完成补全单词,输出如前半部分,补全整个单词)
  • Context Suggester(上下文补全)

效果类似如下所示:
在这里插入图片描述
今天我们主要总结一下关于Term Suggester的使用,希望大家查缺补漏。

创建索引mapping

PUT /book4
{
  "mappings": {
    "english": {
      "properties": {
        "passage": {
          "type": "text"
        }
      }
    }
  }
}

插入数据

curl -H "Content-Type: application/json" -XPOST 'http:localhost:9200/_bulk' -d'
{ "index" : { "_index" : "book4", "_type" : "english" } }
{ "passage": "Lucene is cool"}
{ "index" : { "_index" : "book4", "_type" : "english" } }
{ "passage": "Elasticsearch builds on top of lucene"}
{ "index" : { "_index" : "book4", "_type" : "english" } }
{ "passage": "Elasticsearch rocks"}
{ "index" : { "_index" : "book4", "_type" : "english" } }
{ "passage": "Elastic is the company behind ELK stack"}
{ "index" : { "_index" : "book4", "_type" : "english" } }
{ "passage": "elk rocks"}
{ "index" : { "_index" : "book4", "_type" : "english" } }
{  "passage": "elasticsearch is rock solid"}
'

存储的分词为:

post /_analyze
{
  "text": [
    "Lucene is cool",
    "Elasticsearch builds on top of lucene",
    "Elasticsearch rocks",
    "Elastic is the company behind ELK stack",
    "elk rocks",
    "elasticsearch is rock solid"
  ]
}

结果:

{
    "tokens": [
        {
            "token": "lucene",
            "start_offset": 0,
            "end_offset": 6,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "is",
            "start_offset": 7,
            "end_offset": 9,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "cool",
            "start_offset": 10,
            "end_offset": 14,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "elasticsearch",
            "start_offset": 15,
            "end_offset": 28,
            "type": "<ALPHANUM>",
            "position": 103
        },
        {
            "token": "builds",
            "start_offset": 29,
            "end_offset": 35,
            "type": "<ALPHANUM>",
            "position": 104
        },
        {
            "token": "on",
            "start_offset": 36,
            "end_offset": 38,
            "type": "<ALPHANUM>",
            "position": 105
        },
        {
            "token": "top",
            "start_offset": 39,
            "end_offset": 42,
            "type": "<ALPHANUM>",
            "position": 106
        },
        {
            "token": "of",
            "start_offset": 43,
            "end_offset": 45,
            "type": "<ALPHANUM>",
            "position": 107
        },
        {
            "token": "lucene",
            "start_offset": 46,
            "end_offset": 52,
            "type": "<ALPHANUM>",
            "position": 108
        },
        {
            "token": "elasticsearch",
            "start_offset": 53,
            "end_offset": 66,
            "type": "<ALPHANUM>",
            "position": 209
        },
        {
            "token": "rocks",
            "start_offset": 67,
            "end_offset": 72,
            "type": "<ALPHANUM>",
            "position": 210
        },
        {
            "token": "elastic",
            "start_offset": 73,
            "end_offset": 80,
            "type": "<ALPHANUM>",
            "position": 311
        },
        {
            "token": "is",
            "start_offset": 81,
            "end_offset": 83,
            "type": "<ALPHANUM>",
            "position": 312
        },
        {
            "token": "the",
            "start_offset": 84,
            "end_offset": 87,
            "type": "<ALPHANUM>",
            "position": 313
        },
        {
            "token": "company",
            "start_offset": 88,
            "end_offset": 95,
            "type": "<ALPHANUM>",
            "position": 314
        },
        {
            "token": "behind",
            "start_offset": 96,
            "end_offset": 102,
            "type": "<ALPHANUM>",
            "position": 315
        },
        {
            "token": "elk",
            "start_offset": 103,
            "end_offset": 106,
            "type": "<ALPHANUM>",
            "position": 316
        },
        {
            "token": "stack",
            "start_offset": 107,
            "end_offset": 112,
            "type": "<ALPHANUM>",
            "position": 317
        },
        {
            "token": "elk",
            "start_offset": 113,
            "end_offset": 116,
            "type": "<ALPHANUM>",
            "position": 418
        },
        {
            "token": "rocks",
            "start_offset": 117,
            "end_offset": 122,
            "type": "<ALPHANUM>",
            "position": 419
        },
        {
            "token": "elasticsearch",
            "start_offset": 123,
            "end_offset": 136,
            "type": "<ALPHANUM>",
            "position": 520
        },
        {
            "token": "is",
            "start_offset": 137,
            "end_offset": 139,
            "type": "<ALPHANUM>",
            "position": 521
        },
        {
            "token": "rock",
            "start_offset": 140,
            "end_offset": 144,
            "type": "<ALPHANUM>",
            "position": 522
        },
        {
            "token": "solid",
            "start_offset": 145,
            "end_offset": 150,
            "type": "<ALPHANUM>",
            "position": 523
        }
    ]
}

搜索单个字段

POST /book4/_search
{
    "suggest" : {
    "my-suggestion" : {
      "text" : "Elasticsearaach",
      "term" : {
        "field" : "passage",     "suggest_mode": "popular"
      }
    }
  }
}

结果:

{
    "took": 26,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": 0,
        "hits": []
    },
    "suggest": {
        "my-suggestion": [
            {
                "text": "elasticsearaach",
                "offset": 0,
                "length": 15,
                "options": [
                    {
                        "text": "elasticsearch",
                        "score": 0.84615386,
                        "freq": 3
                    }
                ]
            }
        ]
    }
}

搜索多个字段

POST _search
{
  "suggest": {
    "my-suggest-1" : {
      "text" : "tring out Elasticsearch",
      "term" : {
        "field" : "message"
      }
    },
    "my-suggest-2" : {
      "text" : "kmichy",
      "term" : {
        "field" : "user"
      }
    }
  }
}

常见建议选项

text
建议文字。建议文本是一个必需的选项,需要全局设置或根据建议设置。
field
从中获取候选建议的字段。这是一个必需的选项,需要全局设置或根据建议设置。
analyzer
用于分析建议文本的分析器。默认为建议字段的搜索分析器。
size
每个建议文本标记返回的最大更正。
sort
定义建议应该如何按建议文本术语排序。两个可能的值:

  • score:首先按分数排序,然后是文档频率,然后是术语本身。
  • frequency:先按文档频率排序,然后是相似度评分,然后是术语本身。

suggest_mode
建议模式控制什么建议被包括或控制什么建议文本术语,建议应该被建议。可以指定三个可能的值:

  • missing:仅为索引中没有的建议文本条款提供建议。这是默认值。
  • popular:只建议出现在比原来的建议文本术语更多的文档中的建议。
  • always:根据建议文本中的术语提出匹配的建议。

其他术语建议选项

lowercase_terms
在文本分析之后,建议文本术语小写。
max_edits
最大编辑距离候选建议可以具有以便被视为建议。只能是介于1和2之间的值。任何其他值都会导致抛出错误的请求错误。默认为2。
prefix_length
必须匹配的最小前缀字符的数量才是候选建议。默认为1.增加此数字可提高拼写检查性能。通常拼写错误不会出现在术语的开头。(旧名“prefix_len”已弃用)
min_word_length
建议文本术语必须具有的最小长度才能包含在内。默认为4.(旧名称“min_word_len”已弃用)
shard_size
设置从每个单独分片中检索的最大建议数。在减少阶段,仅根据size选项返回前N个建议。默认为该 size选项。将此值设置为高于该值的值size可能非常有用,以便以性能为代价获得更准确的拼写更正文档频率。由于术语在分片之间被划分,因此拼写校正频率的分片级文档可能不准确。增加这些将使这些文档频率更精确。
max_inspections
用于乘以的因子, shards_size以便在碎片级别上检查更多候选拼写更正。可以以性能为代价提高准确性。默认为5。
min_doc_freq

建议应出现的文档数量的最小阈值。可以指定为绝对数字或文档数量的相对百分比。这可以仅通过建议高频项来提高质量。默认为0f且未启用。如果指定的值大于1,则该数字不能是小数。分片级文档频率用于此选项。
max_term_freq
建议文本令牌可以存在的文档数量的最大阈值,以便包括在内。可以是表示文档频率的相对百分比数(例如0.4)或绝对数。如果指定的值大于1,则不能指定小数。默认为0.01f。这可用于排除高频术语的拼写检查。高频术语通常拼写正确,这也提高了拼写检查的性能。分片级文档频率用于此选项。
string_distance
用于比较类似建议术语的字符串距离实现。可以指定五个可能的值: internal- 默认值基于damerau_levenshtein,但高度优化用于比较索引中术语的字符串距离。damerau_levenshtein - 基于Damerau-Levenshtein算法的字符串距离算法。levenshtein - 基于Levenshtein编辑距离算法的字符串距离算法。 jaro_winkler - 基于Jaro-Winkler算法的字符串距离算法。 ngram - 基于字符n-gram的字符串距离算法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走进IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值