RestAPI操作ES

一、RestAPI

ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址: https://www.elastic.co/guide/en/elasticsearch/client/index.html

 我们学习的是Java HighLevel Rest Client客户端API

1.1初始化RestClient

在elasticsearch提供的API中,与elasticsearch一切交互都封装在一个名为RestHighLevelClient的类 中,必须先完成这个对象的初始化,建立与elasticsearch的连接。

分为三步:

1)引入es的RestHighLevelClient依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:

<properties>
    <java.version>8</java.version>
    <elasticsearch.version>7.12.0</elasticsearch.version>
</properties>

3)初始化RestHighLevelClient:

初始化的代码如下:

这里为了单元测试方便,我们创建一个测试类,然后将初始化的代码编写在 @BeforeEach方法中:

//初始化RestHighLevelClient
    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://localhost:9200")
        ));
    }
   @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }

二、RestClient操作索引库 

 2.1创建索引库

创建一个类,定义mapping映射的JSON字符串常量:

public class HotelConstants {
    public static final String MAPPING_TEMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"address\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"price\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"score\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"brand\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"city\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"starName\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"business\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"location\":{\n" +
            "        \"type\": \"geo_point\"\n" +
            "      },\n" +
            "      \"pic\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"all\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
}

在hotel-demo中的HotelIndexTest测试类中,编写单元测试,实现创建索引:

