最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合ES解析搜索返回字段問(wèn)題

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

1. 數(shù)據(jù)構(gòu)造

索引2個(gè)文檔到 hotel 索引中:

PUT /hotel/_doc/1
{
  "title": "文雅酒店",
  "city": "青島",
  "price": 556,
  "create_time": "20200418120000",
  "amenities": "浴池,普通停車場(chǎng)/充電停車場(chǎng)",
  "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,充電停車場(chǎng)/可升降停車場(chǎng)",
  "full_room": false,
  "location": {
    "lat": 39.915153,
    "lon": 116.403
  },
  "praise": 60
}

PUT /hotel/_doc/1
{
  "title": "文雅酒店",
  "city": "青島",
  "price": 556,
  "create_time": "20200418120000",
  "amenities": "浴池,普通停車場(chǎng)/充電停車場(chǎng)",
  "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,充電停車場(chǎng)/可升降停車場(chǎng)",
  "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,充電停車場(chǎng)/可升降停車場(chǎng)",
          "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" : "浴池,普通停車場(chǎng)/充電停車場(chǎng)",
          "full_room" : false,
          "location" : {
            "lat" : 36.083078,
            "lon" : 120.37566
          },
          "praise" : 10
        }
      }
    ]
  }
}

3. ElasticSearch 搜索結(jié)果字段解析

1. took 搜索請(qǐng)求耗費(fèi)了多少毫秒

took 值告訴我們執(zhí)行整個(gè)搜索請(qǐng)求耗費(fèi)了多少毫秒。

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

_shards 部分告訴我們?cè)诓樵冎袇⑴c分片的總數(shù),以及這些分片成功了多少個(gè)失敗了多少個(gè)。正常情況下我們不希望分片失敗,但是分片失敗是可能發(fā)生的。如果我們?cè)庥龅揭环N災(zāi)難級(jí)別的故障,在這個(gè)故障中丟失了相同分片的原始數(shù)據(jù)和副本,那么對(duì)這個(gè)分片將沒(méi)有可用副本來(lái)對(duì)搜索請(qǐng)求作出響應(yīng)。假若這樣,Elasticsearch 將報(bào)告這個(gè)分片是失敗的,但是會(huì)繼續(xù)返回剩余分片的結(jié)果。

3. timed_out 查詢是否超時(shí)

timed_out 值告訴我們查詢是否超時(shí)。默認(rèn)情況下,搜索請(qǐng)求不會(huì)超時(shí)。

4. hits 表示搜索結(jié)果

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

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

4. SpringBoot 整合ElasticSearch獲取搜索結(jié)果

@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);

        // 搜索結(jié)果
        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);

        // 搜索結(jié)果
        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,充電停車場(chǎng)/可升降停車場(chǎng)","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":"浴池,普通停車場(chǎng)/充電停車場(chǎng)","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,充電停車場(chǎng)/可升降停車場(chǎng)",
                    "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": "浴池,普通停車場(chǎng)/充電停車場(chǎng)",
                    "full_room": false,
                    "location": {
                        "lat": 36.083078,
                        "lon": 120.37566
                    },
                    "praise": 10
                }
            }
        ]
    }
}

5 .ElasticSearch 搜索結(jié)果的面試題

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

答:_score 字段表示匹配文檔的相關(guān)度得分,分?jǐn)?shù)越高表示匹配度越高。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

相關(guān)文章

  • 基于Java實(shí)現(xiàn)Redis多級(jí)緩存方案

    基于Java實(shí)現(xiàn)Redis多級(jí)緩存方案

    這篇文章主要介紹了Redis多級(jí)緩存方案分享,傳統(tǒng)緩存方案、多級(jí)緩存方案、JVM本地緩存,舉例說(shuō)明這些方案,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • 詳解Java中@Override的作用

    詳解Java中@Override的作用

    這篇文章主要介紹了詳解Java中@Override的作用的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • 詳解 問(wèn)題:HttpServlet cannot be resolved to a type

    詳解 問(wèn)題:HttpServlet cannot be resolved to a type

    這篇文章主要介紹了詳解 問(wèn)題:HttpServlet cannot be resolved to a type的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java如何把數(shù)組轉(zhuǎn)換為ArrayList

    Java如何把數(shù)組轉(zhuǎn)換為ArrayList

    這篇文章主要介紹了Java如何把數(shù)組轉(zhuǎn)換為ArrayList,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 解決Springboot項(xiàng)目打包后的頁(yè)面丟失問(wèn)題(thymeleaf報(bào)錯(cuò))

    解決Springboot項(xiàng)目打包后的頁(yè)面丟失問(wèn)題(thymeleaf報(bào)錯(cuò))

    這篇文章主要介紹了解決Springboot項(xiàng)目打包后的頁(yè)面丟失問(wèn)題(thymeleaf報(bào)錯(cuò)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中常用的代碼匯總

    Java中常用的代碼匯總

    本文給大家分享了20個(gè)常用的java代碼,都是別人項(xiàng)目中使用過(guò)的代碼,這里推薦給大家,有需要的小伙伴可以參考下。
    2015-05-05
  • Java AbstractMethodError原因案例詳解

    Java AbstractMethodError原因案例詳解

    這篇文章主要介紹了Java AbstractMethodError原因案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot兩種方式接入DeepSeek的實(shí)現(xiàn)

    SpringBoot兩種方式接入DeepSeek的實(shí)現(xiàn)

    本文主要介紹了SpringBoot兩種方式接入DeepSeek的實(shí)現(xiàn),包括HttpClient方式和基于spring-ai-openai的方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • Java中的HashMap集合深度解析

    Java中的HashMap集合深度解析

    這篇文章主要介紹了Java中的HashMap集合深度解析, HashMap可以說(shuō)是Java中最常用的集合類框架之一,是Java語(yǔ)言中非常典型的數(shù)據(jù)結(jié)構(gòu),我們總會(huì)在不經(jīng)意間用到它,很大程度上方便了我們?nèi)粘i_發(fā),需要的朋友可以參考下
    2023-09-09
  • idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟

    idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟

    這篇文章主要介紹了idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論

西丰县| 厦门市| 乌拉特中旗| 法库县| 慈利县| 舞阳县| 朝阳县| 闽清县| 罗定市| 且末县| 西宁市| 达拉特旗| 元阳县| 南通市| 太仓市| 拉孜县| 天津市| 南木林县| 灌南县| 平顺县| 宁南县| 简阳市| 新化县| 静海县| 勐海县| 鸡东县| 佛坪县| 老河口市| 武义县| 遵义市| 手游| 佳木斯市| 汉源县| 德江县| 游戏| 湖南省| 温泉县| 玉龙| 平乡县| 天峻县| 南城县|