欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合ES解析搜索返回字段問題

 更新時間:2023年03月21日 14:36:00   作者:我一直在流浪  
這篇文章主要介紹了SpringBoot整合ES解析搜索返回字段問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1. 數(shù)據(jù)構造

索引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
}

2. ElasticSearch 查詢集群中所有索引中的所有文檔

 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
        }
      }
    ]
  }
}

3. ElasticSearch 搜索結果字段解析

1. took 搜索請求耗費了多少毫秒

took 值告訴我們執(zhí)行整個搜索請求耗費了多少毫秒。

2. shards 查詢中參與分片的總數(shù)

_shards 部分告訴我們在查詢中參與分片的總數(shù),以及這些分片成功了多少個失敗了多少個。正常情況下我們不希望分片失敗,但是分片失敗是可能發(fā)生的。如果我們遭遇到一種災難級別的故障,在這個故障中丟失了相同分片的原始數(shù)據(jù)和副本,那么對這個分片將沒有可用副本來對搜索請求作出響應。假若這樣,Elasticsearch 將報告這個分片是失敗的,但是會繼續(xù)返回剩余分片的結果。

3. timed_out 查詢是否超時

timed_out 值告訴我們查詢是否超時。默認情況下,搜索請求不會超時。

4. hits 表示搜索結果

返回結果中最重要的部分是 hits ,它包含 total 字段來表示匹配到的文檔總數(shù),并且一個 hits 數(shù)組包含所查詢結果的前十個文檔。在解析搜索結果時,我們通常需要關注以下幾個字段:

hits.total.value:匹配的文檔總數(shù)。
hits.max_score:與查詢所匹配文檔的_score的最大值。
hits.hits:匹配的文檔列表。
hits.hits._source:匹配的文檔的原始數(shù)據(jù)。
hits.hits._score:匹配的文檔的分數(shù)。它衡量了文檔與查詢的匹配程度,默認情況下,首先返回最相關的文檔結果,就是說,返回的文檔是按照score 降序排列的。
hits.hits.highlight:匹配的文檔的高亮顯示信息。

4. SpringBoot 整合ElasticSearch獲取搜索結果

@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:匹配的文檔總數(shù)
        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:匹配的文檔的原始數(shù)據(jù)
            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:匹配的文檔總數(shù)
        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:匹配的文檔的原始數(shù)據(jù)
            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
                }
            }
        ]
    }
}

5 .ElasticSearch 搜索結果的面試題

1. ElasticSearch 搜索結果中的 _score 字段是什么意思?

答:_score 字段表示匹配文檔的相關度得分,分數(shù)越高表示匹配度越高。

2. ElasticSearch 搜索結果中的 highlight 字段是什么意思?

答:highlight 字段表示匹配文檔中被高亮顯示的字段及其高亮顯示的內容。

3. 如何獲取 ElasticSearch 搜索結果中的總文檔數(shù)?

答:可以通過 hits.total.value 字段獲取匹配的文檔總數(shù)。

4. 如何獲取 ElasticSearch 搜索結果中的匹配文檔列表?

答:可以通過 hits.hits 字段獲取匹配的文檔列表。

5. 如何獲取 ElasticSearch 搜索結果中匹配文檔的原始數(shù)據(jù)?

答:可以通過 hits.hits._source 字段獲取匹配文檔的原始數(shù)據(jù)。

6. 如何獲取 ElasticSearch 搜索結果中匹配文檔的高亮顯示信息?

答:可以通過 hits.hits.highlight 字段獲取匹配文檔的高亮顯示信息。

7. ElasticSearch 搜索結果中的 _shards 字段是什么意思?

答:_shards 字段表示搜索涉及的分片信息,包括總分片數(shù)、成功的分片數(shù)、跳過的分片數(shù)和失敗的分片數(shù)。

8. ElasticSearch 搜索結果中的 took 字段是什么意思?

答:took 字段表示搜索耗時,單位為毫秒。

到此這篇關于SpringBoot整合ES解析搜索返回字段的文章就介紹到這了,更多相關SpringBoot整合ES內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 帶你了解Java中的異常處理(下)

    帶你了解Java中的異常處理(下)

    這篇文章主要介紹了Java中的異常處理的相關資料,幫助大家更好的理解和學習Java,感興趣的朋友可以了解下
    2020-08-08
  • SpringBoot自帶模板引擎Thymeleaf使用示例詳解

    SpringBoot自帶模板引擎Thymeleaf使用示例詳解

    Thymeleaf是一款用于渲染XML/HTML5內容的模板引擎,類似JSP,它可以輕易的與SpringMVC等Web框架進行集成作為Web應用的模板引擎,本文給大家介紹SpringBoot自帶模板引擎Thymeleaf使用示例,感興趣的朋友一起看看吧
    2023-12-12
  • Java 抽象類特點總結

    Java 抽象類特點總結

    在面向對象的概念中,所有的對象都是通過類來描繪的,但是反過來,并不是所有的類都是用來描繪對象的,如果一個類中沒有包含足夠的信息來描繪一個具體的對象,這樣的類就是抽象類
    2021-10-10
  • springboot 多環(huán)境切換的方法

    springboot 多環(huán)境切換的方法

    這篇文章主要介紹了springboot 多環(huán)境切換的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • java中Spring Security的實例詳解

    java中Spring Security的實例詳解

    這篇文章主要介紹了java中Spring Security的實例詳解的相關資料,spring security是一個多方面的安全認證框架,提供了基于JavaEE規(guī)范的完整的安全認證解決方案,需要的朋友可以參考下
    2017-09-09
  • JAVA面試題String產(chǎn)生了幾個對象

    JAVA面試題String產(chǎn)生了幾個對象

    這篇文章主要介紹了JAVA面試題 String s = new String("xyz");產(chǎn)生了幾個對象?,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • SpringMVC整合SSM實現(xiàn)表現(xiàn)層數(shù)據(jù)封裝詳解

    SpringMVC整合SSM實現(xiàn)表現(xiàn)層數(shù)據(jù)封裝詳解

    這篇文章主要介紹了SpringMVC整合SSM實現(xiàn)表現(xiàn)層數(shù)據(jù)封裝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-10-10
  • 一文搞懂Spring Bean中的作用域和生命周期

    一文搞懂Spring Bean中的作用域和生命周期

    Spring作為當前Java最流行、最強大的輕量級框架,受到了程序員的熱烈歡迎。了解Spring?Bean的作用域與生命周期是非常必要的,快跟隨小編一起學習學習吧
    2022-06-06
  • Java數(shù)據(jù)結構之基于比較的排序算法基本原理及具體實現(xiàn)

    Java數(shù)據(jù)結構之基于比較的排序算法基本原理及具體實現(xiàn)

    最近剛學習完七種比較常見的基于比較的排序算法,感覺比較重要,所以寫個博客記錄一下,通讀本篇對大家的學習或工作具有一定的價值,需要的朋友可以參考下
    2021-09-09
  • java簡單手寫版本實現(xiàn)時間輪算法

    java簡單手寫版本實現(xiàn)時間輪算法

    這篇文章主要為大家詳細介紹了java簡單手寫版本實現(xiàn)時間輪算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評論