SpringBoot整合ElasticSearch:基于Jest技术

1.给pom.xml添加依赖

<!--SpringBoot默认使用SpringData ElasticSearch模块进行操作
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>-->
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.3</version>
</dependency>

SpringBoot默认使用两种技术来和ES交互

1、Jest(默认不生效)

  需要导入jest的工具包(io.searchbox.client.JestClient)

2、SpringData ElasticSearch

  1)、Client 节点 :配置clusterNodes、clusterName

  2)、ElasticSearchTemplate 操作es

  3)、编写ElasticSearchRepository的子接口来操作es

2.配置ElasticSearch

spring.elasticsearch.jest.uris=http://tanghu.tk:9200

ElasticSearch的默认url为:localhost:9200

3.新建一个JavaBean

package com.fdzang.mblog.pojo.es;

import io.searchbox.annotations.JestId;

public class EsBlog {
    @JestId  // 主键
    private String id;
    private Long blogId; 
    private String title;
    private String summary;
    private String content;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getBlogId() {
        return blogId;
    }

    public void setBlogId(Long blogId) {
        this.blogId = blogId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

4.Test类进行测试

package com.fdzang.mblog;

import com.fdzang.mblog.pojo.es.EsBlog;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MblogApplicationTests {

    @Autowired
    JestClient jestClient;

    @Test
    public void contextLoads() {
        //1.给es中索引(保存)一个文档
        EsBlog esBlog=new EsBlog();
        esBlog.setBlogId(10001l);
        esBlog.setId("10001");
        esBlog.setContent("content");
        esBlog.setSummary("summary");
        esBlog.setTitle("title");
        //构建一个索引功能
        Index index=new Index.Builder(esBlog).index("thblog").type("blog").build();

        try {
            //执行
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

成功!

5.执行搜索功能

    @Test
    public void testSearch(){
        //查询表达式
        String json="{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"title\" : \"tle\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        //构建搜索功能
        Search search=new Search.Builder(json).addIndex("thblog").addType("blog").build();

        try {
            SearchResult result=jestClient.execute(search);
            System.out.println(result.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

得到结果集:

github文档地址:https://github.com/searchbox-io/Jest/tree/master/jest

posted @ 2018-09-14 01:07  市井俗人  阅读(8625)  评论(0编辑  收藏  举报