索引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解析搜尋回傳字段問題怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!