SpringBoot整合ES客户端操作

SpringBoot整合ES客户端操作

介绍ES

ES下载与安装

https://www.elastic.co/cn/downloads/past-releases

不要装太新的,里面自己配置了jdk,太新的可能用不了,免安装的,解压就好

浏览器输入:http://localhost:9200/

返回json,表示启动成功了:

ES索引操作

下载分词器

https://github.com/medcl/elasticsearch-analysis-ik

要注意分词器要和你的ES版本一致。

然后使用Apifox测试请求

put请求要带参数,把你要创建的数据传进去,使用json格式:

java 复制代码
{
    "mapping": {
        "properties": {
            "id": {
                "type": "string"
            },
            "name": {
                "type": "string",
                "analyzer": "string",
                "copy_to": "string"
            },
            "type": {
                "type": "string"
            },
            "description": {
                "type": "string",
                "analyzer": "string",
                "copy_to": "string"
            },
            "all": {
                "type": "string",
                "analyzer": "string"
            }
        }
    },
    "mappings": {
        "properties": {
            "id": {
                "type": "string"
            },
            "name": {
                "type": "string",
                "analyzer": "string",
                "copy_to": "string"
            },
            "type": {
                "type": "string"
            },
            "description": {
                "type": "string",
                "analyzer": "string",
                "copy_to": "string"
            },
            "all": {
                "type": "string",
                "analyzer": "string"
            }
        }
    }
}

ES文档操作

创建文档

java 复制代码
{
    "mappings":{
        "properties":{
            "id":{
                "type":"keyword"
            },
            "name":{
                "type":"text",
                "analyzer":"ik_max_word",
                "copy_to":"all"
            },
            "type":{
                "type":"keyword"
            },
            "description":{
                "type":"text",
                "analyzer":"ik_max_word",
                "copy_to":"all"
            },
             "all":{
                "type":"text",
                "analyzer":"ik_max_word"
            }
        }
    }
}
java 复制代码
{
    "id":2,
    "name":"springboot2",
    "type":"springboot2",
    "description":"springboot2"
}

查询文档

修改文档

java 复制代码
{
    "doc":{
        "name":"springboot2 888"
    }
}

SpringBoot整合ES客户端操作

导入坐标

但是我们不用springboot整合好的low-level的es

我们使用high lebel的es, 但是springboot没有整合,我们就得硬编码



java 复制代码
@SpringBootTest
class Springboot18EsApplicationTests {

//    @Autowired
//    private BookDao bookDao;

    //  这种是es低级别的,是spring已经整合的,es的高级springboot没整合
//    @Autowired
//    private ElasticsearchRestTemplate template;

    // 因为springboot没有整合highLevel,所以不能自动导入bean,我们得硬编码
    private RestHighLevelClient client;


    @BeforeEach
    void setUp() {
        HttpHost host = HttpHost.create("http://localhost:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();

    }

    // 创建客户端
//    @Test
//    void createClient() throws IOException {
//        HttpHost host = HttpHost.create("http:localhost:9200");
//        RestClientBuilder builder = RestClient.builder(host);
//        client = new RestHighLevelClient(builder);
//        client.close();
//    }


    // 创建索引
    @Test
    void createIndex() throws IOException {
//        HttpHost host = HttpHost.create("http://localhost:9200");
//        RestClientBuilder builder = RestClient.builder(host);
//        client = new RestHighLevelClient(builder);

        CreateIndexRequest request = new CreateIndexRequest("books");
        client.indices().create(request, RequestOptions.DEFAULT);

//        client.close();
    }

}

在客户端添加文档




java 复制代码
// 创建索引
    @Test
    void createIndexByIk() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("books");
        //  设置请求中的参数
        String json = "{\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" +
                "            \"type\":{\n" +
                "                \"type\":\"keyword\"\n" +
                "            },\n" +
                "            \"description\":{\n" +
                "                \"type\":\"text\",\n" +
                "                \"analyzer\":\"ik_max_word\",\n" +
                "                \"copy_to\":\"all\"\n" +
                "            },\n" +
                "             \"all\":{\n" +
                "                \"type\":\"text\",\n" +
                "                \"analyzer\":\"ik_max_word\"\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";
        request.source(json, XContentType.JSON);
        client.indices().create(request, RequestOptions.DEFAULT);

    }


    // 添加文档
    @Test
    void testCreateDoc() throws IOException {
        Book book = bookDao.selectById(1);
        IndexRequest request = new IndexRequest("books").id(book.getId().toString());
        String json = JSON.toJSONString(book);
        request.source(json,XContentType.JSON);
        client.index(request,RequestOptions.DEFAULT);

    }



    // 添加全文档
    @Test
    void testCreateDocAll() throws IOException {
        List<Book> bookList = bookDao.selectList(null);
        BulkRequest bulk = new BulkRequest();

        // 把所有请求整到bulk中
        for (Book book : bookList) {
            IndexRequest request = new IndexRequest("books").id(book.getId().toString());
            String json = JSON.toJSONString(book);
            request.source(json,XContentType.JSON);
            bulk.add(request);
        }

        // 全部加到索引中
        client.bulk(bulk,RequestOptions.DEFAULT);
    }

查询文档

按id查

java 复制代码
// 查询文档------按id查
    @Test
    void testGet() throws IOException {
        GetRequest request = new GetRequest("books","1");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String json = response.getSourceAsString();
        System.out.println(json);
    }

按条件查询文档

java 复制代码
// 查询文档------按条件查
    @Test
    void testSearch() throws IOException {
        SearchRequest request = new SearchRequest("books");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.termQuery("name","java"));
        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            String source = hit.getSourceAsString();
//            System.out.println(source);
            Book book = JSON.parseObject(source, Book.class);
            System.out.println(book);
        }
    }
相关推荐
淘淘 小窝3 小时前
springboot配置多数据源mysql+TDengine保姆级教程
spring boot·mysql·tdengine
柴米油盐那点事儿3 小时前
springboot引入kafka
spring boot·kafka
ᝰꫝꪉꪯꫀ3616 小时前
JavaWeb——Maven高级
java·后端·maven·springboot
独泪了无痕7 小时前
探索 IntelliJ IDEA 中 Spring Boot 运行配置
spring boot·intellij idea
小张帅三代7 小时前
【spark-spring boot】学习笔记
spring boot·学习·spark
camellias_8 小时前
SpringBoot(三十九)SpringBoot集成RabbitMQ实现流量削峰添谷
spring boot·rabbitmq·java-rabbitmq
2301_793086878 小时前
springboot+redis+lua脚本实现滑动窗口限流
spring boot·redis·lua
一只爱撸猫的程序猿9 小时前
简单解决一个同城多活中的机房之间数据同步的问题
spring boot·mysql·kafka
前端李易安9 小时前
Git中HEAD、工作树和索引的区别
git·elasticsearch·搜索引擎
轩情吖9 小时前
模拟实现Bash
linux·c语言·开发语言·c++·后端·bash·环境变量