在弹性Serach中使用JestClient检索特定文件

时间:2017-08-09 17:37:40

标签: elasticsearch jest

我在弹性搜索中存储了以下文档

{"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" }'

我想进行搜索,只检索一个字段(本例中为Review Text)作为输出。我使用以下代码

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);

但我一直收到错误 -

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

这里缺少什么?我们如何只检索特定文件而不是像Json

那样检索所有字段

由于 Shabna

1 个答案:

答案 0 :(得分:0)

不推荐直接访问fields,如错误消息中所述。请改用source filtering

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();