Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Analizador simple

La simple analizador divide el texto en términos buscables (tokens) siempre que encuentra un carácter que no es una letra, como espacio en blanco, puntuación o uno o más dígitos. Convierte todo el texto en minúsculas.

Si selecciona Refine Your Index, la interfaz de usuario de Atlas muestra una sección titulada View text analysis of your selected index configuration dentro de la sección Index Configurations. Si amplías esta sección, la Interfaz de Usuario de Atlas muestra los tokens de índice y búsqueda que el analizador simple genera para cada string de muestra. Puedes ver los tokens que el analizador simple crea para un documento de muestra incorporado y un string del query cuando creas o editas un índice en la Interfaz de Usuario de Atlas Visual Editor.

Importante

MongoDB Search no indexará los campos de string en los que los tokens del analizador superen los 32766 bytes de tamaño. Si se utiliza el analizador de keywords, los campos de string que superen los 32766 bytes no se indexarán.

La siguiente definición de índice de ejemplo especifica un índice en el campo title en la Colección sample_mflix.movies usando el analizador simple. Para seguir este ejemplo, carga los datos de muestra en tu clúster y usa mongosh o navegue a la página Create a Search Index en la Interfaz de Usuario de Atlas siguiendo los pasos del tutorial Crear un índice de MongoDB Search.

Luego, utilizando la movies colección como fuente de datos, siga el procedimiento de ejemplo para crear un índice desde mongosh o la interfaz de usuario de Atlas Visual Editor o.JSON editor


➤ Utilice el menú desplegable Seleccione su idioma para configurar la interfaz para el ejemplo de esta página.


  1. Se debe hacer clic en Refine Your Index para configurar el índice.

  2. En la sección Index Configurations, cambie Dynamic Mapping a off.

  3. En la sección Field Mappings, haz clic en Add Field para abrir la ventana Add Field Mapping.

  4. Haga clic en Customized Configuration.

  5. Selecciona title del menú desplegable Field Name.

  6. Haz clic en el menú desplegable Data Type y selecciona String si aún no está seleccionado.

  7. Expande String Properties y realiza los siguientes cambios:

    Analizador de índices

    Selecciona lucene.simple del menú desplegable.

    Analizador de búsqueda

    Selecciona lucene.simple del menú desplegable.

    Opciones del índice

    Usa el offsets por defecto.

    Store

    Usa el true por defecto.

    Ignorar lo anterior

    Mantén la configuración por defecto.

    Normas

    Usa el include por defecto.

  8. Haga clic en Add.

  9. Haga clic en Save Changes.

  10. Haga clic en Create Search Index.

  1. Reemplazar la definición predeterminada del índice con la siguiente definición del índice.

    {
    "mappings": {
    "fields": {
    "title": {
    "type": "string",
    "analyzer": "lucene.simple"
    }
    }
    }
    }
  2. Haga clic en Next.

  3. Haga clic en Create Search Index.

1db.movies.createSearchIndex(
2 "default",
3 {
4 "mappings": {
5 "fields": {
6 "title": {
7 "type": "string",
8 "analyzer": "lucene.simple"
9 }
10 }
11 }
12 }
13)

La siguiente query busca el término lion en el campo title y limita la salida a cinco resultados.

  1. Haz clic en el botón Query para tu índice.

  2. Haz clic en Edit Query para editar la consulta.

  3. Haz clic en la barra de query y selecciona la base de datos y la colección.

  4. Reemplaza la consulta por defecto por la siguiente y haz clic en 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
1db.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' },
]

MongoDB Search devuelve estos documentos haciendo lo siguiente para el texto en el campo title usando el analizador lucene.simple:

  • Convierte el texto a minúsculas.

  • Cree tokens separados dividiendo el texto dondequiera que haya un carácter que no sea una letra.

La siguiente tabla muestra los tokens que MongoDB Search crea utilizando el Analizador simple y, por el contrario, el Analizador estándar y el Analizador de espacios en blanco para los documentos en los resultados:

Título
Tokens del Analizador Simple
Tokens de analizador estándar
Tokens del analizador de espacios en blanco

White Lion

white, lion

white, lion

White, Lion

The Lion King

the, lion, king

the, lion, king

The, Lion, King

The Lion King 1 1/2

the, lion, king

the, lion, king, 1, 1, 2

The, Lion, King, 1, 1/2

Lion's Den

lion, s, den

lion's, den

Lion's, Den

MongoDB Search devuelve el documento Lion's Den en los resultados porque el analizador simple crea un token separado para lion, que coincide con el término de query lion. Por el contrario, si se indexa el campo utilizando el Analizador estándar o el Analizador de espacios en blanco, MongoDB Search devolvería algunos de los documentos en los resultados de la query, pero no Lion's Den porque estos analizadores crearían los tokens lion's y Lion's respectivamente, pero no crean un token para lion.

Volver

Estándar