Luv33preet
Luv33preet

Reputation: 1867

filtering on 2 values of same field

I have a status field, which can have one of the following values,

enter image description here I can filter for data which have status completed. I can also see data which has ongoing.

But I want to display the data which have status completed and ongoing at the same time.

But I don't know how to add filters for 2 values on a single field.

How can I achieve what I want ?

EDIT - Thanks for answers. But that is not what i wanted.

enter image description here

Like here I have filtered for status:completed, I want to filter for 2 values in this exact way.

I know I can edit this filter and , and use your queries, But I need a simple way to do this(query way is complex), as I have to show it to my marketing team and they don't have any idea about queries. I need to convince them.

Upvotes: 0

Views: 128

Answers (3)

Praneeth
Praneeth

Reputation: 761

You need a filter in aggregation.

{
   "size": 0,
   "aggs": {
      "agg_name": {
         "filter": {
            "bool": {
               "should": [
                  {
                     "terms": {
                        "status": [
                           "completed",
                           "ongoing"
                        ]
                     }
                  }
               ]
            }
         }
      }
   }
}

Use the above query to get results like this:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 8,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "agg_name": {
         "doc_count": 6
      }
   }
}

The result what you want is the doc_count

Upvotes: 1

Karthik Saravanan
Karthik Saravanan

Reputation: 158

For your reference bool query in elasticsearch, should it's like OR conditions,

{
   "query":{
       "bool":{
           "should":[
               {"must":{"status":"completed"}},
               {"must":{"status":"ongoing"}}
           ]
      }
   },
    "aggs" : {
       "booking_status" : {
            "terms" : {
              "field" : "status"
           }
     }
 }
}

Upvotes: 0

gil.fernandes
gil.fernandes

Reputation: 14631

If I understand your question correctly, you want to perform an aggregation on 2 values of a field.

This should be possible with a query similar to this one with a terms query:

{
  "size" : 0,
  "query" : {
    "bool" : {
      "must" : [ {
        "terms" : {
          "status" : [ "completed", "unpaid" ]
        }
      } ]
    }
  },
  "aggs" : {
    "freqs" : {
      "terms" : {
        "field" : "status"
      }
    }
  }
}

This will give a result like this one:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "freqs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "unpaid",
        "doc_count" : 4
      }, {
        "key" : "completed",
        "doc_count" : 1
      } ]
    }
  }
}

Here is my toy mapping definition:

{
  "bookings" : {
    "properties" : {
      "status" : {
        "type" : "keyword"
      }
    }
  }
}

Upvotes: 1

Related Questions