3

So far I followed this guide to get native java scripts working with elasticsearch. Accessing normal non-nested fields works fine using doc().field("fieldname").

Does the same work for nested fields? How can I loop over them and access nested fields?

EDIT: After reading imotov's answer below I ended up including the nested field in the root document using include_in_root or include_in_parent (See docs).

GeoPoint[] locations = ((GeoPointDocFieldData)doc().field("places.location")).getValues();

for (GeoPoint location : locations) {
    // Do Stuff
    double lat = location.lat();
    double lon = location.lon();
}
6
  • I'm sorry...what are you asking? Commented Apr 3, 2013 at 4:18
  • How can I access nested fields? See this for non-nested fields. Commented Apr 3, 2013 at 4:56
  • It depends on the type of the script and where it is running. Is it script_field, sort_script, filter_script, etc? Can you provide the query that is using your native script? Commented Apr 3, 2013 at 14:24
  • @imotov It is a script field (instead of the static boost) for a filter inside a custom_filters_score Commented Apr 8, 2013 at 1:27
  • Is this custom_filters_score inside a nested query or on the top level? Can you provide the query that is using your native script? Commented Apr 8, 2013 at 9:51

1 Answer 1

4

This is tough one. The nested objects are indexed internally as separate documents, so the top level custom_filters_score is operating on the root object and doesn't have access to nested objects. But there is a few things you can do.

The best option in my opinion is to move custom_filters_score into the nested filter where your script will run on the nested objects. See Elastic search - tagging strength (nested/child document boosting) as an example.

The second option is to set include_in_root setting to true on the nested object mapping. This way all your nested object fields will be indexed in the root objects as well and you will be able to access them using dot notation: places.location as if were not nested. The problem with this option is that you will have to index fields twice and you will not know which nested object matched your nested query.

The third option is to retrieve nested object fields from source. This is going to be really slow and not really applicable if your searches produce a lot of results.

Sign up to request clarification or add additional context in comments.

Comments

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.