Score with ngram filter


(Francisco Fernández Arrabal) #1

Hi,

I have a problem with the score in a search.

I have two elements, for example: "London Barking" and "Derry / Londonderry".

When I execute this query:

"query": {
        "match": {
          "name": {
            "query": "london"
          }
        }
  }

I get the same score for "London Barking" and "Derry / Londonderry", but I want a different score, higher for "London Barking" because has an exact word and "Derry / Londonderry" hasn't.

For analysis I use ngram_filter. This is my complete mapping:

{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "analysis": {
      "filter": {
        "ngram_filter": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 100
        }
      },
      "analyzer": {
        "ngram_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "ngram_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "hotel": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "ngram_analyzer",
          "search_analyzer": "standard"
        }
      }
    }
  }
}

Can I give more score to elements with an exact word?

Thanks


#2

What you could do, is to add another version of the field name :
The name field will be tokenized with the standard analyzer, and the name.analyzed field will be tokenized with the ngram analyzer.

{
  "mappings": {
    "hotel": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "analyzed": {
              "type": "text",
              "analyzer": "ngram_analyzer",
              "search_analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

Then you will query on both fields, and apply a boost on the default version of field name :

{
  "query": {
    "multi_match": {
      "fields": [
        "name^2",
        "name.analyzed"
      ],
      "query": "london"
    }
  }
}