一尘不染

在elasticsearch中使用脚本化字段时如何返回所有字段

elasticsearch

使用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"
          }
        }
      ]
    }
  }
}

阅读 420

收藏
2020-06-22

共1个答案

一尘不染

不建议检索字段,而应使用源过滤

所以,代替这个

"fields": [
  ".*"
],

用这个:

"_source": true
2020-06-22