作者:xsf9507 | 来源:互联网 | 2023-06-24 15:10
问题描述想要获取搜索结果中hits的数据,遍历his[]的时候发现为空。不知道是什么原因,es版本6.4.3,client版本6.4.3使用postman能获取
问题描述
想要获取搜索结果中hits的数据,遍历his[]的时候发现为空。不知道是什么原因,es版本6.4.3,client版本6.4.3
使用postman能获取数据。使用restclient,控制台显示如下:但是hits[] 中无内容。
总记录数:3
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":3,"max_score":4.0247,"hits":[]}}
总记录数:3
{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":3,"max_score":4.0247,"hits":[]}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| searchText = "安装出错 pass";
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
searchRequest.types(INDEX_TYPE);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.multiMatchQuery(searchText,"title","text"));
sourceBuilder.from(PAGE_STAER);
sourceBuilder.from(PAGE_SIZE);
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
searchRequest.source(sourceBuilder);
try {
SearchResponse searchRespOnse= client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
List tempEntityList = new ArrayList<>();
for(SearchHit hit : searchHits){
float score = hit.getScore();
Map sourceAsMap = hit.getSourceAsMap();
EsSearchDocResponseMessage tempEntity = new EsSearchDocResponseMessage();
tempEntity.setScore(score);
tempEntity.setText((String)sourceAsMap.get("text"));
tempEntity.setArticleId((String)sourceAsMap.get("articleId"));
} |