1

I have the following document stored in elastic search

{"Book_Id" : "102" ,"Book_Name" : "Alice in wonderLand", "Review_Text" :"DescentRead","Rating_Percentage" :"100" }'
{"Book_Id" : "102" ,"Book_Name" : "Alice in wonderLand", "Review_Text" :"For Kids","Rating_Percentage" :"50" }'
{"Book_Id" : "103" ,"Book_Name" : "Blah Blah", "Review_Text" :"Great","Rating_Percentage" :"100" }'

I want to do a search and retrieve only one field(Review Text in this example) as an out put.I am using the following code

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
searchSourceBuilder.query(QueryBuilders.matchQuery("Book_Id", "102"))
        .fields("_source.Review_Text");
Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex("reviews")
                .addType("bookreview")
                .build();

SearchResult result = client.execute(search);

But I keep getting the error -

{"error":{"root_cause":[{"type":"parsing_exception","reason":"Deprecated field [fields] used, expected [stored_fields] instead","line":10,"col":14}],

what is missing here ? How else do we retrieve only specific fileds instead of all the fields as Json

Thanks Shabna

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Sajna
  • 79
  • 1
  • 9

1 Answers1

1

Accessing fields directly is deprecated as described in the error message. Use source filtering instead:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
searchSourceBuilder.query(QueryBuilders.matchQuery("Book_Id", "102"))
        .fetchSource("Review_Text", null);                    <-- change this line
Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex("reviews")
                .addType("bookreview")
                .build();
Val
  • 207,596
  • 13
  • 358
  • 360
  • fetchSource is undefined for SearchSourceBuilder . – Sajna Aug 10 '17 at 04:02
  • Which version of the elasticsearch library are you using? – Val Aug 10 '17 at 04:05
  • Got it working !Thanks a lot after changing the version of elastic serach – Sajna Aug 10 '17 at 07:51
  • When I access two filed the resulting json is of the following format as expected. {"Book_Id":"102","Review_Text":"DescentRead"},{"Book_Id":"102","Review_Text":"For Kids."},{"Book_Id":"103","Review_Text":"Great"} Is it possible to aggregate the multiple values of a particular field into an array based on id for example: [ { Book_Id: 102, Review_Text: [ "DescentRead", "For Kids", ....], { Book_Id: 103, reviews: [ "Great"] } ] – Sajna Aug 10 '17 at 08:00
  • Not in the source directly but you can get a semantically equivalent response by using aggregations. – Val Aug 10 '17 at 08:06
  • In the Jest repo, they have some test class that shows how to go around terms aggregations: https://github.com/searchbox-io/Jest/blob/af2e137311b1ce56ebe233b243b74fa2eed6ace3/jest/src/test/java/io/searchbox/core/search/aggregation/TermsAggregationIntegrationTest.java – Val Aug 10 '17 at 08:25
  • No you can construct it using Java/Jest classes, but please create another question so we don't mix topics together – Val Aug 10 '17 at 08:46
  • moving to a diff query – Sajna Aug 10 '17 at 10:49