文本搜索-mongodb

文本搜索-mongodb,mongodb,full-text-search,text-search,Mongodb,Full Text Search,Text Search,我正在我的应用程序中使用mongo文本搜索 索引: 分数: 文件: 问题: 所以如果我搜索“代理制作” 结果应该是 [ { _id: 1, title: "agent de production", description: "production or agent"}, { _id: 2, title: "agent test production", description: "agent" }, { _id: 3, title: "production agent", "des

我正在我的应用程序中使用mongo文本搜索

索引: 分数: 文件: 问题: 所以如果我搜索“代理制作”

结果应该是

[
  { _id: 1, title: "agent de production", description: "production or agent"},
  { _id: 2, title: "agent test production", description: "agent" },
  { _id: 3, title: "production agent", "description" : "production"},
  { _id: 5, title: "test", "description" : "production example agent"},
]
我所尝试的: 结果:无

查询短语:db.test.find({“$text”:{“$search”:“\“agent\”\“production\”})

结果

{ "_id" : 5, "title" : "test", "description" : "production example agent" }
{ "_id" : 1, "title" : "agent de production", "description" : "production or agent" }
{ "_id" : 3, "title" : "production agent", "description" : "production" }
{ "_id" : 2, "title" : "agent test production", "description" : "agent" }
{ "_id" : 4, "title" : "agent", "description" : "production" }

任何建议都将不胜感激。

让我们回顾一下查询中的$search字符串是如何工作的。如果给定短语,如在
“$search”:“\“代理生产\”
,则只有索引字段与短语匹配的文档才会获得非零分。这就解释了为什么在这个案例中没有发现结果。但是,指定
“$search”:“\'production agent\”
将使文档与
\u id:3
匹配。如果给出了单个单词/术语,如
“$search”:“\“agent\”\“production\”
,则任何具有索引字段且与任何术语匹配的文档都会获得分数。这解释了为什么返回id为4的文档,因为它在一个字段中包含单独的术语,而不一定是两个术语,正如您在所需结果中所示

要强制在单个字段中包含两个搜索词,需要向查询中添加其他子句。您可以执行文本搜索对文档进行评分,并使用正则表达式对其进行进一步筛选,如中所示:

db.test.find( { $and: [ { "$text": { "$search": "\"agent\" \"production\"" } },
    { $or: [
        { $and: [ { title: /agent/i }, { title: /production/i } ] }, 
        { $and: [ { description: /agent/i }, { description: /production/i } ] }
    ] }
 ] }, { score: { $meta: "textScore" } } ).sort( { score: { $meta: "textScore" } } )

请注意,添加textScore是因为默认情况下文档不会根据分数进行排序。

您是否尝试过为每个单独的术语指定短语,如
db.test.find({“$text”:{“$search”:“\”代理\“\”生产\“}”)?@chridam是的,它没有给出预期的结果。我们需要看看会发生什么。“非预期结果”可以是从零到所有文档的任何内容。我不明白。第二个结果是预期的结果。正确返回包含这两个搜索短语的文档。也许你应该加上你所期望的?
[
  { _id: 1, title: "agent de production", description: "production or agent"},
  { _id: 2, title: "agent test production", description: "agent" },
  { _id: 3, title: "production agent", "description" : "production"},
  { _id: 5, title: "test", "description" : "production example agent"},
]
db.test.find({"$text" : {"$search" : "\"agent production\""}}); Query result does not match with the expected result.
{ "_id" : 5, "title" : "test", "description" : "production example agent" }
{ "_id" : 1, "title" : "agent de production", "description" : "production or agent" }
{ "_id" : 3, "title" : "production agent", "description" : "production" }
{ "_id" : 2, "title" : "agent test production", "description" : "agent" }
{ "_id" : 4, "title" : "agent", "description" : "production" }
db.test.find( { $and: [ { "$text": { "$search": "\"agent\" \"production\"" } },
    { $or: [
        { $and: [ { title: /agent/i }, { title: /production/i } ] }, 
        { $and: [ { description: /agent/i }, { description: /production/i } ] }
    ] }
 ] }, { score: { $meta: "textScore" } } ).sort( { score: { $meta: "textScore" } } )