0

I want to implement a simple search query using Elasticsearch.

I have two fields, "title" and "description" that I would like to match the searched term with. Currently, I have the body shown below as the body for search body. How can I make it so that the search prioritizes the title match, but if there are matches in the description, they are still included in the search (with lower priority)? Thanks in advance.

body = {
   size: 200,
   from: 0,
   query: {
      prefix: {
        title: searchTerm
      }
   }
}

2 Answers 2

1

You have to use a constant score query with a score of 0 for the "other" field. Any other boost / function score usage will not reliably score a certain field over another field as the scoring is based on other parameters like text length for example, this means a constant boost (unless very very large) can not guarantee the behaviour you seek.

By using a constant score for each field you can control score manually, like so:

{
    size: 200,
    from: 0,
    query: {
        bool: {
            should: [
                {
                    prefix: {
                        title: searchTerm
                    }
                },
                {
                    constant_score: {
                        filter: {
                            prefix: {
                                description: searchTerm
                            }
                        },
                        boost: 0
                    }
                },
            ]
        }
    }
}

If you set description boost to be more than 0 then the score will be the combined score of both fields, by doing this you can prioritize documents that have that prefix in both fields over ones that have it in just the title field.

Sign up to request clarification or add additional context in comments.

1 Comment

regarding a simple boost usage for example a description=prefix could be prioritized over title= ... alot of text ... prefix ... alot more text. I recommend reading about how elastic scores documents, if you can assume the factors that affect scoring are somewhat negligible between the fields (i.e certain length restrictions, etc) then you could use a simple boost method with some sort of threshold as the boost reliably.
1

You can use a combination of bool/should clause along with the boost parameter

{
  "query": {
    "bool": {
      "should": [
        {
          "prefix": {
            "title": {
              "value": "searchterm"
            }
          }
        },
        {
          "prefix": {
            "description": {
              "value": "searchterm",
              "boost": 4
            }
          }
        }
      ]
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.