Home > OS >  Search match fulltext find with uncomplete word but not found with complete word in Elasticsearch
Search match fulltext find with uncomplete word but not found with complete word in Elasticsearch

Time:11-23

I have a really weird problem in my elasticsearch query. I made an autocomplete search in my website and I have a problem.

For example, there is a neighborhood in my country called "Recreio dos Bandeirantes" When I search for "bandeirant" (while user are typing) the query find the neighborhood, but, when finish the type "bandeirantes" cannot find the same neighborhood.

This is my query

      {
        query: {
          bool: {
            must: [
              {
                match: {
                  'city.name': city,
                },
              },
              {
                match: {
                  'city.state': state,
                },
              },
              {
                match: {
                  keyword: {
                    query, // The query is 'bandeirant' or 'bandeirantes'
                  },
                },
              },
            ],
          },
        },
        highlight: {
          fields: {
            keyword: {
              number_of_fragments: 9,
            },
          },
        },
        size: 20,
      }

The final neighborhood value is 'Recreio dos Bandeirantes, Rio de Janeiro, RJ'

The mapping for this field is this:

{
  "search-neighborhood-01": {
    "mappings": {
      "properties": {
        "city": {
          //.....
        },
        "keyword": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        },
        "name": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        }
      }
    }
  }
}

My settings with analyzer

{
  "search-neighborhood-01": {
    "settings": {
      "index": {
        // .......
        "analysis": {
          "filter": {
            "autocomplete_filter": {
              "token_chars": [
                "letter"
              ],
              "min_gram": "1",
              "type": "edge_ngram",
              "max_gram": "10"
            }
          },
          "analyzer": {
            "autocomplete": {
              "filter": [
                "lowercase",
                "autocomplete_filter",
                "asciifolding"
              ],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        },
        // .....
      }
    }
  }
}

My response with bandeirant

// .....
      {
       //.....
        "_source": {
          "city": {
            "name": "Rio de Janeiro",
            "state": "RJ",
            "keyword": "Rio de Janeiro, RJ"
          },
          "name": "Recreio dos Bandeirantes",
          "keyword": "Recreio dos Bandeirantes, Rio de Janeiro, RJ"
        },
        "highlight": {
          "keyword": [
            "Recreio dos <em>Bandeirantes</em>, Rio de Janeiro, RJ"
          ]
        }
      }

My response with bandeirantes is empty :/

How can I do to solve this?

Thanks o/

CodePudding user response:

You have this issue because you Ngram filter token have"max_gram": "10" configuration that it means words longer than 10 won’t be indexed.

My recommendation would be to increase this amount along with “min_gram” config.

CodePudding user response:

I changed my max_ngram to 20 and worked :)

  • Related