如何在弹性搜索中使用脚本字段时返回所有字段

时间:2016-04-26 08:13:19

标签: elasticsearch

使用弹性搜索我尝试将计算出的distance字段添加到地理位置搜索中。我只想在搜索文档中附加一个额外的计算字段,但是当我通过" script_fields"添加计算字段时,只返回该字段。

我尝试添加通配符字段部分,但它并没有影响结果。

如何使此查询返回complete documents并添加了额外的计算字段?

GET /ocsm_test/inventory/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "2km",
          "address.geo.location": [],
          "loc.address.geo.location": [
            151.2165507,
            -33.8732887
          ]
        }
      }
    }
  },
  "aggs": {
    "partNumber": {
      "terms": {
        "field": "partNumber",
        "order": {
          "_term": "asc"
        }
      }
    },
    "location": {
      "terms": {
        "field": "loc.identifier",
        "order": {
          "_term": "asc"
        }
      }
    }
  },
  "script_fields": {
    "distance": {
      "params": {
        "lat": -33.8732887,
        "lon": 151.2165507
      },
      "script": "doc['loc.address.geo.location'].distanceInKm(lat,lon)"
    }
  },
  "fields": [
    ".*"
  ],
  "post_filter": {
    "bool": {
      "must": [
        {
          "term": {
            "partNumber": "p-0099393-3"
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:4)

建议不要

Retrieving fields,而应使用source filtering代替。

所以,而不是这个

"fields": [
  ".*"
],

使用此:

"_source": true