Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return all fields when using scripted fields in elastic search

Using elastic search I'm trying to add a calculated distance field to a geo search. I just want to append an extra calculated field to the search document but when I add the calculated field via "script_fields", then only that field is returned.

I tried adding a wildcard fields part, but it didn't affect the result.

How to make this query return the complete documents with the extra calculated field added?

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"
          }
        }
      ]
    }
  }
}
like image 500
Richard G Avatar asked Apr 26 '16 08:04

Richard G


1 Answers

Retrieving fields is not recommended, you should use source filtering instead.

So, instead of this

"fields": [
  ".*"
],

Use this:

"_source": true
like image 138
Val Avatar answered Nov 18 '22 19:11

Val