Overview
path パラメータは、検索するフィールドを指定するためにMongoDB Search 演算子によって使用されます。これには以下が含まれる場合があります。
A string
文字列の配列
マルチアナライザ仕様
文字列とマルチアナライザの仕様の組み合わせを含む配列
注意
すべての演算子がすべての異なるタイプのパスを使用できるわけではありません。 サポートされるパスのタイプの詳細については、各演算子のドキュメントを参照してください。
使用法
単一のインデックス付きフィールド のみを検索するには、path パラメータで引用符で囲まれたstringを使用します。 次の例では、 descriptionという名前のフィールドを検索しています。
"path": "description"
複数のインデックス付きフィールド を検索するには、 pathパラメーターで引用符で囲まれた文字列の配列を使用します。 指定されたフィールドのいずれかに一致するドキュメントが結果セットに含まれます。 次の例では、description フィールドとtype フィールドを検索しています。
"path": [ "description", "type" ]
注意
multiパス オプションは、 string 型のフィールドでのみ使用できます。
インデックス定義に複数のアナライザを持つフィールドが含まれている場合は、使用するアナライザを指定できます。 pathパラメーターは、次のフィールドを持つオブジェクトを取ることができます。
フィールド | 説明 |
|---|---|
| 検索するフィールドの名前。 |
| インデックス定義内の |
| ネストされたフィールドを含む、検索するフィールドの名前内の任意の文字と一致するためのワイルドカード文字
ワイルドカード パスは次の演算子によってのみ受け入れられます。 ワイルドカード パスも強調表示に使用できます。 |
次のインデックス定義では、 namesとnotesという名前のフィールドが標準アナライザを使用します。 commentsという名前のフィールドはデフォルトのアナライザとしてstandardを使用し、 lucene.whitespaceアナライザを使用するmySecondaryAnalyzerという名前のmultiも指定します。
{ "mappings": { "dynamic": false, "fields": { "names": { "type": "string", "analyzer": "lucene.standard" }, "notes": { "type": "string", "analyzer": "lucene.standard" }, "comments": { "type": "string", "analyzer": "lucene.standard", "multi": { "mySecondaryAnalyzer": { "analyzer": "lucene.whitespace", "type": "string" } } } } } }
次のpathの例では、インデックス定義でmySecondaryAnalyzerという名前のmultiを使用してcommentsフィールドを検索します。
"path": { "value": "comments", "multi": "mySecondaryAnalyzer" }
複数の アナライザ でインデックス付きフィールドとフィールドの組み合わせを検索するには、配列を使用します。 次の例では、デフォルトのアナライザでnames notesフィールドと フィールドを検索し、インデックス定義で という名前のcomments を使用してmulti mySecondaryAnalyzerフィールドを検索します。
"path": [ "names", "notes", { "value": "comments", "multi": "mySecondaryAnalyzer" } ]
次のpathの例では、インデックス定義でmySecondaryAnalyzerという名前のmultiを使用して、文字nとそれに続く任意の文字を含むすべてのフィールドと、 commentsフィールドを検索します。
"path": [{ "wildcard": "n*" }, { "value": "comments", "multi": "mySecondaryAnalyzer" }]
例
以下の例では、次のドキュメントを含むcarsという名前のコレクションを使用します。
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers." } { "_id" : 2, "type" : "coupe", "make" : "BMW", "description" : "Red two-door convertible, driver's-side airbag." } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Black four-door SUV, three rows of seats." }
静的フィールドマッピングを使用すると、コレクション内の個々のフィールドのインデックス作成と検索方法を指定できます。
carsコレクションのインデックス定義は次のとおりです。
{ "mappings": { "dynamic": false, "fields": { "make": { "type": "string", "analyzer": "lucene.standard" }, "description": { "type": "string", "analyzer": "lucene.standard", "multi": { "simpleAnalyzer": { "analyzer": "lucene.simple", "type": "string" } } } } } }
上記のインデックス定義では、 makeフィールドが標準アナライザでインデックス付けされることを指定しています。 descriptionフィールドはデフォルトでstandardアナライザを使用しますが、 multiパラメータとともにsimpleAnalyzerを指定することで単純なアナライザを使用することもできます。
単一フィールド検索
次の例では、 makeフィールドで string Fordを検索します。
db.cars.aggregate([ { $search: { "text": { "query": "Ford", "path": "make" } } } ])
上記の例では、 _id: 3を含むドキュメントが返されます。
複数のフィールド検索
次の例では、 パラメーターのフィールドの配列を使用して、path bluemakedescriptionフィールドまたは フィールドのいずれかで string を検索します。
db.cars.aggregate([ { $search: { "text": { "query": "blue", "path": [ "make", "description" ] } } } ])
上記のクエリは、次の結果を返します。
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers." }
代替アナライザ検索
簡単なアナライザの例
次の例では、インデックス定義でmulti という名前のsimpleAnalyzer を使用し、 単純なアナライザを使用します。
クエリは、 descriptionフィールドで string driverを検索します。
db.cars.aggregate([ { $search: { "text": { "query": "driver", "path": { "value": "description", "multi": "simpleAnalyzer" } } } } ])
上記のクエリは、次の結果を返します。
{ "_id" : 2, "type" : "coupe", "make" : "BMW", "description" : "Red two-door convertible, driver's-side airbag." }
単純アナライザ driver's
side airbag[driver s side airbagは を としてインデックス付けするため、driver と一致します。
対照的に、デフォルトの 標準アナライザ driver's side airbag[driver's side airbagはdriver's を としてインデックス化するため、 または には一致しますが、side driverには一致しません。
空白アナライザの例
carsコレクションのインデックス定義内のmultiオブジェクトが次のようにあるとします。
"multi": { "simpleAnalyzer": { "analyzer": "lucene.whitespace", "type": "string" } }
次の例では、multi simpleAnalyzer空白アナライザ を使用するインデックス定義で という名前の を使用します。
db.cars.aggregate([ { $search: { "text": { "query": "Three", "path": { "value": "description", "multi": "simpleAnalyzer" } } } } ])
上記のクエリは、次の結果を返します。
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers." }
Three というタームに対する上記のクエリでは、 MongoDB Search ではタームThree に一致するドキュメントのみが返され、three は返されません。これは、空白アナライザが大文字と小文字を区別するためです。対照的に、デフォルトの標準アナライザは大文字と小文字を区別せず、クエリのタームに一致するすべてのドキュメントをコレクションにリストされている順序で返します。
ここで、次のクエリを考えてみましょう。
db.cars.aggregate([ { $search: { "compound": { "should": [ { "text": { "path": "description", "query": "Three" } }, { "text": { "query": "Three", "path": { "value" : "description", "multi" : "simpleAnalyzer" }, score: { boost: { value: 2 }} } } ] } } }, { $project: { "_id": 0, "type": 1, "description": 1, "score": { "$meta": "searchScore" } } } ])
上記のクエリは、次の結果を返します。
{ "type" : "sedan", "description" : "Blue four-door sedan, lots of trunk space. Three to four passengers seats.", "score" : 1.1092689037322998 } { "type" : "SUV", "description" : "Black four-door SUV, three rows of seats.", "score" : 0.17812025547027588 }
上記のクエリでは、 MongoDB Search は Three と three の両方を含むドキュメントを返します。ただし、Three を含む結果のスコアは高くなります。これは、three を含むドキュメントはデフォルトの標準アナライザを使用してマッチングされている間に、Three を含むドキュメントは指定された simpleAnalyzer とデフォルトの標準アナライザの両方に一致しているためです。
次の例では、次のドキュメントを含むpostsというコレクションを使用しています。
{ "_id": 1, "username": "pinto", "post": { "date": "12-03-2018", "forum": "Tofu Recipes", "body": "Spicy Garlic Tofu cooks up crispy in 10 minutes or less. Serve with broccoli and rice for a delicious vegetarian meal." } } { "_id": 2, "username": "paloma", "post": { "date": "12-08-2018", "forum": "Tofu Recipes", "body": "Crispy Tofu in Shiitake Broth has flavors of citrus and umami. Great as an appetizer or entree." } }
動的フィールドマッピングを使用すると、必要に応じてコレクション内のすべてのフィールドにインデックスを作成できます。
postsコレクションのインデックス定義は次のとおりです。
{ "mappings": { "dynamic": true } }
ネストされたフィールド検索
次の複合演算子クエリは、フィールド post.body で broccoli という文字列を検索し、さらにフィールドに cauliflower という文字列を含めないことを指定します。
db.posts.aggregate([ { $search: { "compound": { "must": { "text": { "query": "broccoli", "path": "post.body" } }, "mustNot": { "text": { "query": "cauliflower", "path": "post.body" } } } } } ])
上記のクエリでは、 _id: 1を含むドキュメントが返されます。ここでは、 posts.bodyフィールドに string broccoliが含まれます。
ワイルドカード フィールド検索
次の例では、次のドキュメントを含むcarsという名前のコレクションを使用します。
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Four-door sedan, lots of trunk space. Three to four passengers.", "warehouse" : [ { "inventory" : 3, "color" : "red" } ] } { "_id" : 2, "type" : "coupe", "make" : "BMW", "description" : "Two-door convertible, driver's-side airbag.", "warehouse" : [ { "inventory" : 5, "color" : "black" } ] } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Four-door SUV, three rows of seats.", "warehouse" : [ { "inventory" : 7, "color" : "white" }, { "inventory" : 3, "color" : "red" } ] }
carsコレクションのインデックス定義は次のとおりです。
{ "mappings": { "dynamic": true } }
次のクエリは、ワイルドカード文字*を使用して指定されたフィールドで string redを検索します。
すべてのフィールド検索の例
次のクエリは、すべてのフィールドでstring red を検索します。
db.cars.aggregate([ { "$search": { "phrase": { "path": { "wildcard": "*" }, "query": "red" } } } ])
クエリは、次の結果を返します。
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Four-door sedan, lots of trunk space. Three to four passengers.", "warehouse" : [ { "inventory" : 3, "color" : "red" } ] } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Four-door SUV, three rows of seats.", "warehouse" : [ { "inventory" : 7, "color" : "white" }, { "inventory" : 3, "color" : "red" } ] }
ネストされたフィールド検索の例
次のクエリは、 warehouseフィールド内にネストされているフィールドで string redを検索します。
db.cars.aggregate([ { "$search": { "text": { "path": { "wildcard": "warehouse.*" }, "query": "red" } } } ])
クエリは、次の結果を返します。
{ "_id" : 1, "type" : "sedan", "make" : "Toyota", "description" : "Four-door sedan, lots of trunk space. Three to four passengers.", "warehouse" : [ { "inventory" : 3, "color" : "red" } ] } { "_id" : 3, "type" : "SUV", "make" : "Ford", "description" : "Four-door SUV, three rows of seats.", "warehouse" : [ { "inventory" : 7, "color" : "white" }, { "inventory" : 3, "color" : "red" } ] }