SpringBoot整合ES客户端操作

本文详细介绍了如何在SpringBoot项目中整合Elasticsearch(ES)客户端,包括ES的下载与安装、索引和文档操作,以及如何使用HighLevelClient进行高阶操作,如创建索引、添加文档和查询等。
摘要由CSDN通过智能技术生成

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格式:

{
    "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文档操作

创建文档

在这里插入图片描述

{
    "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"
            }
        }
    }
}
{
    "id":2,
    "name":"springboot2",
    "type":"springboot2",
    "description":"springboot2"
}

查询文档

在这里插入图片描述

修改文档

在这里插入图片描述

{
    "doc":{
        "name":"springboot2 888"
    }
}

SpringBoot整合ES客户端操作

导入坐标

在这里插入图片描述
但是我们不用springboot整合好的low-level的es
我们使用high lebel的es, 但是springboot没有整合,我们就得硬编码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

}

在客户端添加文档

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

// 创建索引
    @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查

在这里插入图片描述

// 查询文档——按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);
    }

按条件查询文档

在这里插入图片描述

// 查询文档——按条件查
    @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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值