1

アプリケーションでmongoテキスト検索を使用しています。

索引:

db.test.createIndex(
    {
        title: 'text',
        description: 'text'
    },
    {
        name: "TextIndex",
        weights: {
           title: 10,
           description: 1
        }
    }
)

スコア:

title : 10
description : 1

書類:

db.test.insert(
  [
    { _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: 4, title: "agent", "description" : "production"},
    { _id: 5, title: "test", "description" : "production example agent"},
  ]
)

質問

「エージェント プロダクション」で検索すると

結果は

[
  { _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.

結果:なし

クエリフレーズ: 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" }

どんな提案でも感謝します。

4

1 に答える 1

2

$textクエリの $search 文字列がどのように機能するかを確認しましょう。のように語句が指定されている場合、その語句に"$search": "\"agent production\""一致するインデックス付きフィールドを持つドキュメントのみがゼロ以外のスコアを受け取ります。これが、この場合に結果が見つからなかった理由を説明しています。ただし、 を指定"$search": "\"production agent\""すると、ドキュメントは と一致します_id: 3。のように個々の単語/用語が指定されている場合"$search": "\"agent\" \"production\""、いずれかの用語に一致するインデックス付きフィールドを持つドキュメントがスコアを受け取ります。_id: 4これは、目的の結果で示したように、単一のフィールドに必ずしも両方の用語が含まれているとは限らない個々の用語が含まれているため、 document with が返される理由を説明しています。

両方の検索用語が 1 つのフィールドに含まれるようにするには、クエリに句を追加する必要があります。ドキュメントをスコアリングするためのテキスト検索を実行し、次のように正規表現を使用してさらにフィルタリングできます。

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 が追加されることに注意してください。

于 2016-02-18T06:08:30.523 に答える