首页 > 其他分享 >RestClient查询【ElasticSearch】

RestClient查询【ElasticSearch】

时间:2023-06-23 11:46:08浏览次数:42  
标签:RestClient searchSourceBuilder searchRequest hotel private 查询 ElasticSearch org 

package cn.itcast.hotel;

import cn.itcast.hotel.pojo.HotelDoc;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
public class HotelSearchApplicationTests {

    private RestHighLevelClient client;

    @BeforeEach
    public void before() {
        //client对象就是操作ES的对象
        client =
                new RestHighLevelClient(RestClient.builder
                        (HttpHost.create("http://192.168.221.135:9200")));
    }

    @AfterEach
    public void after() throws IOException {
        client.close();
    }

    /**
     * match 分词再查询
     */
    @Test
    public void test01() throws IOException {
        //todo 2.搜索条件封装
        SearchRequest searchRequest = new SearchRequest("hotel");

        //todo 4.所有搜索条件封装到 searchSourceBuilder对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //todo 5.添加对象
        searchSourceBuilder.query(QueryBuilders.matchQuery("name","如家全季"));

        //todo 3.添加搜索条件对象
        searchRequest.source(searchSourceBuilder);

        //todo 1.找到搜索的API
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        //todo 6.解析 searchResponse 返回结果
        SearchHits hits = searchResponse.getHits();

        System.out.println("查询到的总条数为:" + hits.getTotalHits().value);

        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
            String sourceAsString = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
            System.out.println("查询到的数据:" + hotelDoc);
        }
    }

    /**
     * match_all 查询所有
     */
    @Test
    public void test02() throws IOException {
        SearchRequest searchRequest = new SearchRequest("hotel");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits searchHits = searchResponse.getHits();
        System.out.println("查询总条数:" + searchHits.getTotalHits().value);

        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class);
            System.out.println("数据:" + hotelDoc);
        }
    }

    /**
     * term 精确查询
     */
    @Test
    public void test03() throws IOException {
        SearchRequest searchRequest = new SearchRequest();

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termQuery("brand","万怡"));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits searchHits = searchResponse.getHits();

        System.out.println("总条数:" + searchHits.getTotalHits().value);
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class);
            System.out.println("数据:" + hotelDoc);
        }
    }

    /**
     * rang 范围查询
     */
    @Test
    public void test04() throws IOException {

        SearchRequest searchRequest = new SearchRequest();

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.rangeQuery("price").gt(1000).lt(3000));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits searchHits = searchResponse.getHits();
        System.out.println("总条数:" + searchHits.getTotalHits().value);
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class);
            System.out.println("数据:" + hotelDoc);
        }
    }
}
package cn.itcast.hotel.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class HotelDoc {
    private Long id;
    private String name;
    private String address;
    private Integer price;
    private Integer score;
    private String brand;
    private String city;
    private String starName;
    private String business;
    private String location;
    private String pic;

    public HotelDoc(Hotel hotel) {
        this.id = hotel.getId();
        this.name = hotel.getName();
        this.address = hotel.getAddress();
        this.price = hotel.getPrice();
        this.score = hotel.getScore();
        this.brand = hotel.getBrand();
        this.city = hotel.getCity();
        this.starName = hotel.getStarName();
        this.business = hotel.getBusiness();
        this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
        this.pic = hotel.getPic();
    }
}

 

标签:RestClient,searchSourceBuilder,searchRequest,hotel,private,查询,ElasticSearch,org,
From: https://www.cnblogs.com/Rover20230226/p/17498921.html

相关文章

  • match_all 查询所有【ElasticSearch】
    /***match_all查询所有*/@Testpublicvoidtest02()throwsIOException{SearchRequestsearchRequest=newSearchRequest("hotel");SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();......
  • rang 范围查询【ElasticSearch】
    /***rang范围查询*/@Testpublicvoidtest04()throwsIOException{SearchRequestsearchRequest=newSearchRequest();SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();searchSourceBuilder......
  • term 精确查询【ElasticSearch】
    /***term精确查询*/@Testpublicvoidtest03()throwsIOException{SearchRequestsearchRequest=newSearchRequest();SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();searchSourceBuilder.......
  • match查询【ElasticSearch】
    /***match分词再查询*/@Testpublicvoidtest01()throwsIOException{//todo2.搜索条件封装SearchRequestsearchRequest=newSearchRequest("hotel");//todo4.所有搜索条件封装到searchSourceBuilder对象S......
  • Elasticsearch核心应用场景-日志优化实践
    1.背景日志领域是Elasticsearch(ES)最重要也是规模最大的应用场景之一。这得益于ES有高性能倒排索引、灵活的schema、易用的分布式架构,支持高吞吐写入、高性能查询,同时有强大的数据治理生态、端到端的完整解决方案。但原生ES在高吞吐写入、低成本存储、高性能查询等方面还有......
  • Elasticsearch核心应用场景-日志优化实践
    1.背景日志领域是Elasticsearch(ES)最重要也是规模最大的应用场景之一。这得益于ES有高性能倒排索引、灵活的schema、易用的分布式架构,支持高吞吐写入、高性能查询,同时有强大的数据治理生态、端到端的完整解决方案。但原生ES在高吞吐写入、低成本存储、高性能查询等方面还有......
  • es 查询
    packagecom.heima.search.service.impl;importcom.alibaba.fastjson.JSON;importcom.heima.model.common.dtos.ResponseResult;importcom.heima.model.common.enums.AppHttpCodeEnum;importcom.heima.model.search.dtos.UserSearchDto;importcom.heima.model.user.pojos......
  • es多字段查询:queryString
      https://blog.csdn.net/zl18603543572/article/details/129629817 ......
  • ElasticSearch的安装与启动
    1.ElasticSearch介绍1.1.什么是ESElasticSearch是一个基于Lucene的搜索服务器。它提供了一个基于RESTfulweb接口的分布式全文搜索引擎。ElasticSearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。ElasticSearch用于云计算中,能够达到实......
  • linux下根据端口号查询对应进程
    1、命令lsof,以查找占用端口80为例,用法如下:[root@localhostnginx]#lsof-i:80--命令[root@localhostnginx]#--返回什么都没有表示:没有进程占用80端口[root@localhostsbin]#lsof-i:80COMMANDPIDUSERFDTYPEDEVICESIZE/OFFNODENAMEnginx8246root6uI......