Elasticsearch与自己的代码怎么集成?

ElasticSearch–代码集成

Elasticsearch与自己的代码怎么集成?
image-20210329135917531

代码集成

SpringBoot 默认支持两种技术来与ES交互

Jest 此种比较方便,否则还得考虑版本匹配

默认不生效

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

SpringData ElasticSearch

==注意:ES版本有可能不合适==

==解决办法:==

==1、升级或降级SpringBoot版本==

==2、安装对应版本的es==

实际应用过程中SpringBoot的版本是2.1.9,spring-data-elasticsearch对应的版本是3.1.11,elasticsearch对应为6.2.2,但是实际上已经很难找到6.2.2的镜像,最后我抱着试一试的想法,下载了6.2.4,发现果然可以。

6.2.4的镜像路径为:Docker pull docker.elastic.co/elasticsearch/elasticsearch:6.2.4

详见elastic官网:https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docker.html

1、TransportClient  spring.data.elasticsearch cluster-nodes  clusterName

2、ElasticSearchTemplate

3、编写一个ElasticsearchRespository的子接口来操作ES。

spring:
  data:
    elasticsearch:
      cluster-name: docker-cluster
      cluster-nodes: 192.168.1.103:9301
//新建Repository接口
public interface BookRepository extends ElasticsearchRepository<Book,Integer{
}

//创建实体类 @Document为SpringDataElasticSearch对应的配置,相应的index和type
@Data
@Document(indexName = "test",type = "book")
public class Book {
    private Integer id;
    private String bookname;
    private String author;
}

//测试类
public class EsTestRepository {
    @Autowired
    BookRepository bookRepository;
    @Test
    public void contextLoads(){
        Book book = new Book();
        book.setId(1);
        book.setAuthor("李四");
        book.setBookname("西游记");
        bookRepository.index(book);
    }
}

附上一些Repository的关键字:(需要好好理解SpringData Repository)

关键字 使用示例 等同于的ES查询
And findByNameAndPrice {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Or findByNameOrPrice {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Is findByName {“bool” : {“must” : {“field” : {“name” : “?”}}}}
Not findByNameNot {“bool” : {“must_not” : {“field” : {“name” : “?”}}}}
Between findByPriceBetween {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
LessThanEqual findByPriceLessThan {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
GreaterThanEqual findByPriceGreaterThan {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Before findByPriceBefore {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
After findByPriceAfter {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Like findByNameLike {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
StartingWith findByNameStartingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
EndingWith findByNameEndingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}}
Contains/Containing findByNameContaining {“bool” : {“must” : {“field” : {“name” : {“query” : “?”,”analyze_wildcard” : true}}}}}
In findByNameIn(Collectionnames) {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}}
NotIn findByNameNotIn(Collectionnames) {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}}
True findByAvailableTrue {“bool” : {“must” : {“field” : {“available” : true}}}}
False findByAvailableFalse {“bool” : {“must” : {“field” : {“available” : false}}}}
OrderBy findByAvailableTrueOrderByNameDesc {“sort” : [{ “name” : {“order” : “desc”} }],”bool” : {“must” : {“field” : {“available” : true}}}}

ES官方推荐 RestHighLevelClient

源码:https://gitee.com/zxporz/ESClientRHL

ESClientRHL便是一个实践

选择ESClientRHL原因

  • 目前spring-data-elasticsearch底层采用es官方TransportClient,而es官方计划放弃TransportClient,工具以es官方推荐的RestHighLevelClient进行封装
  • spring-data-elasticsearch支持的api有限,而EsClientRHL支持更丰富的api调用
  • 能够极大简化Java client API,并不断更新,让es更高级的功能更轻松的使用
  • 支持两种自动化的功能,减轻开发者工作量,使其更专注于业务开发
  1. 支持启动自动扫描elasticsearch索引实体类,并为没有索引结构的实体自动创建索引结构
  2. 支持开发者只定义一个接口,就拥有了常用与es交互的黑魔法
  • 组件中包含了:es索引数据增删改、es查询、es数据分析等丰富的API工具,开发者可以通过EsClientRHL来参考在java中如何与elasticsearch进行各种交互
  • EsClientRHL中部分API结合了实际场景中最佳实践的使用方法
  • 总之ESClientRHL能给您带来帮助,那它就有存在的价值,如果对您有些许帮助,请不吝Star https://gitee.com/zxporz/ESClientRHL
docker pull elasticsearch:7.3.0

– END –


原文始发于微信公众号(云户):Elasticsearch与自己的代码怎么集成?

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/35321.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!