【问题标题】:how to return all fields when using scripted fields in elastic search在弹性搜索中使用脚本字段时如何返回所有字段
【发布时间】:2016-04-26 19:59:00
【问题描述】:

使用弹性搜索我正在尝试将计算的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"
          }
        }
      ]
    }
  }
}

【问题讨论】:

  • 尝试使用"_source": true 而不是"fields"
  • 成功了,谢谢 Val.

标签: elasticsearch


【解决方案1】:

不推荐Retrieving fields,您应该使用source filtering

所以,而不是这个

"fields": [
  ".*"
],

使用这个:

"_source": true

【讨论】:

  • 谢谢 Val,我没有意识到有源过滤可用。实际上,我会利用它,不是为了这个特定的东西,而是为了其他一些东西。
猜你喜欢
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多