2

I'm building a native query but I only want to return certain fields, all of which are held within a parent field. I think I am looking for the QueryBuilders or NativeSearchQueryBuilder equivalent of the REST API's _source. Here's a code example:

NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
.withIndices("myIndex")
.withTypes("myType")
.withQuery(QueryBuilders.queryStringQuery("parent.field2:Foo*"));
.withFields("parent.field1");

I'd expect this to return a list of only parent.field1 that are associated with objects that have parent.field2 like Foo*. But it returns nothing.

Thanks for any help!

5
  • This won't return any list. This will just create a builder. How are you fetching results from this? Please share that code too. Commented Jul 7, 2016 at 3:55
  • Richa, right, I only included this part bc I thought the fetching of results was unrelated to the fields returned --- ie, that everything is included in the query. I build the builder into a query and pass it to the elastic template queryForList method. Commented Jul 7, 2016 at 3:58
  • So u are saying the list u are getting is empty. This may be due to query did not return anything. Are you sure query part is fine .. Commented Jul 7, 2016 at 5:37
  • @Richa Good point, the query part is fine though, I can get "id" I think because it is at the parent level, but I cannot selectively add new fields below the parent level. If I remove the withFields part I get the entire set of data that I'd expect from the query. Commented Jul 7, 2016 at 5:44
  • Ohhk. I think I have faced this same issue. Spring data tries to search the field with name "parent.field1" which does not exist.. May be springdata doesn't allow to add fields which are not at root. You can do that with native Elasticsearch query Commented Jul 7, 2016 at 5:52

1 Answer 1

2

After some research, I found the answer is in NativeSearchQueryBuilder. I was just using an older version of spring-data elastic search, so I could not see this method: withSourceFilter. The way to do this is:

NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
.withIndices("myIndex")
.withTypes("myType")
.withQuery(QueryBuilders.queryStringQuery("parent.field2:Foo*"));
.withSourceFilter(new FetchSourceFilter(<String array of includes>, null));

FetchSourceFilter takes 2 arguments, a String[] array of includes and one of excludes. In my example, I'd have an array like new String[]{"parent.field1"} passed to FetchSourceFilter, which in turn is passed to withSourceFilter. The search above will then return (once built and ran) a list of parent.field1 with parent.field2 like Foo*.

The version I upgraded to was spring-data-elasticsearch 2.0.2.

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.