moreLikeThis
演算子の制限
moreLikeThis
演算子を使用して、文字列以外の値をクエリすることはできません。文字列以外の値を検索するには、moreLikeThis
クエリと near、range、またはその他の演算子を compound クエリ内で組み合わせて使用できます。
EmbeddedDocument 内の moreLikeThis
演算子を使用して、配列内のドキュメントをクエリすることはできません。
定義
動作
moreLikeThis
クエリを実行すると、 MongoDB Search は次のアクションを実行します。
演算子の
like
オプションで指定した入力ドキュメントに基づいて、限られた数の最も一般的なタームを抽出します。最も一般的なタームに基づいて類似したドキュメントを検索するための論理和( OR )クエリを作成し、その結果を返します。
moreLikeThis
演算子は、 インデックス構成 で指定される アナライザ を使用して類似ドキュメントを検索します。インデックス定義で アナライザを省略すると、 moreLikeThis
演算子はデフォルトの標準 アナライザを使用します。 複数のアナライザを指定すると、 moreLikeThis
演算子は各アナライザを通じて入力テキストを実行し、すべてのアナライザの結果を検索して返します。
MongoDB Search が構築する論理和(OR)を表示するには、moreLikeThis
演算子クエリで explain を使用します。
使用法
moreLikeThis
演算子クエリを実行する前に、1 つ以上の入力ドキュメントを取得することをお勧めします。 入力ドキュメントを取得するには、次のいずれかを実行します。
find()などのクエリまたは別の MQL クエリを実行して、 BSONドキュメントを検索します。
BSONドキュメントを返す集計パイプラインを実行します。
アプリケーション内の他のドキュメント ソースを使用します。
入力ドキュメントを識別したら、それをmoreLikeThis
演算子に渡すことができます。
moreLikeThis
演算子クエリを実行すると、 MongoDB Search はクエリ結果に元の入力ドキュメントを返します。クエリ結果から入力ドキュメントを除外するには、複合演算子クエリでmoreLikeThis
_id
演算子を使用し、equals
mustNot 句で 演算子を使用して入力ドキュメントを で除外します。
構文
moreLikeThis
の構文は次のとおりです。
{ "$search": { "index": index name, // optional, defaults to "default" "moreLikeThis": { "like": [ { <"field-name">: <"field-value">, ... }, ... ], "score": <options> } } }
オプション
moreLikeThis
は、次のオプションを使用してクエリを構築します。
フィールド | タイプ | 説明 | 必要性 |
---|---|---|---|
| 1 つのBSONドキュメントまたはドキュメントの配列 | MongoDB Search がクエリ対象の一般的なタームを抽出するために使用する 1 つ以上のBSONドキュメント。 | 必須 |
| オブジェクト | 一致する検索結果に割り当てる スコア 。 デフォルトのスコアは、次のオプションを使用して変更できます。
クエリで 配列値をクエリする場合、 MongoDB Search は、配列内のクエリに一致する値の数に関係なく、同じスコアを割り当てます。 | 任意 |
例
例では、 sample_mflix
データベース内のmovies
コレクションを使用します。 このセクションの各例では、異なるインデックス定義を使用して、 演算子の異なる機能を示します。
クラスターで例クエリを実行する前に、クラスターにサンプルデータをロードし、推奨されるインデックスを作成します。UI、 API 、または CLI を使用してMongoDB Searchインデックスを作成する方法の詳細については、 MongoDB Search インデックスの管理 を参照してください。インデックス定義では default
という名前が使用されます。
インデックスに default
と名付けると、$search
パイプライン ステージで index
パラメーターを指定する必要がありません。インデックスにカスタム名を付ける場合は、index
パラメーターで指定する必要があります。
例 1: 複数のフィールドを持つ単一のドキュメント
次の例では、 moreLikeThis
演算子を使用して、複数のフィールド値に類似するドキュメントを検索します。 この例では、インデックス定義には、コレクション内のすべての 動的にインデックス付け可能なフィールドタイプ を動的にインデックス化するための 動的 マッピング が含まれています。sample_mflix.movies
コレクションのインデックス定義は次のようになります。
{ "mappings": { "dynamic": true } }
例
次のクエリは、入力映画タイトル「The GofFAther」と入力映画のジャンル「アクション」に似た映画を検索します。 出力を5
の結果に制限する$limit
ステージと、 title
、 released
、 genres
を除くすべてのフィールドを除外する$project
ステージが含まれています。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 moreLikeThis: { 5 like: 6 { 7 "title": "The Godfather", 8 "genres": "action" 9 } 10 } 11 } 12 }, 13 { "$limit": 5}, 14 { 15 $project: { 16 "_id": 0, 17 "title": 1, 18 "released": 1, 19 "genres": 1 20 } 21 } 22 ])
[ { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'Godfather' }, { genres: [ 'Crime', 'Drama' ], title: 'The Godfather', released: ISODate("1972-03-24T00:00:00.000Z") }, { genres: [ 'Crime', 'Drama' ], title: 'The Godfather: Part II', released: ISODate("1974-12-20T00:00:00.000Z") }, { genres: [ 'Crime', 'Drama' ], title: 'The Godfather: Part III', released: ISODate("1990-12-26T00:00:00.000Z") }, { genres: [ 'Action' ], title: 'The Defender', released: ISODate("1994-07-28T00:00:00.000Z") } ]
MongoDB Search 結果には、入力映画タイトル「」と入力映画のジャンル「アクション」に類似する映画が含まれています。
例 2: 結果で除外される入力ドキュメント
次の例では、 find()
を使用して入力ドキュメントを識別し、 moreLikeThis
演算子を使用して類似ドキュメントを検索します。 この例では、インデックス定義では静的マッピングを使用して、 title
、 genres
、および_id
フィールドのみがインデックス化されています。
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "title": { 6 "type": "string" 7 }, 8 "genres": { 9 "type": "string" 10 }, 11 "_id": { 12 "type": "objectId" 13 } 14 } 15 } 16 }
例
次のfind()
クエリは、「The GofFAther」というタイトルの映画を検索し、その結果をmovie
内に保存します。 結果には一致するドキュメントの フィールドとtitle
genres
フィールドのみを含めることを指定します。デフォルトでは、 find
()
コマンドは常に_id
フィールドを返します。このフィールドの値はクラスターによって異なる場合があることに注意してください。
movie = db.movies.find( { title: "The Godfather" }, { genres: 1, title: 1} ).toArray()
[ { _id: ObjectId("573a1396f29313caabce4a9a"), genres: [ 'Crime', 'Drama' ], title: 'The Godfather' } ]
次のクエリでは、複合演算子と moreLikeThis
演算子を使用して、title
フィールドおよび genres
フィールドをクエリし、等価演算子を使用して次の句を用いて入力ドキュメントを除外します。
movie
に保存されている映画に似た映画をクエリするmust
句。入力ドキュメントを除外する
mustNot
句は、_id
値の結果から除外します。 クエリで使用される_id
値は、前のfind()
クエリの結果の_id
値と一致していることに注意してください。
クエリは出力を5
の結果に制限します。 クエリは$project
ステージを使用して、 _id
、 title
、 released
、 genres
フィールドを結果に含めます。
注意
このクエリを実行する前に、13 行目の_id
フィールドの値を、クエリ結果内の_id
フィールドの値に置き換えます。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "compound":{ 5 "must":[{ 6 "moreLikeThis": { 7 "like": movie 8 } 9 }], 10 "mustNot":[{ 11 "equals": { 12 "path": "_id", 13 "value": ObjectId ("573a1396f29313caabce4a9a") 14 } 15 }] 16 } 17 } 18 }, 19 {"$limit": 5}, 20 { 21 "$project": { 22 "_id": 1, 23 "title": 1, 24 "released": 1, 25 "genres": 1 26 } 27 } 28 ])
[ { _id: ObjectId("573a13acf29313caabd27afc"), genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'Godfather' }, { _id: ObjectId("573a1396f29313caabce557f"), genres: [ 'Crime', 'Drama' ], title: 'The Godfather: Part II', released: ISODate("1974-12-20T00:00:00.000Z") }, { _id: ObjectId("573a1398f29313caabcebf7b"), genres: [ 'Crime', 'Drama' ], title: 'The Godfather: Part III', released: ISODate("1990-12-26T00:00:00.000Z") }, { _id: ObjectId("573a1399f29313caabceed8d"), genres: [ 'Action' ], title: 'The Defender', released: ISODate("1994-07-28T00:00:00.000Z") }, { _id: ObjectId("573a139af29313caabcef2a0"), genres: [ 'Action' ], title: 'The Enforcer', released: ISODate("1995-03-02T00:00:00.000Z") } ]
MongoDB Search 結果には、action
ジャンルのクエリタームThe Godfather
に類似するドキュメントが含まれます。ただし、結果には _id
によって除外されたドキュメントは含まれません。このドキュメントは ObjectId("573a1396f29313caabce4a9a")
です。
例 3: 複数のアナライザ
次の例では、 find()
を使用して入力ドキュメントを識別し、 moreLikeThis
演算子を使用して類似ドキュメントを検索します。 この例では、インデックス定義は静的マッピングを使用して、さまざまなアナライザでsample_mflix.movies
コレクション内のフィールドをインデックス化します。 インデックスの定義:
_id
、title
、genres
フィールドにインデックスを構成します。lucene.standard
アナライザと、lucene.keyword
アナライザを使用するkeywordAnalyzer
という名前の代替アナライザを使用してtitle
フィールドを分析します。lucene.english
アナライザを使用してフィールドを分析および検索します。
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "title": { 6 "type": "string", 7 "analyzer": "lucene.standard", 8 "multi": { 9 "keywordAnalyzer": { 10 "type": "string", 11 "analyzer": "lucene.keyword" 12 } 13 } 14 }, 15 "genres": { 16 "type": "string" 17 }, 18 "_id": { 19 "type": "objectId" 20 } 21 } 22 }, 23 "analyzer": "lucene.english" 24 }
例
次のfind()
クエリは、「Alice in Universal」というタイトルの映画を検索し、その結果をmovie
に保存します。 結果には一致するドキュメントの フィールドとtitle
genres
フィールドのみを含めることを指定します。デフォルトでは、 find()
コマンドは常に_id
フィールドを返します。このフィールドの値はクラスターによって異なる場合があることに注意してください。
movie = db.movies.find( { title: "Alice in Wonderland" }, { genres: 1, title: 1} ).toArray
[ { _id: ObjectId("573a1394f29313caabcde9ef"), plot: 'Alice stumbles into the world of Wonderland. Will she get home? Not if the Queen of Hearts has her way.', title: 'Alice in Wonderland' }, { _id: ObjectId("573a1398f29313caabce963d"), plot: 'Alice is in Looking Glass land, where she meets many Looking Glass creatures and attempts to avoid the Jabberwocky, a monster that appears due to her being afraid.', title: 'Alice in Wonderland' }, { _id: ObjectId("573a1398f29313caabce9644"), plot: 'Alice is in Looking Glass land, where she meets many Looking Glass creatures and attempts to avoid the Jabberwocky, a monster that appears due to her being afraid.', title: 'Alice in Wonderland' }, { _id: ObjectId("573a139df29313caabcfb504"), plot: `The wizards behind The Odyssey (1997) and Merlin (1998) combine Lewis Carroll's "Alice in Wonderland" and "Through the Looking Glass" into a two-hour special that just gets curiouser and curiouser.`, title: 'Alice in Wonderland' }, { _id: ObjectId("573a13bdf29313caabd5933b"), plot: "Nineteen-year-old Alice returns to the magical world from her childhood adventure, where she reunites with her old friends and learns of her true destiny: to end the Red Queen's reign of terror.", title: 'Alice in Wonderland' } ]
次の例では、 複合 演算子を使用して、次の句を使用してtitle
genres
フィールドと フィールドをクエリします。
should
句はmoreLikeThis
演算子を使用して、movie
のドキュメントに類似するドキュメントを検索します。title
フィールドは アナライザとlucene.standard
lucene.keyword
アナライザの両方で分析されていることに注意してください。mustNot
句は、_id
値で指定された入力ドキュメントの 1 つを結果に含めることはできないことを指定します。
クエリは結果リストを10
ドキュメントに制限します。 クエリは$project
ステージを使用して、 _id
、 title
、 genres
フィールドを結果に含めます。
例
1 db.movies.aggregate([ 2 { 3 $search: { 4 "compound": { 5 "should": [{ 6 "moreLikeThis": { 7 "like": movie 8 } 9 }], 10 "mustNot": [ 11 { 12 "equals": { 13 "path": "_id", 14 "value": ObjectId ("573a1394f29313caabcde9ef") 15 } 16 }] 17 } 18 } 19 }, 20 { $limit: 10 }, 21 { 22 $project: { 23 "title": 1, 24 "genres": 1, 25 "_id": 1 26 } 27 } 28 ])
[ { _id: ObjectId("573a1398f29313caabce963d"), genres: [ 'Adventure', 'Family', 'Fantasy' ], title: 'Alice in Wonderland' }, { _id: ObjectId("573a1398f29313caabce9644"), genres: [ 'Adventure', 'Family', 'Fantasy' ], title: 'Alice in Wonderland' }, { _id: ObjectId("573a139df29313caabcfb504"), genres: [ 'Adventure', 'Comedy', 'Family' ], title: 'Alice in Wonderland' }, { _id: ObjectId("573a13bdf29313caabd5933b"), genres: [ 'Adventure', 'Family', 'Fantasy' ], title: 'Alice in Wonderland' }, { _id: ObjectId("573a1396f29313caabce3e7e"), genres: [ 'Comedy', 'Drama' ], title: 'Alex in Wonderland' }, { _id: ObjectId("573a13bdf29313caabd5a44b"), genres: [ 'Drama' ], title: 'Phoebe in Wonderland' }, { _id: ObjectId("573a139af29313caabcf0e23"), genres: [ 'Documentary' ], title: 'Wonderland' }, { _id: ObjectId("573a139ef29313caabcfcebc"), genres: [ 'Drama' ], title: 'Wonderland' }, { _id: ObjectId("573a13a0f29313caabd03dab"), genres: [ 'Drama' ], title: 'Wonderland' }, { _id: ObjectId("573a13abf29313caabd2372a"), genres: [ 'Crime', 'Drama', 'Mystery' ], title: 'Wonderland' } ]
次のクエリでは、explainを前述のクエリと併用して、MongoDB Search が類似ドキュメントを検索するために構築する論理和(OR)を示します。
db.movies.explain("queryPlanner").aggregate([ { $search: { "compound": { "should": [{ "moreLikeThis": { "like": [{ "title": "Alice in Wonderland" }] } }], "mustNot": [ { "equals": { "path": "_id", "value": ObjectId ("573a1394f29313caabcde9ef") } }] } } }, { $limit: 10 }, { $project: { "title": 1, "genres": 1, "_id": 1 } } ])
{ explainVersion: '1', stages: [ { '$_internalSearchMongotRemote': { mongotQuery: { compound: { should: [ { moreLikeThis: { like: [ { title: 'Alice in Wonderland' } ] } } ], mustNot: [ { equals: { path: '_id', value: ObjectId("573a1394f29313caabcde9ef") } } ] } }, explain: { type: 'BooleanQuery', args: { must: [], mustNot: [ { path: 'compound.mustNot', type: 'ConstantScoreQuery', args: { query: { type: 'TermQuery', args: { path: '_id', value: '[57 3a 13 94 f2 93 13 ca ab cd e9 ef]' } } } } ], should: [ { path: 'compound.should', type: 'BooleanQuery', args: { must: [], mustNot: [], should: [ { type: 'TermQuery', args: { path: 'title', value: 'in' } }, { type: 'TermQuery', args: { path: 'title.keywordAnalyzer', value: 'Alice in Wonderland' } }, { type: 'TermQuery', args: { path: 'title', value: 'wonderland' } }, { type: 'TermQuery', args: { path: 'title', value: 'alice' } } ], filter: [], minimumShouldMatch: 0 } } ], filter: [], minimumShouldMatch: 0 } } } }, { '$_internalSearchIdLookup': {} }, { '$limit': Long("10") }, { '$project': { _id: true, title: true, genres: true } } ], serverInfo: { ... }, serverParameters: { ... }, command: { aggregate: 'movies', pipeline: [ { '$search': { compound: { should: [ { moreLikeThis: { like: [ { title: 'Alice in Wonderland' } ] } } ], mustNot: [ { equals: { path: '_id', value: ObjectId("573a1394f29313caabcde9ef") } } ] } } }, { '$limit': 10 }, { '$project': { title: 1, genres: 1, _id: 1 } } ], cursor: {}, '$db': 'sample_mflix' }, ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1659133479, i: 1 }), signature: { hash: Binary(Buffer.from("865d9ef1187ae1a74c4a0da1e29882aebcf2be7c", "hex"), 0), keyId: Long("7123262728533180420") } }, operationTime: Timestamp({ t: 1659133479, i: 1 }) }