O analisador simple
divide o texto em termos pesquisáveis (tokens) onde quer que ele encontre um caractere que não seja uma letra, como espaços em branco, pontuação ou um ou mais dígitos. Converte todo o texto em minúsculas.
Se você selecionar Refine Your Index, a UI do Atlas exibirá uma seção intitulada View text analysis of your selected index configuration dentro da seção Index Configurations. Se você expandir esta seção, a UI do Atlas exibirá o índice e os tokens de pesquisa que o analisador simple
gera para cada string de amostra. É possível ver os tokens que o analisador simple
cria para um documento de amostra integrado e uma cadeia de query ao criar ou editar um índice na UI do Atlas Visual Editor.
Importante
O MongoDB Search não indexará campos de string em que os tokens do analisador excedam 32766 bytes de tamanho. Se estiver usando o analisador de palavras-chave, os campos de string que excederem 32766 bytes não serão indexados.
Exemplo
A seguinte definição de índice de exemplo especifica um índice no title
campo na coleçãosample_mflix.movies utilizando o simple
analisador. Para acompanhar esse exemplo, carregue os dados de amostra em seu cluster e use mongosh
ou navegue até a Create a Search
Index página na UI do Atlas seguindo as etapas do tutorial Criar um índice de pesquisa do MongoDB .
Em seguida, usando a movies
collection como sua fonte de dados, siga o procedimento de exemplo para criar um índice a partir de ou da UI do mongosh
Atlas Visual Editor JSON editorou.
➤ Use o menu suspenso Selecione seu idioma para definir a interface para o exemplo nesta página.
Clique em Refine Your Index para configurar seu índice.
Na seção Index Configurations, alterne Dynamic Mapping para off.
Na seção Field Mappings, clique em Add Field para abrir a janela Add Field Mapping.
Clique em Customized Configuration.
Selecione
title
no menu suspenso Field Name.Clique no menu suspenso Data Type e selecione String se ainda não estiver selecionado.
Expanda o String Properties e faça as seguintes alterações:
Analisador de índice
Selecione
lucene.simple
no menu suspenso.Analisador de pesquisa
Selecione
lucene.simple
no menu suspenso.Opções de índice
Use o
offsets
padrão.Store
Use o
true
padrão.Ignorar acima
Manter a configuração padrão.
Normas
Use o
include
padrão.Clique em Add.
Clique em Save Changes.
Clique em Create Search Index.
Substitua a definição de índice padrão pela seguinte definição de índice.
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "lucene.simple" } } } } Clique em Next.
Clique em Create Search Index.
1 db.movies.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "type": "string", 8 "analyzer": "lucene.simple" 9 } 10 } 11 } 12 } 13 )
A seguinte query pesquisa o termo lion
no campo title
e limita a saída a cinco resultados.
Clique no botão Query para o seu índice.
Clique em Edit Query para editar a query.
Clique em na barra de query e selecione o banco de dados e a coleção.
Substitua a query padrão pelo seguinte e clique em Find:
[ { "$search": { "text": { "query": "lion", "path": "title" } } } ] SCORE: 3.9090898036956787 _id: "573a13cbf29313caabd8135d" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "According to the legend of the Shangaan, white lions are the messenger…" genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-09-02 00:45:38.833000000" num_mflix_comments: 2 plot: "According to the legend of the Shangaan, white lions are the messenger…" poster: "https://m.media-amazon.com/images/M/MV5BMTcwMTAyMzg5OV5BMl5BanBnXkFtZT…" rated: "PG" released: 2010-02-19T00:00:00.000+00:00 runtime: 88 title: "White Lion" type: "movie" writers: Array (3) year: 2010 SCORE: 3.363236427307129 _id: "573a1399f29313caabcee7fc" awards: Object cast: Array (4) countries: Array (1) directors: Array (2) fullplot: "A young lion Prince is cast out of his pride by his cruel uncle, who c…" genres: Array (3) imdb: Object languages: Array (4) lastupdated: "2015-08-31 00:04:32.670000000" metacritic: 83 num_mflix_comments: 132 plot: "Lion cub and future king Simba searches for his identity. His eagernes…" poster: "https://m.media-amazon.com/images/M/MV5BYTYxNGMyZTYtMjE3MS00MzNjLWFjNm…" rated: "G" released: 1994-06-24T00:00:00.000+00:00 runtime: 89 title: "The Lion King" tomatoes: Object type: "movie" writers: Array (29) year: 1994 SCORE: 3.363236427307129 _id: "573a13a9f29313caabd1f600" awards: Object cast: Array (4) countries: Array (2) directors: Array (1) fullplot: "Timon and Pumbaa start to watch the original Lion King movie, but Timo…" genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-09-14 00:01:14.313000000" num_mflix_comments: 0 plot: "Timon the meerkat and Pumbaa the warthog retell the story of The Lion …" poster: "https://m.media-amazon.com/images/M/MV5BYzg2N2Y1ODYtY2QyMi00ZDAyLWE3MT…" rated: "G" released: 2004-02-10T00:00:00.000+00:00 runtime: 77 title: "The Lion King 1 1/2" tomatoes: Object type: "movie" writers: Array (5) year: 2004 SCORE: 3.363236427307129 _id: "573a13abf29313caabd24af6" awards: Object cast: Array (4) countries: Array (2) directors: Array (1) fullplot: "Timon and Pumbaa start to watch the original Lion King movie, but Timo…" genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-08-31 05:44:38.700000000" num_mflix_comments: 0 plot: "Timon the meerkat and Pumbaa the warthog retell the story of The Lion …" poster: "https://m.media-amazon.com/images/M/MV5BYzg2N2Y1ODYtY2QyMi00ZDAyLWE3MT…" rated: "G" released: 2004-02-10T00:00:00.000+00:00 runtime: 77 title: "The Lion King 1 1/2" tomatoes: Object type: "movie" writers: Array (5) year: 2004 SCORE: 2.9511470794677734 _id: "573a1396f29313caabce366e" awards: Object cast: Array (4) countries: Array (2) directors: Array (1) fullplot: "Christmas 1183--an aging and conniving King Henry II plans a reunion w…" genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-09-17 01:39:32.220000000" num_mflix_comments: 0 plot: "1183 AD: King Henry II's three sons all want to inherit the throne, bu…" poster: "https://m.media-amazon.com/images/M/MV5BMTkzNzYyMzA5N15BMl5BanBnXkFtZT…" rated: "PG" released: 1968-10-30T00:00:00.000+00:00 runtime: 134 title: "The Lion in Winter" tomatoes: Object type: "movie" writers: Array (2) year: 1968 SCORE: 2.9511470794677734 _id: "573a13c1f29313caabd63be7" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) genres: Array (1) imdb: Object languages: Array (1) lastupdated: "2015-04-24 02:38:23.767000000" num_mflix_comments: 0 poster: "https://m.media-amazon.com/images/M/MV5BMTg4Mzg4NDk5MF5BMl5BanBnXkFtZT…" released: 2009-11-06T00:00:00.000+00:00 runtime: 92 title: "Son of a Lion" tomatoes: Object type: "movie" writers: Array (1) year: 2007 SCORE: 2.9511470794677734 _id: "573a13dbf29313caabdaf30d" awards: Object cast: Array (4) countries: Array (2) directors: Array (1) fullplot: "Neo-Nazi falls in love with a woman who has a black son and finds hims…" genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-08-15 00:13:18.457000000" num_mflix_comments: 0 plot: "Neo-Nazi falls in love with a woman who has a black son and finds hims…" poster: "https://m.media-amazon.com/images/M/MV5BY2M4ZjI5NmMtZjcyNy00NWU3LWI2Zj…" released: 2013-10-18T00:00:00.000+00:00 runtime: 104 title: "Heart of a Lion" tomatoes: Object type: "movie" writers: Array (1) year: 2013 SCORE: 2.629019260406494 _id: "573a1397f29313caabce5e62" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "At the beginning of the 20th century an American woman is abducted in …" genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-09-02 00:17:16.943000000" num_mflix_comments: 2 plot: "At the beginning of the 20th century an American woman is abducted in …" poster: "https://m.media-amazon.com/images/M/MV5BYTNhODI4NWYtYzc1Zi00OGIxLTk5ZW…" rated: "PG" released: 1975-10-26T00:00:00.000+00:00 runtime: 119 title: "The Wind and the Lion" tomatoes: Object type: "movie" writers: Array (1) year: 1975 SCORE: 2.629019260406494 _id: "573a13ebf29313caabdcfc8d" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "A documentary on young actress, Marianna Palka, as she confronts her r…" genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-09-03 00:37:45.227000000" num_mflix_comments: 0 plot: "A documentary on young actress, Marianna Palka, as she confronts her r…" poster: "https://m.media-amazon.com/images/M/MV5BMTgzMTc2OTg2N15BMl5BanBnXkFtZT…" released: 2014-01-18T00:00:00.000+00:00 runtime: 15 title: "The Lion's Mouth Opens" type: "movie" writers: Array (1) year: 2014 SCORE: 2.3702940940856934 _id: "573a139af29313caabcf0ccd" awards: Object cast: Array (4) countries: Array (2) directors: Array (2) fullplot: "Simba and Nala have a daughter, Kiara. Timon and Pumbaa are assigned t…" genres: Array (3) imdb: Object languages: Array (1) lastupdated: "2015-08-24 00:49:09.900000000" num_mflix_comments: 0 plot: "Simba's daughter is the key to a resolution of a bitter feud between S…" poster: "https://m.media-amazon.com/images/M/MV5BY2Y3MTk2MDgtOTc1Yy00ZmFjLThlNT…" rated: "G" released: 1998-10-27T00:00:00.000+00:00 runtime: 81 title: "The Lion King 2: Simba's Pride" tomatoes: Object type: "movie" writers: Array (10) year: 1998
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "text": { 5 "query": "lion", 6 "path": "title" 7 } 8 } 9 }, 10 { 11 "$limit": 5 12 }, 13 { 14 "$project": { 15 "_id": 0, 16 "title": 1 17 } 18 } 19 ])
[ { title: 'White Lion' }, { title: 'The Lion King' }, { title: 'The Lion King 1 1/2' }, { title: 'The Lion King 1 1/2' }, { title: 'Lion's Den' }, ]
O MongoDB Search retorna esses documentos fazendo o seguinte para o texto no campo title
usando o analisador lucene.simple
:
Converta texto em minúsculas.
Crie tokens separados dividindo o texto sempre que houver um caractere que não seja uma letra.
A tabela a seguir mostra os tokens que o MongoDB Search cria usando o Simple Analyzer e, por outro lado, o Standard Analyzer e o Whitespace Analyzer para os documentos nos resultados:
Título | Tokens de analisador simples | Tokens de analisador padrão | Tokens do analisador de espaço em branco |
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
O MongoDB Search retorna o documento Lion's Den
nos resultados porque o analisador simple
cria um token separado para lion
, que corresponde ao termo de query lion
. Por outro lado, se você indexar o campo usando o Standard Analyzer ou o Whitespace Analyzer, o MongoDB Search retornará alguns dos documentos nos resultados da query, mas não Lion's Den
, pois esses analisadores criariam os tokens lion's
e Lion's
respectivamente, mas não crie um token para lion
.