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);
        }
    }
相关推荐
想太多会累i32 分钟前
Spring Boot 使用Itext绘制并导出PDF
spring boot·pdf
jiedaodezhuti5 小时前
ElasticSearch重启之后shard未分配问题的解决
笔记·elasticsearch
jiedaodezhuti5 小时前
为什么elasticsearch配置文件JVM配置31G最佳
大数据·jvm·elasticsearch
松树戈6 小时前
plus-ui&RuoYi-Vue-Plus 基于pgSql本地运行实践
前端·vue.js·spring boot·ui
white.tie6 小时前
Docker部署单节点Elasticsearch
elasticsearch·docker·jenkins
胡小禾7 小时前
ES常识7:ES8.X集群允许4个 master 节点吗
大数据·elasticsearch·搜索引擎
TinpeaV7 小时前
websocket入门详解
java·网络·spring boot·websocket·网络协议
-曾牛7 小时前
基于微信小程序的在线聊天功能实现:WebSocket通信实战
前端·后端·websocket·网络协议·微信小程序·小程序·notepad++
wxin_VXbishe8 小时前
springboot旅游小程序-计算机毕业设计源码76696
java·spring boot·python·spring·django·sqlite·flask
胡小禾9 小时前
ES常识8:ES8.X如何实现热词统计
大数据·elasticsearch·jenkins