Give document fields different priority in search

12,256

Solution 1

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"

Solution 2

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

Share:
12,256
user548936
Author by

user548936

Updated on August 05, 2022

Comments

  • user548936
    user548936 over 1 year

    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.

    • user548936
      user548936 over 8 years
      better ex. using the field priority from above, lets say doc1 has a match with field3 and doc2 has a match with field2. even though doc1 and doc2 match, i need doc2 to have a higher score than doc1
  • N.K
    N.K about 6 years
    can we use the same with Nest ?
  • Andriy Lesyuk
    Andriy Lesyuk almost 2 years
    Which version Elasticsearch supports that?
  • Ewoks
    Ewoks almost 2 years
    sorry, 3y later, I don't remember it :(