11

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"
          }
        }
      ]
    }
  }
}
2
  • 1
    Try using "_source": true instead of "fields" Commented Apr 26, 2016 at 8:16
  • that worked, thanks Val. Commented Apr 26, 2016 at 11:54

1 Answer 1

13

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

So, instead of this

"fields": [
  ".*"
],

Use this:

"_source": true
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Val, I didn't realise there's source filtering available. I'll make use of that actually, not for this particular thing, but some others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.