您当前的位置: 首页 >  ar

止步前行

暂无认证

  • 3浏览

    0关注

    247博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot整合使用ElasticSearch

止步前行 发布时间:2018-12-16 17:02:36 ,浏览量:3

文章目录
  • 1. 创建SpringBoot工程
    • 1.1 选择ElasticSearch
    • 1.2 配置ElasticSearch
    • 1.3 小节
  • 2. 测试ElasticSearch
    • 2.1 使用Jest操作ElasticSearch
    • 2.2 使用SpringData操作ElasticSearch
  • 3. 小结

1. 创建SpringBoot工程 1.1 选择ElasticSearch

创建SpringBoot工程,选择ElasticSearch 在这里插入图片描述

1.2 配置ElasticSearch

application.properties文件中配置ElasticSearch

注意:在构建完工程后,打开项目的pom.xml文件,可以发现有如下的依赖。因此,可以看出SpringBoot默认使用的是SpringDataElasticSearch模块进行操作的。可以进入来确认一下,打开SpringBootautoConfigure包,看一下ElasticSearch的自动配置,如下图。


    org.springframework.boot
    spring-boot-starter-data-elasticsearch

在这里插入图片描述

1.3 小节

SpringBoot默认支持两种技术来和ElasticSearch交互:

  • Jest(默认不生效),需要导入jest的工具包(io.searchbox.client.JestClient)
  • SpringDataElasticSearch模块(默认方式)
2. 测试ElasticSearch 2.1 使用Jest操作ElasticSearch
  • Maven中央仓库搜索Jest依赖,本文选择如下版本


    io.searchbox
    jest
    5.3.3

  • application.properties配置文件中,做如下配置
spring.elasticsearch.jest.uris=http://192.168.1.108:9200
  • 编写测试代码,其中Article类的id属性要加@JestId注解,如下
public class Article {

    @JestId
    private int id;
		......
}

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

    @Autowired
    JestClient jestClient;

    @Test
    public void contextLoads() {

        Article article = new Article(1, "西游记", "吴承恩", "师徒四人西天取经");

        //构建一个索引功能:在scorpios索引下,类型为article
        Index build = new Index.Builder(article).index("scorpios").type("article").build();

        try {
            jestClient.execute(build);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  }
  • 测试结果

    在这里插入图片描述

在浏览器中输入如下地址,可以查看存入ElasticSearch中的数据:http://192.168.1.108:9200/scorpios/article/1

在这里插入图片描述

  • ElasticSearch中获取数据的测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {

    @Autowired
    JestClient jestClient;

     @Test
    public void testSearch(){
	//查询条件可参考ElasticSearch的官方文档
        String json ="{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"取经\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        //构建搜索
        Search build = new Search.Builder(json).addIndex("scorpios").addType("article").build();

        try {
            SearchResult result = jestClient.execute(build);
            System.out.println("搜索结果:"+result.getJsonString());
            System.out.println("搜索结果:"+result.getSourceAsString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 测试结果

    在这里插入图片描述

2.2 使用SpringData操作ElasticSearch
  • 打开pom.xml中的依赖
  • application.properties配置文件中添加如下配置:
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.1.108:9200
  • 编写BookRepository接口
public interface BookRepository extends ElasticsearchRepository {
}
  • 编写MyBook类,注意@Document注解
@Document(indexName ="scorpios",type = "book")
public class MyBook {

    private Integer id;
    private String name;
    private String author;
    ......
}
  • 测试代码:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {

    @Autowired
    BookRepository bookRepository;
    
    @Test
    public void test02(){
        MyBook myBook = new MyBook(1, "红楼梦", "曹雪芹");
        bookRepository.index(myBook);
    }
}
  • 搜索代码
public interface BookRepository extends ElasticsearchRepository {
	//只写方法声明,不用实现
	public List findByBookNameLike(String bookName);
}
  • 使用
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests {

    @Autowired
    BookRepository bookRepository;
    
    @Test
    public void test02(){
      for(MyBook book:bookRepository.findByBookNameLike("西游记")){
			System.out.println(book);
		}
    }
}
3. 小结

本文使用了两种方式来操作ElasticSearchSpringBoot默认使用的是SpringData里的模块。上面只是简单的使用,具体深入的学习,可以参考ElasticSearchd的官方文档。

https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

关注
打赏
1657848381
查看更多评论
立即登录/注册

微信扫码登录

0.0410s