如何获取 top_hits 聚合中的字段

How to get fields in top_hits aggregation

我有 elasticsearch 集群 - 版本 1.3.0。该集群的索引文档没有启用 _source,因此在检索命中时,我通常根据请求中的 "fields" 参数获取。

现在我正在为重复分组功能实施 top_hits 聚合。我想获取 top_hits 结果中的字段,但我现在不能这样做,因为 _source 在我的映射中默认未启用。您能否建议我 option/work 在不更改现有映射的情况下实现此目的?

我没有在 top-hits aggregation doc 中找到它。非常感谢任何帮助。

谢谢!

使用script fields:

  "aggs": {
    "sample": {
      "top_hits": {
        "size": 1,
        "script_fields": {
          "field1": {
            "script": "doc['field1']"
          },
          "field2": {
            "script": "doc['field2']"
          }
          ...
        }
      }
    }
  }

但是,如果 field1field2 被分析,你需要一个子字段来保持 not_analyzed 的版本场。为什么?因为,如果以任何方式分析普通字段,doc['field'] 调用将 return 分析的术语,而不是索引的初始内容。

像这样:

  "mappings": {
    "test": {
      "_source": {
        "enabled": false
      }, 
      "properties": {
        "field1": {
          "type": "string",
          "fields": {
            "notAnalyzed": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }

以及查询:

  "aggs": {
    "sample": {
      "top_hits": {
        "size": 1,
        "script_fields": {
          "field1": {
            "script": "doc['field1.notAnalyzed']"
          }
        }
      }
    }
  }