MeJ
MeJ

Reputation: 1108

ElasticSearch Filtered query

I created an elastic search index and the result of a simple search looks like:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 11,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop-bestellung",
        "_type": "bestellung",
        "_id": "dc144b04-8e73-4ea5-9f73-95c01768fd26",
        "_score": 1,
        "_source": {
          "id": "dc144b04-8e73-4ea5-9f73-95c01768fd26",
          "bestellnummer": "B-20170302-026",
          "shopid": "0143d767-8986-432a-a15d-00e1c4862b24",
          "shopname": "DeeDa",
          "erstelltVon": "5663bb4b-fc44-46ca-b875-a3487b588b24",
          "bestellername": "Max Mann",
          "bestelldatum": "2017-01-30T23:00:00Z",
          "bestellpositionen": []
        }
      }
    ]
  }
}

I tried to create a filter which should consits of following three restrictions:

My filter only consits of query text and date range:

{  
   "query":{  
      "query_string":{  
         "fields":[  
            "bestellnummer",
            "bestellername",
            "bestelldatum",
            "erstelltVon",
            "bestellpositionen.artikelname",
            "bestellpositionen.artikelnummer",
            "bestellpositionen.referenznummer"
         ],
         "query":"*"
      }
   },
    "filter": {
        "range" : {
            "bestelldatum" : {
                "gte": "2017-02-04T23:00:00Z",
                "lte": "now",
                "time_zone": "+01:00"
            }
        }
    }
}

I would like to add the third filter:

"erstelltVon": "5663bb4b-fc44-46ca-b875-a3487b588b24"

How can I do that?

Upvotes: 1

Views: 338

Answers (1)

Paulin Trognon
Paulin Trognon

Reputation: 813

You need to use a boolean filter.

Here is how to use it:

  "filter": {
    "bool" : {
      "must": [
        // FIRST FILTER
        {
          "range" : {
            "bestelldatum" : {
              "gte": "2017-02-04T23:00:00Z",
              "lte": "now",
              "time_zone": "+01:00"
            }
          }
        },
        {
          // YOUR OTHER FILTER HERE
        }
      ]
    }

change "must" to "should" if you want to use a OR instead of an AND.

Upvotes: 3

Related Questions