mike
mike

Reputation: 639

How to express optional matching using Query String Query

I have a query as below using the query string dsl which I'm expecting to return results that match all the three assertions. (status = active, price = 100 , language = en).

How can I make the language param optional, just score lower if is not matching instead to not match at all ?

(status:"active") AND (price:100) AND (language:"en")

Upvotes: 0

Views: 41

Answers (1)

Val
Val

Reputation: 217254

You can try this

(status:active AND language:en) OR (price:100 AND language:en)

Or a shorter version like this:

+status:active +price:100 language:en

An equivalent query rewritten using bool and match queries is this one:

{
  "bool": {
    "must": [
      {
        "match": {
          "status": "active"
        }
      },
      {
        "match": {
          "price": 100
        }
      }
    ],
    "should": {
      "match": {
        "language": "en"
      }
    }
  }
}

Upvotes: 1

Related Questions