user548936
user548936

Reputation: 113

Give document fields different priority in search

I have the following query:

'{"query": {"query_string": {"query": "ABC"}'

this will search all fields in given index\type ex. Mytype has field1, field2, field3 this will match "ABC" to field1, field2, field3

Equal value is placed on the 3 fields I need to prioritize the field ranking so let's say the priority should be field2, field3, field1

How do I modify my search to set the field ranking/priority?

In my research I saw _score but that looks like its used to prioritize each doc instead of the fields.

Upvotes: 9

Views: 16420

Answers (2)

Ewoks
Ewoks

Reputation: 12435

Here is update for newer Elasticsearch versions. Query like this should boost results depending on respective fields content:

GET /_search
{ 
  "query":{
    "simple_query_string" : {
      "query": "ABC",
      "fields": [
        "field2^3",
        "field3^2",
        "field1^1"]
    }
  }  
}

Pay attention to "^3" after field2, this will boost results, so if "ABC" is found in all fields, they will be sorted as:

  1. doc with field2 containing "ABC"
  2. doc with field3 containing "ABC"
  3. doc with field1 containing "ABC"

Upvotes: 8

Eric Hogberg
Eric Hogberg

Reputation: 99

Use a boost query and weight the fields in accordance with the prioritization you desire: https://www.elastic.co/guide/en/elasticsearch/guide/current/_boosting_query_clauses.html

Upvotes: 4

Related Questions