2

I'm using source filtering on a query to just return a small set of information and within that information I want the first photo url of an object:

"query" : { ... },
"_source" : ["name", "photos.thumbnail_url"]

Of course this returns the thumbnail_url for all of the photos stored against each item.

Is there any way to get just the first nested photo's thumbnail_url using source filtering?

(I appreciate that one way to do it would be to index the first photo separately and just source filter to include that separate field but I'm asking whether it is possible without a separate field.)

(I've guessed a few approaches without luck: photos.0.thumbnail_url, photos[0].thumbnail_url, photos.first.thumbnail_url)

1
  • Have you tried inner_hits with size: 1? Commented Oct 9, 2015 at 10:26

1 Answer 1

2

Just an attempt, as I don't know your mapping, with inner_hits:

{
  "size": 5,
  "_source": ["name"], 
  "query": {
    "filtered": {
      "query": {
        "match": {
          "name": "whatever"
        }
      },
      "filter": {
        "nested": {
          "path": "photos",
          "query": {
            "match_all": {}
          },
          "inner_hits": {
            "size": 1,
            "_source": ["thumbnail_url"]
          }
        }
      }
    }
  }
}

or use a script field:

  "script_fields": {
    "FIELD": {
      "script": "urls=_source.photos.thumbnail_url;return urls[0]"
    }
  }

and I don't think it's possible to do this with source filtering.

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

7 Comments

Yep, that definitely returns just one photo thumbnail url. I can't seem to get it to return the first one though but I can index an, er, index field easily enough to sort on. Thanks for the help.
The question is what does "first" mean to you? First as you indexed (like source first) or "first" by other definitions?
First as I indexed, yes, perhaps that just needs to be a bit more explicit.
You tested with script_field or inner_hits?
I need to be a bit more explicit all round :) I tested with inner hits - my expectation is that script_field will return the first in the source but I've not got to testing that yet (have scripting off at the moment).
|

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.