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);
        }
    }
相关推荐
2401_857622666 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589366 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
hengzhepa7 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
哎呦没7 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
_.Switch8 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
编程、小哥哥8 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程9 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
杨哥带你写代码9 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
AskHarries10 小时前
读《show your work》的一点感悟
后端
A尘埃10 小时前
SpringBoot的数据访问
java·spring boot·后端