Search code examples
elasticsearchelastic-stackelasticsearch-5

Create the Elastic search query to show Random 5 Questions by category


I Have fields Category & Questions in the Table.

My Requirement is for the below mentioned 3 category against I need the questions which is tagged (SO I want the Category and Questions field in the query) by writing elastic search query

Category :

OLA

BNA

DRG

GET logstash-sdc-feedback/_search? { "_source":["Category.keyword"], "size": 5, "query":{ "bool": { "must": [ {"match":{"Category.keyword"": "OLA","BNA","DRG"}}

],

} }, "aggs": { "MyBuckets": { "terms": { "field": "questions.keyword","Category.keyword" "order":{ "_count": "asc" }, "size": "5"

} } } }


Solution

  • You can use terms query along with terms aggregation, to achieve your use case.

    Adding a working example

    Index Data:

    {
      "category": "XYZ",
      "question": "d"
    }
    {
      "category": "OLA",
      "question": "a"
    }
    {
      "category": "BNA",
      "question": "b"
    }
    {
      "category": "DRG",
      "question": "c"
    }
    

    Search Query:

    {
      "query": {
        "bool": {
          "must": {
            "terms": {
              "category.keyword": [
                "OLA",
                "BNA",
                "DRG"
              ]
            }
          }
        }
      },
      "aggs": {
        "top_tags": {
          "terms": {
            "field": "category.keyword"
          },
          "aggs": {
            "top_faq_hits": {
              "top_hits": {
                "_source": {
                  "includes": [
                    "question"
                  ]
                },
                "size": 1
              }
            }
          }
        }
      }
    }
    

    Search Result:

    "aggregations": {
        "top_tags": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "BNA",                 // note this
              "doc_count": 1,
              "top_faq_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "65566020",
                      "_type": "_doc",
                      "_id": "2",
                      "_score": 1.0,
                      "_source": {
                        "question": "b"            // note this
                      }
                    }
                  ]
                }
              }
            },
            {
              "key": "DRG",
              "doc_count": 1,
              "top_faq_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "65566020",
                      "_type": "_doc",
                      "_id": "3",
                      "_score": 1.0,
                      "_source": {
                        "question": "c"
                      }
                    }
                  ]
                }
              }
            },
            {
              "key": "OLA",
              "doc_count": 1,
              "top_faq_hits": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "65566020",
                      "_type": "_doc",
                      "_id": "1",
                      "_score": 1.0,
                      "_source": {
                        "question": "a"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }