在 elasticSearch 中使用 RestClient 检索特定字段
retrieving specific fileds using JestClient in elastic Serach
我在弹性搜索中存储了以下文档
{"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" }'
我想进行搜索并仅检索一个字段(本例中的评论文本)作为输出 put.I 我正在使用以下代码
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 结果 = client.execute(搜索);
但我一直收到错误消息 -
{"error":{"root_cause":[{"type":"parsing_exception","reason":"Deprecated field [fields] used, expected [stored_fields] instead","line":10,"col":14}],
这里缺少什么?我们如何只检索特定的字段而不是所有字段 Json
谢谢
莎布娜
如错误消息中所述,直接访问 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();
我在弹性搜索中存储了以下文档
{"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" }'
我想进行搜索并仅检索一个字段(本例中的评论文本)作为输出 put.I 我正在使用以下代码
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 结果 = client.execute(搜索);
但我一直收到错误消息 -
{"error":{"root_cause":[{"type":"parsing_exception","reason":"Deprecated field [fields] used, expected [stored_fields] instead","line":10,"col":14}],
这里缺少什么?我们如何只检索特定的字段而不是所有字段 Json
谢谢 莎布娜
如错误消息中所述,直接访问 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();