1
votes

In ElasticSearch how do i sort documents based on finding a phrase in the following order of fields.

Search Phrase: Miami Fields: Title, Content, Topics

If found in Title, Content and in Topics it will show before other documents that the phrase is only found in Content.

Maybe there is a way to say: if phrase found in Title then weight 2 if phrase found in Content then weight 1.5 if phrase found in Topics then weight 1

and this will be sum(weight) with _score

My Current query looks like

{
  "index": "abc",
  "type": "mydocuments",
  "body": {
    "query": {
      "multi_match": {
        "query": "miami",
        "type": "phrase",
        "fields": [
          "title",
          "content",
          "topics",
          "destinations"
        ]
      }
    }
  }
}
1

1 Answers

2
votes

You can use boosting on fields with the caret ^ notation to score them higher than other matching fields

{
  "index": "abc",
  "type": "mydocuments",
  "body": {
    "query": {
      "multi_match": {
        "query": "miami",
        "type": "phrase",
        "fields": [
          "title^10",
          "content^3",
          "topics",
          "destinations"
        ]
      }
    }
  }
}

Here I have applied a weight of 10 to title and weight of 3 to content. Documents will be returned in decreasing _score order so you need to boost scores in fields that you consider more important; the values to use for boosting are up to you and may require a little trial and improvement to return documents in your preferred order.