MongoDB文本索引通配符权重

MongoDB文本索引通配符权重

本文介绍了MongoDB文本索引通配符权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有示例数据库结构

Suppose I have sample db structure

[
  { name: 'hello world', description: { key: 'something' } },
  { name: 'user', description: { key: 'hello world' } },
]

带有索引

db.fulltext.createIndex({ name: 'text', '$**': 'text' }, { weights: { name: 10, '$**': 5 } })

我正在通过查询查找文档

I am finding documents with the query

db.fulltext.find({ $text: { $search: 'hello world' } }, { score: { $meta: 'textScore' } })

但是...这两个文档都给我15.0分...无法为通配符运算符增加权重吗?为什么第二个文档通过name键乘以分数?

But... It gives me 15.0 score for both documents... It's impossible to add weight to wildcard operator? Why second document multiply score from name key?

推荐答案

通配符索引"$**"在文本索引中包括文档中的所有字符串字段.在上述情况下,name是一个字符串属性,其权重为10,通常所有字符串字段的权重均分配为5(包括name字段,因为使用了通配符).因此,权重将被覆盖.

The wildcard index "$**" includes all the string fields in the document in the text index. In the above scenario, name is a string attribute for which weight was given as 10 and in general all string fields weight was assigned as 5 (including name field because wild card is used). So, the weight is overridden.

完成文本搜索后,所有字符串字段的权重均相等.因此,两个文档的得分都相同,因为与其他索引字段没有相对重要性(即,因为在创建索引时使用了通配符).

When the text search is done, equal weightage is given for all String fields. So, the score is same for both the documents as there is no relative significance to the other indexed fields (i.e. because the wild card was used while creating the index).

当不同的字段需要不同的权重时,您需要在创建索引时专门提供字段名称.换句话说,您不应为字符串"字段提供权重,而应为所有字符串字段都包含通配符权重.显然,一个权重会覆盖另一个权重.

When different weight is need for different fields, you need to provide the field names specifically while creating the index. In other words, you should not provide a weight for a String field and include wild card weight for all string fields. Obviously, one weight will override the other.

如果您可以按如下所述更改索引,则可以看到不同之处.

If you can change the index as mentioned below, you can see the difference.

创建索引:-

db.fulltext.createIndex({ name: 'text', 'description.key' : 'text' }, { weights: { name: 10, 'description.key' : 5 } })

搜索:-

db.fulltext.find({ $text: { $search: 'hello world' } }, { score: { $meta: 'textScore' } })

结果:-

{
    "_id" : ObjectId("57e119cbf522cc85b5595797"),
    "name" : "hello world",
    "description" : {
        "key" : "something"
    },
    "score" : 15
}

{
    "_id" : ObjectId("57e119cbf522cc85b5595798"),
    "name" : "user",
    "description" : {
        "key" : "hello world"
    },
    "score" : 7.5
}

这篇关于MongoDB文本索引通配符权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:39