Home  >  Article  >  Java  >  How to solve the problem of SpringBoot integrating ES parsing search return fields

How to solve the problem of SpringBoot integrating ES parsing search return fields

PHPz
PHPzforward
2023-05-16 08:13:071165browse

    1. Data structure

    Index 2 documents into the hotel index:

    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 queries all indexes in the cluster All documents

     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 search result field parsing

    1. took how many milliseconds the search request took

    took The value tells us to execute the entire How many milliseconds the search request took.

    2. shards The total number of participating shards in the query

    _shards part tells us the total number of participating shards in the query, and how many failures these shards succeeded How many. Normally we don't want sharding to fail, but sharding failure can happen. If we experience a catastrophic failure in which original data and replicas of the same shard are lost, there will be no replicas available for that shard to respond to search requests. If so, Elasticsearch will report this shard as failed, but will continue to return results for the remaining shards.

    3. timed_out query times out

    timed_out The value tells us whether the query times out. By default, search requests do not time out.

    4. hits represents the search results

    The most important part of the returned results is hits, which contains the total field to represent the matched documents total, and a hits array containing the top ten documents for the query results. When parsing search results, we usually need to pay attention to the following fields:

    hits.total.value: The total number of matching documents.
    hits.max_score: The maximum value of _score of the document matching the query.
    hits.hits: List of matching documents.
    hits.hits._source: The original data of the matching document.
    hits.hits._score: The score of the matching document. It measures how well the document matches the query. By default, the most relevant document results are returned first, that is, the returned documents are sorted in descending order by score.
    hits.hits.highlight: Highlight information of matching documents.

    4. SpringBoot integrates ElasticSearch to obtain search results

    @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 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 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. Interview questions for ElasticSearch search results

    1. What is the _score field in ElasticSearch search results mean?

    Answer: The _score field indicates the relevance score of the matching document. The higher the score, the higher the matching degree.

    2. What does the highlight field in ElasticSearch search results mean?

    Answer: The highlight field indicates the highlighted field in the matching document and its highlighted content.

    3. How to get the total number of documents in ElasticSearch search results?

    Answer: You can get the total number of matching documents through the hits.total.value field.

    4. How to get the list of matching documents in ElasticSearch search results?

    Answer: You can get the matching document list through the hits.hits field.

    5. How to obtain the original data of matching documents in ElasticSearch search results?

    Answer: You can obtain the original data of matching documents through the hits.hits._source field.

    6. How to obtain the highlighting information of matching documents in ElasticSearch search results?

    Answer: You can obtain the highlight information of matching documents through the hits.hits.highlight field.

    7. What does the _shards field in ElasticSearch search results mean?

    Answer: The _shards field represents the shard information involved in the search, including the total number of shards, the number of successful shards, the number of skipped shards, and the number of failed shards.

    8. What does the took field in ElasticSearch search results mean?

    Answer: The took field indicates the search time, in milliseconds.

    The above is the detailed content of How to solve the problem of SpringBoot integrating ES parsing search return fields. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete