Increase if the result starts with the word

I use Elasticsearch for autocomplete search using ngram filter. I need to increase the result if it starts with a search keyword.

My request is simple:

"query": {
   "match": {
      "query": "re",
      "operator": "and"
   }
}

And these are my results:

  • Re staurants
  • Couture et re touches
  • Re stauration rapide

But I want them to be like this:

  • Re staurants
  • Re stauration rapide
  • Couture et re touches

How can I increase a result starting with a keyword?

In case this can help, here is my mapping:

{
    "settings": {
        "analysis": {
            "analyzer": {
                "partialAnalyzer": {
                    "type": "custom",
                    "tokenizer": "ngram_tokenizer",
                    "filter": ["asciifolding", "lowercase"]
                },
                "searchAnalyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": ["asciifolding", "lowercase"]
                }
            },

            "tokenizer": {
                "ngram_tokenizer": {
                    "type": "edge_ngram",
                    "min_gram": "1",
                    "max_gram": "15",
                    "token_chars": [ "letter", "digit" ]
                }
            }
        }
    },

    "mappings": {
        "place": {
            "properties": {
                "name": {
                    "type": "string",
                    "index_analyzer": "partialAnalyzer",
                    "search_analyzer": "searchAnalyzer",
                    "term_vector": "with_positions_offsets"
                }
            }
        }
    }
}

Hello,

+4
source share
1 answer

What about this idea, and not 100% sure that it depends on the data that I think:

  • name, keyword ( )
  • bool should s
  • should - ,
  • should match phrase_prefix .

:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "partialAnalyzer": {
          "type": "custom",
          "tokenizer": "ngram_tokenizer",
          "filter": [
            "asciifolding",
            "lowercase"
          ]
        },
        "searchAnalyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "asciifolding",
            "lowercase"
          ]
        },
        "keyword_lowercase": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "asciifolding",
            "lowercase"
          ]
        }
      },
      "tokenizer": {
        "ngram_tokenizer": {
          "type": "edge_ngram",
          "min_gram": "1",
          "max_gram": "15",
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "place": {
      "properties": {
        "name": {
          "type": "string",
          "index_analyzer": "partialAnalyzer",
          "search_analyzer": "searchAnalyzer",
          "term_vector": "with_positions_offsets",
          "fields": {
            "as_is": {
              "type": "string",
              "analyzer": "keyword_lowercase"
            }
          }
        }
      }
    }
  }
}

:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": {
              "query": "re",
              "operator": "and"
            }
          }
        },
        {
          "match": {
            "name.as_is": {
              "query": "re",
              "type": "phrase_prefix"
            }
          }
        }
      ]
    }
  }
}
+6

Source: https://habr.com/ru/post/1616141/


All Articles