Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

wildcard 演算子

wildcard

wildcard 演算子を使用すると、検索文字列に任意の文字と一致する特殊文字を使用するクエリが可能になります。

文字
説明

?

任意の 1 文字と一致します。

*

0 文字以上の文字と一致します。

\

エスケープ文字。

wildcard はタームレベルの演算子であるため、query フィールドは分析されません。query フィールドは特殊文字を含む 1 つのタームとして扱われるため、タームレベルの演算子は キーワード アナライザと連携します。分析済みの query フィールドに対してクエリを実行する例については、分析済みフィールドの例」を参照してください。

wildcard の構文は次のとおりです。

{
$search: {
"index": <index name>, // optional, defaults to "default"
"wildcard": {
"query": "<search-string>",
"path": "<field-to-search>",
"allowAnalyzedField": <boolean>,
"score": <options>
}
}
}

wildcard では、次の用語を使用してクエリを作成します。

フィールド
タイプ
説明
必要性
default

query

文字列または複数の文字列の配列

検索する文字列または複数の文字列。

はい

path

文字列または複数の文字列の配列

検索対象となるインデックス付きフィールド。ワイルドカード パスを指定して検索することもできます。

はい

allowAnalyzedField

ブール値

Set to true if you run your query against a field indexed with a tokenizing analyzer. This avoids the field's analyzer from splitting the indexed value into many terms. Fields indexed with the Keyword Analyzer don't require this option.

no

false

score

オブジェクト

一致する検索用語の結果に割り当てられたスコアを変更します。次の選択肢があります。

  • boost: 結果のスコアに指定された数値を掛けます。

  • constant: 結果のスコアを指定された数値に置き換えます。

  • function: 指定された式を使用して結果スコアを置き換えます。

クエリで score を使用する方法については、「結果内のドキュメントのスコアリング」を参照してください。

no

wildcard is a term-level operator, meaning that the query field isn't analyzed. Use the wildcard operator on fields indexed with tokenizing analyzers by setting allowAnalyzedField to true. Fields indexed with the Keyword Analyzer already treat the field value as a single term, so allowAnalyzedField: true isn't required.

If you use wildcard with allowAnalyzedField: true, MongoDB Search applies character filters and token filters based on the specified analyzer or custom analyzer for the field. MongoDB Search skips the tokenization and the output is always a single token.

次の例では、分析対象フィールドで検索を実行する際の wildcard 演算子の挙動が示されています。

フィールドfoo bar baz標準アナライザでインデックス付けされてます。MongoDB Search はフィールドを分析し、foobarbaz としてインデックスを作成します。このフィールドで foo bar* を検索しても何も見つかりません。これは、ワイルドカード演算子が foo bar* を末尾にワイルドカードが付いた単一の検索タームとして扱うためです。つまり、 MongoDB Search はフィールドで foo bar で始まるタームを検索しますが、タームが存在しないため何も見つかりません。

キーワード アナライザーでインデックス付けされたフィールドで *Star Trek* を検索すると、任意のコンテキストでフィールドに文字列 Star Trek が含まれるすべてのドキュメントが見つかります。標準アナライザでフィールドにインデックス付けされた *Star Trek* を検索しても何も見つかりません。これは、 StarTrek の間にスペースがあり、インデックスにスペースが含まれていないためです。

mongoshまたはドライバーとともにエスケープ文字を使用する場合は、エスケープ文字の前にdoubleバックスラッシュを使用する必要があります。

リテラル アスタリスクを含む任意の文字列を検索するワイルドカード式を作成するには、次の式を使用します。

"*\\**"

最初と最後のアスタリスクは任意の文字に一致するワイルドカードとして機能し、\\* はリテラルのアスタリスクに一致します。

注意

次の式を使用して、リテラルのバックスラッシュをエスケープします。

"*\\\*"

The following examples use the movies collection in the sample_mflix database with a custom index definition that uses the keyword analyzer. If you have the sample dataset on your cluster, you can create a MongoDB Search index on the movies collection and run the example queries on your cluster.

次のインデックス定義は、キーワード アナライザを使用して、movies コレクションの title フィールドにインデックスを作成します。

1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}

次の例では、Green D で始まり、その後に任意の数の他の文字が続く映画のタイトルをすべての title フィールドで検索します。

次の例では、文字列 Wom?n? は任意の 1 文字)で始まり、その後にスペースが続き、任意の数の追加文字が続く映画のタイトルを、すべての title フィールドで検索します。

次の例では、エスケープ文字を使用して、title フィールドが疑問符で終わるドキュメントを検索します。

query フィールドの * 文字は任意の文字に一致し、\\? 文字列はリテラルの疑問符に一致します。

戻る

vectorSearch

項目一覧