User2112512
User2112512

Reputation: 13

Adding two filters with one query in elasticsearch

I am trying to combine a range filter query, a bool-must-match query, and a constant score (terms) filter query all in one query. I have both the range filter query and the bool-must-match query combine so far:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "market": "Android"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "sold": {
              "gte": "20170601",
              "lte": "20170602"
            }
          }
        }
      ]
    }
  }
}

How do I add this query in there:

{
    "query" : {
        "constant_score" : {
            "filter" : {
                "terms" : { 
                    "price" : [200, 300]
                }
            }
        }
    }
}

Is there a way to combine all three together?

Upvotes: 0

Views: 97

Answers (1)

Val
Val

Reputation: 217254

You can simply add it in the bool/filter section as filters have a constant score:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "market": "Android"
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "price": [
              200,
              300
            ]
          }
        },
        {
          "range": {
            "sold": {
              "gte": "20170601",
              "lte": "20170602"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 0

Related Questions