SpringBoot整合ES客户端操作

SpringBoot整合ES客户端操作

介绍ES

SpringBoot整合ES客户端操作_第1张图片

ES下载与安装

https://www.elastic.co/cn/downloads/past-releases
不要装太新的,里面自己配置了jdk,太新的可能用不了,免安装的,解压就好
SpringBoot整合ES客户端操作_第2张图片
浏览器输入:http://localhost:9200/
返回json,表示启动成功了:
SpringBoot整合ES客户端操作_第3张图片

ES索引操作

下载分词器

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

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

然后使用Apifox测试请求

SpringBoot整合ES客户端操作_第4张图片
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文档操作

创建文档

SpringBoot整合ES客户端操作_第5张图片

{
    "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"
}

查询文档

SpringBoot整合ES客户端操作_第6张图片

修改文档

SpringBoot整合ES客户端操作_第7张图片

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

SpringBoot整合ES客户端操作

导入坐标

SpringBoot整合ES客户端操作_第8张图片
但是我们不用springboot整合好的low-level的es
我们使用high lebel的es, 但是springboot没有整合,我们就得硬编码
SpringBoot整合ES客户端操作_第9张图片
SpringBoot整合ES客户端操作_第10张图片
SpringBoot整合ES客户端操作_第11张图片
SpringBoot整合ES客户端操作_第12张图片

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

}

在客户端添加文档

SpringBoot整合ES客户端操作_第13张图片
SpringBoot整合ES客户端操作_第14张图片
SpringBoot整合ES客户端操作_第15张图片
SpringBoot整合ES客户端操作_第16张图片

// 创建索引
    @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);
    }

查询文档

SpringBoot整合ES客户端操作_第17张图片

按id查

SpringBoot整合ES客户端操作_第18张图片

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

按条件查询文档

SpringBoot整合ES客户端操作_第19张图片

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

你可能感兴趣的:(spring,boot,elasticsearch,后端)