如何在ElasticSearch中的脚本中访问文档ID?

时间:2015-09-03 08:50:56

标签: elasticsearch aggregation

我正在尝试编写一个聚合文档类型的查询。问题是,文档类型可以从文档id获得 - 它是id的4-5个第一个字符,如BPR :: fawL5CcPE72Wf3m93hUg2。
这是我放在一起的查询:

{
    "query": {
        "match_all": { }
    },
    "aggregations": {
        "by_document_type": {
            "script": {
                "script": "doc['_id'].value.substring(0, 3)",
                "order": { 
                    "_count": "desc" 
                }
            }
        }
    }
}

但它说Parse Failure [Could not find aggregator type [script] in [by_document_type]]];

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您只需使用terms aggregation并在其中提供您的脚本:

{
  "query": {
    "match_all": {}
  },
  "aggregations": {
    "by_document_type": {
      "terms": {
        "script": "doc['_id'].value.substring(0, 3)",
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
相关问题