您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot如何整合ES解析搜索返回字段問題”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot如何整合ES解析搜索返回字段問題”吧!
索引2個文檔到 hotel 索引中:
PUT /hotel/_doc/1 { "title": "文雅酒店", "city": "青島", "price": 556, "create_time": "20200418120000", "amenities": "浴池,普通停車場/充電停車場", "full_room": false, "location": { "lat": 36.083078, "lon": 120.37566 }, "praise": 10 } PUT /hotel/_doc/2 { "title": "金都嘉怡假日酒店", "city": "北京", "price": 337, "create_time": "20210315200000", "amenities": "wifi,充電停車場/可升降停車場", "full_room": false, "location": { "lat": 39.915153, "lon": 116.403 }, "praise": 60 } PUT /hotel/_doc/1 { "title": "文雅酒店", "city": "青島", "price": 556, "create_time": "20200418120000", "amenities": "浴池,普通停車場/充電停車場", "full_room": false, "location": { "lat": 36.083078, "lon": 120.37566 }, "praise": 10 } PUT /hotel/_doc/2 { "title": "金都嘉怡假日酒店", "city": "北京", "price": 337, "create_time": "20210315200000", "amenities": "wifi,充電停車場/可升降停車場", "full_room": false, "location": { "lat": 39.915153, "lon": 116.403 }, "praise": 60 }
GET /hotel/_search
{ "took" : 499, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "hotel", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "title" : "金都嘉怡假日酒店", "city" : "北京", "price" : 337, "create_time" : "20210315200000", "amenities" : "wifi,充電停車場/可升降停車場", "full_room" : false, "location" : { "lat" : 39.915153, "lon" : 116.403 }, "praise" : 60 } }, { "_index" : "hotel", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "title" : "文雅酒店", "city" : "青島", "price" : 556, "create_time" : "20200418120000", "amenities" : "浴池,普通停車場/充電停車場", "full_room" : false, "location" : { "lat" : 36.083078, "lon" : 120.37566 }, "praise" : 10 } } ] } }
took
值告訴我們執行整個搜索請求耗費了多少毫秒。
_shards
部分告訴我們在查詢中參與分片的總數,以及這些分片成功了多少個失敗了多少個。正常情況下我們不希望分片失敗,但是分片失敗是可能發生的。如果我們遭遇到一種災難級別的故障,在這個故障中丟失了相同分片的原始數據和副本,那么對這個分片將沒有可用副本來對搜索請求作出響應。假若這樣,Elasticsearch 將報告這個分片是失敗的,但是會繼續返回剩余分片的結果。
timed_out
值告訴我們查詢是否超時。默認情況下,搜索請求不會超時。
返回結果中最重要的部分是 hits
,它包含 total
字段來表示匹配到的文檔總數,并且一個 hits
數組包含所查詢結果的前十個文檔。在解析搜索結果時,我們通常需要關注以下幾個字段:
hits.total.value:匹配的文檔總數。
hits.max_score:與查詢所匹配文檔的_score的最大值。
hits.hits:匹配的文檔列表。
hits.hits._source:匹配的文檔的原始數據。
hits.hits._score:匹配的文檔的分數。它衡量了文檔與查詢的匹配程度,默認情況下,首先返回最相關的文檔結果,就是說,返回的文檔是按照score 降序排列的。
hits.hits.highlight:匹配的文檔的高亮顯示信息。
@Slf4j @Service public class ElasticSearchImpl { @Autowired private RestHighLevelClient restHighLevelClient; public void searchUser() throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchRequest searchRequest = new SearchRequest(new String[]{"hotel"},searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); TimeValue took = searchResponse.getTook(); System.out.println("took = " + took); // 搜索結果 SearchHits searchHits = searchResponse.getHits(); // hits.total.value:匹配的文檔總數 TotalHits totalHits = searchHits.getTotalHits(); long value = totalHits.value; System.out.println("value = " + value); // hits.max_score:與查詢所匹配文檔的_score的最大值 float maxScore = searchHits.getMaxScore(); System.out.println("maxScore = " + maxScore); // hits.hits:匹配的文檔列表 SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { // hits.hits._source:匹配的文檔的原始數據 String sourceAsString = hit.getSourceAsString(); System.out.println("sourceAsString = " + sourceAsString); // hits.hits._id:匹配的文檔的id String id = hit.getId(); System.out.println("id = " + id); Map<String, DocumentField> fields = hit.getFields(); System.out.println("fields = " + fields); String index = hit.getIndex(); System.out.println("index = " + index); float score = hit.getScore(); System.out.println("score = " + score); } System.out.println(searchResponse); } } @Slf4j @Service public class ElasticSearchImpl { @Autowired private RestHighLevelClient restHighLevelClient; public void searchUser() throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchRequest searchRequest = new SearchRequest(new String[]{"hotel"},searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); TimeValue took = searchResponse.getTook(); System.out.println("took = " + took); // 搜索結果 SearchHits searchHits = searchResponse.getHits(); // hits.total.value:匹配的文檔總數 TotalHits totalHits = searchHits.getTotalHits(); long value = totalHits.value; System.out.println("value = " + value); // hits.max_score:與查詢所匹配文檔的_score的最大值 float maxScore = searchHits.getMaxScore(); System.out.println("maxScore = " + maxScore); // hits.hits:匹配的文檔列表 SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { // hits.hits._source:匹配的文檔的原始數據 String sourceAsString = hit.getSourceAsString(); System.out.println("sourceAsString = " + sourceAsString); // hits.hits._id:匹配的文檔的id String id = hit.getId(); System.out.println("id = " + id); Map<String, DocumentField> fields = hit.getFields(); System.out.println("fields = " + fields); String index = hit.getIndex(); System.out.println("index = " + index); float score = hit.getScore(); System.out.println("score = " + score); } System.out.println(searchResponse); } }
took=2ms value = 2 maxScore = 1.0 sourceAsString = {"title":"金都嘉怡假日酒店","city":"北京","price":337,"create_time":"20210315200000","amenities":"wifi,充電停車場/可升降停車場","full_room":false,"location":{"lat":39.915153,"lon":116.403},"praise":60} id = 2 fields = {} index = hotel score = 1.0 sourceAsString = {"title":"文雅酒店","city":"青島","price":556,"create_time":"20200418120000","amenities":"浴池,普通停車場/充電停車場","full_room":false,"location":{"lat":36.083078,"lon":120.37566},"praise":10} id = 1 fields = {} index = hotel score = 1.0
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "hotel", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "title": "金都嘉怡假日酒店", "city": "北京", "price": 337, "create_time": "20210315200000", "amenities": "wifi,充電停車場/可升降停車場", "full_room": false, "location": { "lat": 39.915153, "lon": 116.403 }, "praise": 60 } }, { "_index": "hotel", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "title": "文雅酒店", "city": "青島", "price": 556, "create_time": "20200418120000", "amenities": "浴池,普通停車場/充電停車場", "full_room": false, "location": { "lat": 36.083078, "lon": 120.37566 }, "praise": 10 } } ] } }
1. ElasticSearch 搜索結果中的 _score 字段是什么意思?
答:_score 字段表示匹配文檔的相關度得分,分數越高表示匹配度越高。
2. ElasticSearch 搜索結果中的 highlight 字段是什么意思?
答:highlight 字段表示匹配文檔中被高亮顯示的字段及其高亮顯示的內容。
3. 如何獲取 ElasticSearch 搜索結果中的總文檔數?
答:可以通過 hits.total.value 字段獲取匹配的文檔總數。
4. 如何獲取 ElasticSearch 搜索結果中的匹配文檔列表?
答:可以通過 hits.hits 字段獲取匹配的文檔列表。
5. 如何獲取 ElasticSearch 搜索結果中匹配文檔的原始數據?
答:可以通過 hits.hits._source 字段獲取匹配文檔的原始數據。
6. 如何獲取 ElasticSearch 搜索結果中匹配文檔的高亮顯示信息?
答:可以通過 hits.hits.highlight 字段獲取匹配文檔的高亮顯示信息。
7. ElasticSearch 搜索結果中的 _shards 字段是什么意思?
答:_shards 字段表示搜索涉及的分片信息,包括總分片數、成功的分片數、跳過的分片數和失敗的分片數。
8. ElasticSearch 搜索結果中的 took 字段是什么意思?
答:took 字段表示搜索耗時,單位為毫秒。
感謝各位的閱讀,以上就是“SpringBoot如何整合ES解析搜索返回字段問題”的內容了,經過本文的學習后,相信大家對SpringBoot如何整合ES解析搜索返回字段問題這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。