
您唯一需要在分数上进行 *** 作的地方是完全匹配,否则按词条位置的顺序将为您提供正确的顺序。让我们通过以下内容了解这一点:
首先创建一个映射,如下所示:
PUT test{ "mappings": { "_doc": { "properties": { "my_field1": { "type": "text", "analyzer": "whitespace", "fields": { "keyword": { "type": "keyword" } } } } } }}我已经创建了
my_field1带有
whitespace分析器的字段,以确保通过仅将空格用作定界符来创建令牌。其次,我创建了一个名为
keywordtype
的子字段
keyword。
keyword将保存输入字符串的非分析值,我们将使用它进行精确匹配。
让我们向索引添加一些文档:
PUT test/_doc/1{ "my_field1": "apple"}PUT test/_doc/2{ "my_field1": "apple tree"}PUT test/_doc/3{ "my_field1": "green apple"}如果使用以下查询搜索术语
apple,则文档的顺序将为2、1、3。
POST test/_doc/_search{ "explain": true, "query": { "query_string": { "query": "apple", "fields": [ "my_field1" ] } }}"explain": true在上面的查询中,在输出中给出分数计算步骤。阅读本文将使您了解文档的评分方式。
我们需要做的就是提高得分以实现完全匹配。我们将对场进行精确匹配
my_field1.keyword。您可能有一个问题,为什么不这样呢
my_field1。这样做的原因是因为
my_field1经过分析,当为3个文档的输入字符串生成令牌时,都会针对此字段存储一个令牌(术语)
apple(以及其他术语(例如,
tree对于doc
2和
greendoc 3,如果存在其他术语))
。当我们在此字段上对术语进行完全匹配时,
apple所有文档都会匹配,并且对每个文档的得分都会产生相似的影响,因此得分没有变化。由于只有一个文档具有
apple与
my_field1.keyword该文档(文档1)相对的确切值,因此可以匹配精确查询,因此我们将对其进行增强。因此查询将是:
{ "query": { "bool": { "should": [ { "query_string": { "query": "apple", "fields": [ "my_field1" ] } }, { "query_string": { "query": ""apple"", "fields": [ "my_field1.keyword^2" ] } } ] } }}以上查询的输出:
{ "took": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1.7260925, "hits": [ { "_index": "test3", "_type": "_doc", "_id": "1", "_score": 1.7260925, "_source": { "my_field1": "apple" } }, { "_index": "test3", "_type": "_doc", "_id": "2", "_score": 0.6931472, "_source": { "my_field1": "apple tree" } }, { "_index": "test3", "_type": "_doc", "_id": "3", "_score": 0.2876821, "_source": { "my_field1": "green apple" } } ] }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)