Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 669
user548936 Avatar asked Aug 05 '15 15:08

user548936


1 Answers

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"
like image 184
Ewoks Avatar answered Oct 14 '22 20:10

Ewoks