1

I have indexed some documents in ElasticSearch. Short view looks like this:

{
  "tenant_id":"abcd1234"
  "provider_id":"3456"
   .
   .
   .
   "doctor_summary": ["line1", "line2", "line3"] OR it could be null.
}

I want to calculate this list as 1 if not null,

     query_template = {

        "bool":{
            "must":[
                {"term":{"tenant_id.keyword":tenant_id}},
                {"term":{"provider_id.keyword":provider_id}},
                {"range":{"visit_date":{"gte":from_date,"lte":to_date}}},
                
            ],
            "filter":[]
        
        }
    }



aggs_query = {
   "doctor_summary_count":{
            "value_count":{"field":"doctor_summary.keyword"}
        }
}    

res = CLIENT.search(index = config['elasticsearch']['abcd'],
                        query = query_template,
                        size = 10000,
                        aggs = aggs_query)

After calling this aggregation query, it gives result as ( size of the list * total doctor_summary field).

For example: the result of above query on above document should be 1. (As it is not null). But it gives as 3(because list contains 3 lines.)

1 Answer 1

2

You can use exist query in aggregation with filter aggregation.

Query:

{
  "aggs": {
    "count": {
      "filter": {
       "exists": {
         "field": "doctor_summary.keyword"
       }
      }
    }
  }
}

Response:

"aggregations" : {
    "count" : {
      "doc_count" : 1
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

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.