如何使聚合适用于文本字段

时间:2019-07-24 08:30:44

标签: elasticsearch elasticsearch-aggregation

我正在尝试编写Elasticsearch查询以获取独特的地区城镇。我的locality_town_keyword是关键字类型。当我尝试搜索locality_town_keyword时,我得到搜索命中,但在“聚合”:“桶”中什么也没有。

以下是我的架构的样子...

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

我的搜索查询如下

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

这是它给出的输出

{
  "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": []
    }
  }
}

这是一个文档的样子

{
    "_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)\"]"
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您的聚合查询中有错字:

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

改为尝试使用locality_town_keyword

希望这会有所帮助!