spring boot学习第八篇:操作elastic search的索引和索引中的数据

前提参考:elastic search入门-CSDN博客

前提说明:已经安装好了elastic search 7.x版本,我的es版本是7.11.1

1、 pom.xml文件内容如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hmblogs
    hmblogs
    0.0.1-SNAPSHOT
    hmblogs
    hmblogs
    
        8
        1.2.8
        1.16
        7.9.2
    
    
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.version}
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.3.1
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
        

        
            org.projectlombok
            lombok
            true
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.bgee.log4jdbc-log4j2
            log4jdbc-log4j2-jdbc4.1
            ${log4jdbc.version}
        

        
            com.alibaba
            fastjson
            1.2.9
        

        
            redis.clients
            jedis
        

        
            org.apache.kafka
            kafka-clients

        

        
            org.springframework.kafka
            spring-kafka

        

        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${es.version}
            
                
                    org.elasticsearch
                    elasticsearch
                
                
                    org.elasticsearch.client
                    elasticsearch-rest-client
                
            
        

        
        
            org.elasticsearch
            elasticsearch
            ${es.version}
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            ${es.version}
        
        
            junit
            junit
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、application.yml文件内容如下:

server:
  port: 8081
  servlet.context-path: /

#配置数据源
spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
      url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
      username: ${DB_USER:root}
      password: ${DB_PWD:123456}
  redis:
    host: localhost
    port: 6379
    password: heming
    database: 10

es:
  host: 43.138.0.199
  port: 9200
  scheme: http

3、BackendApplication.java文件内容如下:

package com.hmblogs.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }

}

4、测试验证,ElasticsearchClientTest.java文件内容如下:


import com.alibaba.fastjson.JSONObject;
import com.hmblogs.backend.entity.Users;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
//@Component
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchClientTest {
    @Autowired
    private RestHighLevelClient client;

    String index = "users";

    /**
     * 创建索引
     *
     * @throws IOException
     */
    @Test
    public void createIndex() throws IOException {
        CreateIndexRequest indexRequest = new CreateIndexRequest(index);
        CreateIndexResponse response = client.indices()
                .create(indexRequest, RequestOptions.DEFAULT);
        log.info("创建索引:"+response.isAcknowledged());
    }

    /**
     * 判断索引是否存在
     *
     * @throws IOException
     */
    @Test
    public void indexExists() throws IOException {
        GetIndexRequest request = new GetIndexRequest(index);
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        log.info("索引是否存在:"+exists);
    }

    /**
     * 添加文档
     *
     * @throws IOException
     */
    @Test
    public void addDoc() throws IOException {
        IndexRequest request = new IndexRequest(index);
        String source = JSONObject.toJSONString(new Users(10000, "逍遥", 30));
        // 手动设置id
//        request.id("10000");
        request.source(source, XContentType.JSON);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        log.info("添加文档:"+response.getResult());
    }


    /**
     * 批量添加文档
     */
    @Test
    public void batchAddDoc() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        List requests = generateRequests();
        for (IndexRequest indexRequest : requests) {
            bulkRequest.add(indexRequest);
        }
        BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("批量添加结果:"+!responses.hasFailures());
    }

    public List generateRequests() {
        List requests = new ArrayList<>();
        requests.add(generateNewsRequest(1, "小明", 22));
        requests.add(generateNewsRequest(2, "隔壁老王", 30));
        requests.add(generateNewsRequest(3, "lily", 25));
        return requests;
    }

    public IndexRequest generateNewsRequest(Integer id, String name, Integer age) {
        IndexRequest indexRequest = new IndexRequest(index);
        String source = JSONObject.toJSONString(new Users(id, name, age));
        indexRequest.source(source, XContentType.JSON);
        return indexRequest;
    }

    /**
     * 更新文档
     *
     * @throws IOException
     */
    @Test
    public void updateDoc() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest(index, "AmxCP40BM8-M0To1vOET");
        Map params = new HashMap<>();
        params.put("id", "1");
        params.put("name", "逍遥");
        params.put("age", 33);
        params.put("hobby", "唱歌,跳舞,网上冲浪,看电影,旅行");
        updateRequest.doc(params);
        UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);
        log.info("修改文档结果:"+response.getResult());
    }

    /**
     * 搜索
     *
     * @throws IOException
     */
    @Test
    public void search() throws IOException {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder
                .must(new RangeQueryBuilder("age").from(20).to(30))
                .must(new TermQueryBuilder("id", 3));
        builder.query(boolQueryBuilder);
        request.source(builder);
        log.info("搜索语句为: " + request.source().toString());
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);
        log.info("搜索结果为:"+search);
        SearchHits hits = search.getHits();
        SearchHit[] hitsArr = hits.getHits();
        for (SearchHit documentFields : hitsArr) {
            log.info(documentFields.getSourceAsString());
        }
    }


    @Test
    public void search2() {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.from(0);
        sourceBuilder.size(10);
        sourceBuilder.fetchSource(new String[]{"name", "age"}, new String[]{});
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("hobby", "唱歌,跳舞,网上冲浪,看电影,旅行");
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "逍遥");
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        rangeQueryBuilder.gte(20);
        rangeQueryBuilder.lte(40);
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        boolBuilder.must(matchQueryBuilder);
        boolBuilder.must(termQueryBuilder);
        boolBuilder.must(rangeQueryBuilder);
        sourceBuilder.query(boolBuilder);
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.source(sourceBuilder);
        try {
            log.info("搜索语句为: " + searchRequest.source().toString());
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            log.info("搜索结果为:"+search);
            SearchHits hits = search.getHits();
            SearchHit[] hitsArr = hits.getHits();
            for (SearchHit documentFields : hitsArr) {
                log.info(documentFields.getSourceAsString());
            }
        } catch (IOException e) {
            log.error("搜索报错:{}",e);
        }
    }

    /**
     * 删除文档
     * @throws IOException
     */
    @Test
    public void deleteDoc() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest(index, "3g8vP40Bi8WQ8ue06wWd");
        DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
        log.info("删除结果为:"+response.getResult());
    }

    /**
     * 删除索引
     * @throws IOException
     */
    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest(index);
        AcknowledgedResponse response = client.indices()
                .delete(request, RequestOptions.DEFAULT);
        log.info("删除索引结果为:"+response.isAcknowledged());
    }

}

