Use analisadores específicos de idioma para criar índices personalizados para um idioma específico. Cada analisador de idioma tem palavras vazias e divisões de palavras integradas com base nos padrões de uso desse idioma.
MongoDB Search offers the following language analyzers:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 cjk
é um analisador genérico de chinês, Japonês e Coreano
2 kuromoji
é um analisador Japonês
3 morfologik
é um analyzer polonês
4 nori
é um analisador coreano
5 smartcn
é um analisador chinês
Exemplos
Considere uma coleção denominada cars
com os seguintes documentos:
{ "_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": "הזמן הטוב ביותר לעשות זאת הוא מיד לאחר שמילאת דלק." } }
Exemplo de analisador de idioma integrado
A seguinte definição de índice de exemplo especifica um índice no campo subject.fr
utilizando o analisador french
:
{ "mappings": { "fields": { "subject": { "fields": { "fr": { "analyzer": "lucene.french", "type": "string" } }, "type": "document" } } } }
A seguinte query procura o pour
de string no campo subject.fr
:
db.cars.aggregate([ { $search: { "text": { "query": "pour", "path": "subject.fr" } } }, { $project: { "_id": 0, "subject.fr": 1 } } ])
A query anterior não retorna resultados ao usar o analisador french
, pois pour
é uma palavra vazia embutida. Usando o analisador standard
, a mesma query retornaria os dois documentos.
A seguinte query procura o carburant
de string no campo subject.fr
:
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 returns a document with _id: 1
in the results because the query matched a token that the lucene.french
analyzer created for the document. The lucene.french
analyzer creates the following tokens for the subject.fr
field in document with _id: 1
:
|
|
|
|
|
|
|
|
|
Exemplo de analisador de idioma personalizado
Você também pode criar índices para idiomas sem suporte criando um analisador personalizado com os filtros de token icuFolding e stopword.
O exemplo de definição de índice a seguir especifica um índice no campo subject.he
usando um analisador personalizado chamado myHebrewAnalyzer
para analisar e criar tokens para texto hebraico:
{ "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" } } ] }
A seguinte query procura o המכוניות
de string no campo subject.he
:
db.cars.aggregate([ { $search: { "text": { "query": "המכוניות", "path": "subject.he" } } }, { $project: { "_id": 0, "subject.he": 1 } } ])
{ subject: { he: 'עדיף לצייד את המכוניות שלנו כדי להבין את הגורמים לתאונה.' } }
MongoDB Search returns a document with _id: 1
in the results because the query matched a token that the myHebrewAnalyzer
analyzer created for document. The myHebrewAnalyzer
analyzer creates the following tokens for the subject.he
field in document with _id: 1
:
|
|
|
|
|
|
|
|
|
Exemplo de pesquisa multilíngue
Você também pode criar um índice que usa vários analisadores de linguagem para executar uma pesquisa multilíngue.
The following example index definition specifies an index with dynamic mapping on the sample_mflix.movies
collection. The definition applies the lucene.italian
language analyzer to index the fullplot
field, and uses the multi option to specify lucene.english
as an alternate language analyzer. MongoDB Search uses the default lucene.english
language analyzer for all other fields that it dynamically indexes in the movies
collection.
{ "analyzer": "lucene.standard", "mappings": { "dynamic": true, "fields": { "fullplot": { "type": "string", "analyzer": "lucene.italian", "multi": { "fullplot_english": { "type": "string", "analyzer": "lucene.english", } } } } } }
The following MongoDB Search query uses the following compound operator clauses to query the collection:
must
cláusula pesquisa tramas de filmes em inglês e italiano que contêm o termoBella
usando o operador de textomustNot
cláusula exclui filmes lançados entre os anos 1984 a 2016 usando o operador de intervaloshould
cláusula especifica preferência pelo gêneroComedy
usando o operador de texto
[ { $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" } }] } } } ]
SCORE: 3.909510850906372 _id: "573a1397f29313caabce8bad" plot: "He is a revenge-obssessed stevedore whose sister was brutally raped an…" genres: 0: "Drama" runtime: 137 fullplot: "In Marseilles, a woman commits suicide after she is raped in an alley.…" released: 1983-05-18T00:00:00.000+00:00 SCORE: 3.4253346920013428 _id: "573a1396f29313caabce5735" plot: "Giovanna e' una bella ragazza, ma ha qualche problema con gli uomini: …" genres: 0: "Comedy" runtime: 100 fullplot: "Giovanna e' una bella ragazza, ma ha qualche problema con gli uomini: …" released: 1974-11-15T00:00:00.000+00:00 SCORE: 3.363344430923462 _id: "573a1395f29313caabce13cf" plot: "Gerardo è un attore o almeno cerca di esserlo, ma il pubblico non è de…" genres: 0: "Comedy" runtime: 95 fullplot: "Gerardo è un attore o almeno cerca di esserlo, ma il pubblico non è de…" released: 1960-02-10T00:00:00.000+00:00 SCORE: 1.9502882957458496 _id: "573a1396f29313caabce5299" plot: "Dr Tremayne is an enigmatic Psychiatrist running a Futuristic asylum h…" genres: 0: "Horror" runtime: 90 fullplot: "Dr Tremayne is an enigmatic Psychiatrist running a Futuristic asylum h…" released: 1973-10-31T00:00:00.000+00:00