言語固有のアナライザ を使用して、特定の言語にカスタマイズされたインデックスを作成します。 各言語アナライザには、その言語の使用パターンに基づくストップワードと単語の除算が組み込まれています。
MongoDB Search は、次の言語アナライザを提供します。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 cjk は、一般的な中国語、日本語、大文字と小文字のアナライザです
2 kuromoji は、日本語のアナライザです
3 morfologik は、ポーランド語アナライザです
4 nori は、 韓国語アナライザ
5 smartcn は中国語アナライザです
例
次のドキュメントを含むcarsという名前のコレクションについて考えてみます。
{ "_id": 1, "subject": { "en": "It is better to equip our cars to understand the causes of the accident.", "fr": "Mieux équiper nos voitures pour comprendre les causes d'un accident.", "he": "עדיף לצייד את המכוניות שלנו כדי להבין את הגורמים לתאונה." } }
{ "_id": 2, "subject": { "en": "The best time to do this is immediately after you've filled up with fuel", "fr": "Le meilleur moment pour le faire c'est immédiatement après que vous aurez fait le plein de carburant.", "he": "הזמן הטוב ביותר לעשות זאת הוא מיד לאחר שמילאת דלק." } }
組み込み言語アナライザの例
次のインデックス定義の例では、 frenchアナライザを使用してsubject.frフィールドのインデックスを指定します。
{ "mappings": { "fields": { "subject": { "fields": { "fr": { "analyzer": "lucene.french", "type": "string" } }, "type": "document" } } } }
以下のMongoDB Searchクエリは、subject.frフィールドの文字列pourを検索します。このクエリを実行するには、以下を使用してクラスターに接続します。mongoshを使用してクラスターに接続し、cars コレクションを含むデータベースに切り替えます。
db.cars.aggregate([ { $search: { "text": { "query": "pour", "path": "subject.fr" } } }, { $project: { "_id": 0, "subject.fr": 1 } } ])
上記のクエリでは、 frenchアナライザを使用しても結果が返されません。 pourは組み込みのストップワードであるためです。 standardアナライザを使用すると、同じクエリで両方のドキュメントが返されます。
以下のMongoDB Searchクエリは、subject.frフィールドの文字列carburantを検索します。このクエリを実行するには、mongoshを使用してクラスターに接続し、carsコレクションを含むデータベースに切り替えます。
db.cars.aggregate([ { $search: { "text": { "query": "carburant", "path": "subject.fr" } } }, { $project: { "_id": 0, "subject.fr": 1 } } ])
{ subject: { fr: "Le meilleur moment pour le faire c'est immédiatement après que vous aurez fait le plein de carburant." } }
MongoDB Search では、結果に _id: 1 を含むドキュメントが返されます。このドキュメントは、ドキュメント用に lucene.frenchアナライザが作成したトークンとクエリが一致したためです。lucene.frenchアナライザは、_id: 1 を使用してドキュメントの subject.frフィールドに次のトークンを作成します。
|
|
|
|
|
|
|
|
|
カスタム言語アナライザの例
また、 icuフォールディング と ストップワード トークン フィルターを使用して カスタムアナライザ を作成し、サポートされていない言語のインデックスを作成することもできます。
次のインデックス定義の例では、 myHebrewAnalyzerというカスタムアナライザを使用してヘブライ テキストのトークンを分析および作成し、 subject.heフィールドのインデックスを指定します。
{ "analyzer": "lucene.standard", "mappings": { "dynamic": false, "fields": { "subject": { "fields": { "he": { "analyzer": "myHebrewAnalyzer", "type": "string" } }, "type": "document" } } }, "analyzers": [ { "charFilters": [], "name": "myHebrewAnalyzer", "tokenFilters": [ { "type": "icuFolding" }, { "tokens": [ "אן", "שלנו", "זה", "אל" ], "type": "stopword" } ], "tokenizer": { "type": "standard" } } ] }
以下のMongoDB Searchクエリは、subject.heフィールドの文字列המכוניותを検索します。このクエリを実行するには、mongoshを使用してクラスターに接続し、carsコレクションを含むデータベースに切り替えます。
db.cars.aggregate([ { $search: { "text": { "query": "המכוניות", "path": "subject.he" } } }, { $project: { "_id": 0, "subject.he": 1 } } ])
{ subject: { he: 'עדיף לצייד את המכוניות שלנו כדי להבין את הגורמים לתאונה.' } }
MongoDB Search では、結果に _id: 1 を含むドキュメントが返されます。このクエリは、myHebrewAnalyzerアナライザがドキュメント用に作成したトークンとクエリが一致したためです。myHebrewAnalyzerアナライザは、_id: 1 を使用してドキュメントの subject.heフィールドに次のトークンを作成します。
|
|
|
|
|
|
|
|
|
多言語検索の例
複数の言語アナライザを使用して多言語検索を実行するインデックスを作成することも可能です。
次のインデックス定義の例では、 sample_mflix.moviesコレクションに 動的マッピング を含むインデックスを指定しています。この定義は、lucene.italian言語アナライザを適用して fullplotフィールドをインデックスし、マルチオプションを使用して lucene.english を代替言語アナライザとして指定します。MongoDB Search は、 moviesコレクション内の動的にインデックスを作成する他のすべてのフィールドに対してデフォルトのlucene.english言語アナライザを使用します。
{ "analyzer": "lucene.standard", "mappings": { "dynamic": true, "fields": { "fullplot": { "type": "string", "analyzer": "lucene.italian", "multi": { "fullplot_english": { "type": "string", "analyzer": "lucene.english", } } } } } }
次のMongoDB Searchクエリでは、複合演算子を使用して、複数言語でコレクションをクエリします。このクエリを実行するには、mongoshを使用してクラスターに接続し、sample_mflixデータベースに切り替えます。
複合演算子には、次の句が含まれます。
must句は、text 演算子を使用してBellaという用語を含む英語とイタリア語の映画のプロットを検索しますmustNot句は、1984 年から 2016 年の間に公開された映画を range 演算子を使用して除外しますshould句は、text 演算子を使ってComedyジャンルの好みを指定します
db.movies.aggregate([ { $search: { "index": "multilingual-tutorial", "compound": { "must": [{ "text": { "query": "Bella", "path": { "value": "fullplot", "multi": "fullplot_english" } } }], "mustNot": [{ "range": { "path": "released", "gt": ISODate("1984-01-01T00:00:00.000Z"), "lt": ISODate("2016-01-01T00:00:00.000Z") } }], "should": [{ "text": { "query": "Comedy", "path": "genres" } }] } } }, { $project: { "_id": 0, "title": 1, "plot": 1, "genres": 1, "runtime": 1, "fullplot": 1, "released": 1, "score": { "$meta": "searchScore" } } } ])
[ { plot: "Giovanna e' una bella ragazza, ma ha qualche problema con gli uomini: tutti la vogliono solo usare, anche il suo fidanzata Claudio. Trovera' una via d'uscita diventando vigile urbano. Come ...", genres: [ 'Comedy' ], runtime: 100, title: 'Policewoman', fullplot: "Giovanna e' una bella ragazza, ma ha qualche problema con gli uomini: tutti la vogliono solo usare, anche il suo fidanzata Claudio. Trovera' una via d'uscita diventando vigile urbano. Come Giovanna d'Arco, il suo idolo, non guardera' in faccia a nessuno e con l'aiuto del pretore Patane', innamorato di lei, smascherera' una serie di intrallazzi e corruzione denunciando perfino il suo capo, Marcellini. I due paladini della giustizia coroneranno il loro sogno d'amore, trasferiti in una lontana isoletta a sud della Sicilia, ma i corrotti resteranno al loro posto.", released: ISODate('1974-11-15T00:00:00.000Z'), score: 3.4109344482421875 }, { plot: `Gerardo è un attore o almeno cerca di esserlo, ma il pubblico non è del suo parere. Cosè, per arrotondare gli introiti, aiuta l'amico Lallo in un suo "lavoretto". Questo gli costa perè la ...`, genres: [ 'Comedy' ], runtime: 95, title: 'Love and Larceny', fullplot: `Gerardo è un attore o almeno cerca di esserlo, ma il pubblico non è del suo parere. Cosè, per arrotondare gli introiti, aiuta l'amico Lallo in un suo "lavoretto". Questo gli costa perè la prigione, dove incontra Chinotto e Gloria Patri. Uscito inizia, con l'opposizione di Annalisa che lo vuole sposare, una carriera come truffatore, dapprima in societè con Chinotto e quindi con la bella Elena. Tutto sembra filare a gonfie vele, e le truffe divengono sempre piè grosse e di successo. Ma a volte è destino che il ragno resti preso dalla stessa tela che tesse.`, released: ISODate('1960-02-10T00:00:00.000Z'), score: 3.3489856719970703 }, { plot: 'He is a revenge-obssessed stevedore... She is a wealthy, elusive woman. They try hard to get together... or do they?', genres: [ 'Drama' ], runtime: 137, title: 'The Moon in the Gutter', fullplot: "Nightly, Gerard broods in an alley hoping to catch his sister's attacker. He lives with his lover Bella whom he neglects, an alcoholic brother who lurks about, and his father who's stayed drunk since the daughter's death, ignoring work and his own companion. At a seedy bar, Gerard meets a wealthy, nihilistic hedonist and his beautiful sister. Gerard flips for her and thinks she's his ticket out of the slum...", released: ISODate('1983-05-18T00:00:00.000Z'), score: 3.2985665798187256 }, { plot: 'Dr Tremayne is an enigmatic Psychiatrist running a Futuristic asylum housing four very special cases. Visited by colleague Nicholas, Tremayne explains his amazing and controversial theories...', genres: [ 'Horror' ], runtime: 90, title: 'Tales That Witness Madness', fullplot: "Dr Tremayne is an enigmatic Psychiatrist running a Futuristic asylum housing four very special cases. Visited by colleague Nicholas, Tremayne explains his amazing and controversial theories as to why each of the four patients went mad... cue four distinct tales each with a different set of characters: 'Mr Tiger' tells of Paul, the sensitive and troubled young son of prosperous but constantly bickering and unlovely parents, and the boy's 'imaginary' friend, a tiger. 'Penny Farthing' tells of Timothy, an antique store owner propelled backwards in time by a penny-farthing bicycle in his shop, all the while being watched over by the constantly changing photograph of Uncle Albert, which endangers the lives of both Timothy and his beautiful wife, Ann. 'Mel' tells of Brian, a man who brings home an old dead tree and prominently displays it in his living room as a work of art. His fiery wife Bella soon becomes jealous of the tree, which the husband has lovingly named Mel, and it seems to be developing a will of its own. 'Luau' tells of Auriol, a flamboyant and ambitious literary agent who will do anything to impress her sinister new client, though he seems more interested in Auriol's beautiful and precocious young daughter Ginny. Ginny sneaks off on holiday while Auriol plans a sumptuous feast for her client.", released: ISODate('1973-10-31T00:00:00.000Z'), score: 1.9504895210266113 } ]