每个方法的作用见注释说明,

spring boot学习第八篇:操作elastic search的索引和索引中的数据_第1张图片

 这里点击Run search2()

然后查看console的log,验证OK

spring boot学习第八篇:操作elastic search的索引和索引中的数据_第2张图片

这样写代码,还是不够灵活,能不能执行自定义的SQL或者DSL语句,这样就可以不用去想办法看下Builder那些类对象怎么设置了。

其实,要想实现这样的效果,就是调用http接口而已

RestTemplateUtil.java文件内容如下:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class RestTemplateUtil {

    @Autowired
    private RestTemplate restTemplate;

    public String post(String url,Map map){
        // 设置请求头属性
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(map, httpHeaders);
        String results = restTemplate.postForObject(url, httpEntity, String.class);
        return results;
    }
}

ElasticsearchClientSqlDslTest.java类文件内容如下:


import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

@Slf4j
//@Component
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchClientSqlDslTest {

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Test
    public void complexQueryEsData() {
        // 请求参数
        Map map = new HashMap<>();
        map.put("query", "SELECT id,name,age FROM users order by name desc limit 6");
        String url = "http://43.138.0.199:9200/_sql?format=json";
        String studentData = restTemplateUtil.post(url, map);
        log.info("studentData:"+studentData);
    }
}

执行后,查出了数据,console如下截图:

spring boot学习第八篇:操作elastic search的索引和索引中的数据_第3张图片

返回数据如下:

{"columns":[{"name":"id","type":"long"},{"name":"name","type":"text"},{"name":"age","type":"long"}],"rows":[[2,"隔壁老王",30],[1,"逍遥",33],[1,"小明",22],[3,"lily",25]]}

 

你可能感兴趣的:(Spring,Boot,spring,boot,java,elasticsearch)