在使用脚本字段进行Elasticsearch搜索时如何返回所有字段

11

使用Elasticsearch,我正试图向地理搜索添加一个计算的distance字段。我只想在搜索文档中追加一个额外的计算字段,但是当我通过“script_fields”添加计算字段时,只返回该字段。

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

如何使此查询返回添加了额外计算字段的完整文档

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
尝试使用"_source": true而不是"fields" - Val
那个有效,谢谢 Val。 - Richard G
1个回答

13

检索字段不被推荐使用,应该使用源过滤

因此,不要这样做:

"fields": [
  ".*"
],

使用这个:

"_source": true

谢谢Val,我没意识到有源代码过滤可用。实际上我会利用它,不是为了这个特定的事情,而是其他一些事情。 - Richard G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接