0
votes

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"

} } } }

1
your question is not quite clear, can you please explain your use case ? - ESCoder
My Requirement is : I want to show 2 fields (Category & Questions) . In this Category I have to select only this Codes ("OLA","BNA","DRG") for each category codes i need to show the questions as well. How to write the query - Prabhudas8703

1 Answers

1
votes

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"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }