springboot检索之使用JestClient整合elasticsearch测试

1-新建工程

2-选中web,elasticsearch模块

springboot默认使用spring data elasticsearch模块进行操作

3-

springboot默认支持两种技术来和es交互

   Jest(默认不生效),需要导入jest的工具包(io.searchbox.client.JestClient)

   springdata elasticsearch

client节点信息  clusterNodes,clusterName

ElasticsearchTemplate操作elasticsearch

编写一个ElasticsearchRepository的子接口来操作elasticsearch

4-来到maven中央仓库 搜索jest

选择6版本的

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>6.3.0</version>
        </dependency>

修改pom.xml文件

5-编写application.properties

spring.elasticsearch.jest.uris=http://192.168.3.18:9200

6-启动,连接池连到9200端口

7-新建bean包 Article类

package com.example.springbootelasticsearch.bean;

import io.searchbox.annotations.JestId;

public class Article {
    
    @JestId   //标记主键
    private Integer id;
    
    private String author;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

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

    public String getContent() {
        return content;
    }

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

8-编写测试类

package com.example.springbootelasticsearch;

import com.example.springbootelasticsearch.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class SpringbootElasticsearchApplicationTests {

    @Autowired
    JestClient jestClient;

    @Test
    void contextLoads() {
        //给elasticsearch中索引(保存)一个文档
        Article article = new Article();
        article.setId(1);
        article.setTitle("好消息");
        article.setAuthor("zhangsan");
        article.setContent("hello     world");

        //构建一个索引功能,不能大小写混着写
        //Index index = new Index.Builder(article).index("testElasticSearch").type("news").id("id").build();
        //Index index = new Index.Builder(article).index("testElasticSearch").type("news").build();
        Index index = new Index.Builder(article).index("test").type("news").build();

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

    }

}

9-运行测试类

查看所有索引

查询结果

10-测试类中新增

    //测试搜索
    @Test
    public void search(){
        //查询表达式,修改属性值为article的属性content
        String queryString="{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"hello\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        //构建搜索功能
        Search search = new Search.Builder(queryString).addIndex("test").addType("news").build();

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

运行测试方法,打印出

2019-11-29 17:39:08.817  INFO 9940 --- [           main] .SpringbootElasticsearchApplicationTests : Started SpringbootElasticsearchApplicationTests in 2.741 seconds (JVM running for 4.002)
{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"test","_type":"news","_id":"1","_score":0.2876821,"_source":{"id":1,"author":"zhangsan","title":"好消息","content":"hello     world"}}]}}
2019-11-29 17:39:09.086  INFO 9940 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

 11-查看官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值