//创建索引库
    @Test
    void createHotelIndex() throws IOException {
        // 1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest("hotels");
        // 2.准备请求的参数:DSL语句
        request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
        // 3.发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

2.2删除索引库 

   @Test
    void testDeleteHotelIndex() throws IOException {
        // 1.创建Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("hotels");
        // 2.发送请求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

2.3判断索引库是否存在

  @Test
    void testExistsHotelIndex() throws IOException {
        // 1.创建Request对象
        GetIndexRequest request = new GetIndexRequest("hotels");
        // 2.发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        // 3.输出
        System.err.println(exists ? "索引库已经存在!" : "索引库不存在!");
    }

总结:

JavaRestClient操作elasticsearch的流程基本类似。核心是client.indices()方法来获取索引库的操作对象。

 索引库操作的基本步骤:

  • 初始化RestHighLevelClient
  • 创建XxxIndexRequest。XXX是Create、Get、Delete
  • 准备DSL( Create时需要,其它是无参)
  • 发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete

三、RestClient操作文档

3.1新增文档

与数据库相关的实体类

@Data
@TableName("tb_hotel")
public class Hotel {
    @TableId(type = IdType.INPUT)
    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 longitude;//经度
    private String latitude;//纬度
    private String pic;
}

我们需要定义一个新的类型,与索引库结构吻合:为ES索引库设计实体类

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

在测试类中,编写测试单元

 @Test
    void testAddDocument() throws IOException{
        // 1.根据id查询酒店数据
        Hotel hotel = hotelService.getById(672207);
        // 2.转换为文档类型
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 3.将HotelDoc转json
        String json = JSON.toJSONString(hotelDoc);
        // 1.准备Request对象
        IndexRequest request = new IndexRequest("hotels").id(hotelDoc.getId().toString());
        // 2.准备Json文档
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

3.2查询文档

   @Test
    void testGetDocumentById() throws IOException{
        // 1.准备Request
        GetRequest request = new GetRequest("hotels", "672207");
        //发送请求,得到响应
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //3.解析响应结果
        String json = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }

3.3删除文档

 @Test
    void testDeleteDocument() throws IOException{
        // 1.准备Request
        DeleteRequest request = new DeleteRequest("hotels", "672207");
        // 2.发送请求
        client.delete(request,RequestOptions.DEFAULT);
    }

3.4修改文档 

 修改讲过两种方式:

  • 全量修改:本质是先根据id删除,再新增
  • 增量修改:修改文档中的指定字段值

在RestClient的API中,全量修改与新增的API完全一致

 @Test
    void testUpdateDocument() throws IOException{
        //1.准备Request
        UpdateRequest request = new UpdateRequest("hotels", "672207");
        //2.准备请求参数
        request.doc(
                "price", "2000",
                "starName", "五星级"
        );
        //3.发送请求
        client.update(request,RequestOptions.DEFAULT);
    }

3.5 批量导入文档

   @Test
    void testBulkRequest() throws IOException{
        // 批量查询酒店数据
        List<Hotel> hotels = hotelService.list();
        //1.创建Request
        BulkRequest request = new BulkRequest();
        //2.准备参数,添加多个新增的Request
        for(Hotel hotel:hotels){
            // 2.1.转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.创建新增文档的Request对象
            request.add(new IndexRequest("hotels")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }
        //3.发送请求
        client.bulk(request,RequestOptions.DEFAULT);
    }

 小结:

文档操作的基本步骤:

  • 初始化RestHighLevelClient
  • 创建XxxRequest。XXX是Index、Get、Update、Delete、Bulk
  • 准备参数(Index、Update、Bulk时需要)
  • 发送请求。调用RestHighLevelClient#.xxx()方法,xxx是index、get、update、delete、bulk
  • 解析结果(Get时需要)

四、DSL查询文档

DSL查询分类

查询所有:查询出所有数据,一般测试用(不会显示出所有,自带分页功能)。例如:match_all

全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:

  • match_query:单字段查询
  • multi_match_query:多字段查询,任意一个字段符合条件就算符合查询条件

精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。

例如:

  • ids range根据值的范围查询
  • term根据词条精确值查询

 查询所有:

 @Test
    void testMatchAll() throws IOException{
        //1.准备Request
        SearchRequest request = new SearchRequest("hotels");
        // 2.准备DSL,QueryBuilders构造查询条件
        request.source().query(QueryBuilders.matchAllQuery());
        // 3.发送请求
        SearchResponse response=client.search(request,RequestOptions.DEFAULT);
        show(response);
    }

 单字段查询:

//查询all字段内容中有如家的(or拼接多条件)
    @Test
    void testMatch() throws IOException{
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotels");
        // 2.准备DSL 参数1:字段  参数2:数据
        request.source().query(QueryBuilders.matchQuery("all","如家"));
        //3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4.解析响应
        show(response);
    }

 多字段查询:

   //查询name,business字段内容中有如家的
    @Test
    void testMultiMatch() throws IOException{
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotels");
        // 2.准备DSL 参数1:字段  参数2:数据
        request.source().query(QueryBuilders.multiMatchQuery("如家","name","business"));
        //3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4.解析响应
        show(response);
    }

精确查询:

   //词条查询
    @Test
    void testTermQuery() throws IOException{
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotels");
        // 2.准备DSL,QueryBuilders构造查询条件
        request.source().query(QueryBuilders.termQuery("city","上海"));
        //3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        show(response);
    }

范围查询:

  //范围查询
    @Test
    void testRangeQuery() throws IOException{
        //1.准备Request
        SearchRequest request = new SearchRequest("hotels");
        //2.准备DSL,QueryBuilders构造查询条件
        request.source().query(QueryBuilders.rangeQuery("price").gte(160).lte(260));
        //3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        show(response);
    }

工具方法:分页功能和解析响应对象

    void testPageAndSort() throws IOException{
        //页码,每页大小
        int page=1,size=5;
        //查询条件
        String searchName="如家";
        //String searchName=null;

        //1.准备Request
        SearchRequest request = new SearchRequest("hotels");
        //2.准备DSL
        //2.1 query
        if (searchName==null){
            request.source().query(QueryBuilders.matchAllQuery());
        }else{
            request.source().query(QueryBuilders.matchQuery("name",searchName));
        }

        //2.2分页 from,size
        request.source().from((page-1)*size).size(size);
        //2.3 排序
        request.source().sort("price", SortOrder.DESC);

        //3 发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4 解析响应
        show(response);
    }

    //解析响应对象
    public void show(SearchResponse response){
        // 4.解析响应
        SearchHits searchHits = response.getHits();
        // 4.1.获取总条数
        long total = searchHits.getTotalHits().value;
        System.out.println("共搜索到" + total + "条数据");
        // 4.2.文档数组
        SearchHit[] hits = searchHits.getHits();
        // 4.3.遍历
        for (SearchHit hit : hits) {
            // 获取文档source
            String json = hit.getSourceAsString();
            // 反序列化
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            System.out.println("hotelDoc = " + hotelDoc);
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值