0

Is there a way in ElasticSearch to run a boolean filter, then without refinding the search further, sort/order the results based on a multi_field query?

Eg: Get all items with status_id = 1 (the filter), then order those documents by using the keywords "red car" (documents whose name and description contain those keywords are first, documents without are last).

1 Answer 1

1

You can use bool query - As per condition of should -

The clause (query) should appear in the matching document. In a boolean query with no must clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.

In our case , as there is a must and its a number match , score value wont be computed. But then conditions in should would be used for computing the score alone -

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "status_id": 1
          }
        }
      ],
      "should": [
        {
          "multi_match": {
            "query": "red car",
            "fields": [
              "subject",
              "message"
            ]
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was actually doing this, minus one thing, I was doing the 'must' as part of a filter, not the query. As a result, the should was by itself, and was reducing the result set.

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.