0

I want to run the any query/filter based on the field exists. In our case if user answers a particular field then only we will store that value, other wise will not store that field it self. How can I run the query?

Below is my mapping:

"mappings": {
     "responses_10_57": {
        "properties": {
           "rid: {
              "type": "long"
           },
           "end_time": {
              "type": "date",
              "format": "dateOptionalTime"
           },
           "start_time": {
              "type": "date",
              "format": "dateOptionalTime"
           },
       "qid_1": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "analyzer": "str_params"
                 }
              }
           },
    "qid_2": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "analyzer": "str_params"
                 }
              }
           },
    "qid_3": {
              "properties": {
                 "msg_text": {
                    "type": "string"
                 },
         "msg_tags": {
                    "type": "string",
                    "fields": {
                       "raw": {
                          "type": "string",
                          "analyzer": "str_params"
                       }
                    }
                 }
              }
           }
   }
  }

}

qid_1 is the name field, qid_2 is the category field, qid_3 is the text message field.

But the qid_3 is not a mandatory field. So we will not insert the record if user doesn't entered any text message.

1) I want each category wide count those who responded the third question.

2) I have to search the names who answered the third question.

How can I write these two queries?

Sudhakar Reddy
  • 153
  • 1
  • 11

1 Answers1

0

Both queries should have an exists filter to limit the response to only those documents where the qid_3 exists (is not null). For your first query you could try a terms aggregation. For your second query, you can filter the source to include only the names in the response or store the field and use fields.

1)

{
    "size": 0,
    "filter" : {
        "exists" : { "field" : "quid_3" }
    },
    "aggs" : {
      "group_by_category" : {
        "terms" : { "field" : "qid_2" }
      }
    }
}

2)

{
  "filter" : {
    "exists" : { "field" : "quid_3" }
  },
  "_source": [ "qid_1"]
}
Community
  • 1
  • 1