Search code examples
elasticsearchelasticsearch-aggregation

How to make aggregations work for text fields


I am trying to write a elasticsearch query to get unique locality towns. my locality_town_keyword is of keyword type. when I try to search into locality_town_keyword, I get search hits but nothing in "aggregations":"Buckets".

Following is how my schema looks like...

                "locality_town": {
                    "type": "text"
                },
                "locality_town_keyword": {
                    "type": "keyword"
                },

My Search query looks like following

{ 
    "query": 
    {
        "prefix" : { "locality_town" : "m" }
    },
    "size": "1",
    "_source": {
        "includes": [
            "locality_town*"
        ]
    },
    "aggs": {
        "loc": {
            "terms": {
                "field": "locality_town_keyoword",
                "size": 5,
                "order": {
                    "_count": "desc"
                }
            }
        }
    }
}

Here is the output it gives

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 799,
    "max_score": 1.0,
    "hits": [
      {
        "_index": "tenderindex_2",
        "_type": "tender_2",
        "_id": "290077",
        "_score": 1.0,
        "_source": {
          "locality_town": "Manchester",
          "locality_town_keyword": "Manchester"
        }
      }
    ]
  },
  "aggregations": {
    "loc": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    }
  }
}

This is how one document looks like

{
    "_index": "tenderindex_2",
    "_type": "tender_2",
    "_id": "290077",
    "_version": 1,
    "_seq_no": 39,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "Legal Services",
        "buyers": "CENTRAL MANCHESTER UNIVERSITY HOSPITALS NHS FOUNDATION TRUST",
        "postal_code": "M13 0JR",
        "publish_date": "2015-03-03T15:48:45Z",
        "status": "cancelled",
        "start_date": "2017-03-03T00:00:00Z",
        "endt_date": "2020-03-03T00:00:00Z",
        "url": "https://www.temp.com",
        "country": "England",
        "description": "desc......",
        "language": "en-GB",
        "service": "OPEN_CONTRACTING",
        "value": "0",
        "value_currency": "GBP",
        "winner": "",
        "create_time": "2019-05-11T21:39:42Z",
        "deadline_date": "1970-01-01T00:00:00Z",
        "address": "Central Manchester University Hospitals NHS Foundation Trust Wilmslow Park",
        "locality_town": "Manchester",
        "locality_town_keyword": "Manchester",
        "region": "North West",
        "tender_type": "planning",
        "cpv": "Health services ",
        "strpublish_date": "2015-03-03T15:48:45Z",
        "strstart_date": "2017-03-03T00:00:00Z",
        "strend_date": "2020-03-03T00:00:00Z",
        "strdeadline_date": "",
        "winner_email": "",
        "winner_address": "",
        "winner_town": "",
        "winner_postalcode": "",
        "winner_phone": "",
        "cpvs": "[\"Health services (85100000-0)\"]"
    }
}

Solution

  • Looks like you have a typo in your aggregation query:

    "aggs": {
        "loc": {
            "terms": {
                "field": "locality_town_keyoword", <== here
                "size": 5,
    

    Try with locality_town_keyword instead!

    Hope this helps!