关于ElasticSearch整合SpringBoot

首先导入需要的依赖:这里要注意导入的Jest版本号和你的elasticsearch版本号在同一大版本,我的ES是6.5.4,所以这里我用的6版本的jest

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 镜像ID
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
     <groupId>io.searchbox</groupId>
     <artifactId>jest</artifactId>
     <version>6.3.1</version>
</dependency>

安装ES的时候,注意内存不足的问题,之前在云主机上安装,多次因为内存分配问题,一直给我报错,气煞我也。
不熟悉ES语法的可以参照这里中文文档操作

写一个测试用例:
添加


@Autowired
   private JestClient jestClient;
 @Test
    public void Insert() {

        Article article = new Article();
        article.setId(1);
        article.setAuthor("老舍");
        article.setTitle("家,春,秋");
        article.setContent("hello elasticsearch");
        Index index = new Index.Builder(article).index("mix").type("book").build();
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public class Article {

    @JestId
    private Integer id;
    private String author;
    private String title;
    private String content;
    //省略get set方法
}

访问:http://38.108.101.225:9200/mix/book/1 就会看到json字符串

如果是查找:

 @Test
    public void search(){

        String json = "";
        Search build = new Search.Builder(json).addIndex("mix").addType("book").build();
        try {
            SearchResult execute = jestClient.execute(build);
            System.out.println(execute.getJsonString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值