草庐IT

十、SpringBoot与检索(Elasticsearch)

qq_19768387 2023-04-10 原文

检索组件elasticsearch

一、ElasticSearch简介

1.简介

Elasticsearch是一个分布式搜索服务,提供restful API 底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github等大型站点也是采用了Elasticsearch作为其搜索服务;

2.docker安装

  1. 搜索镜像
    docker search elasticsearch
    
  2. 拉取镜像
    docker pull elasticsearch:7.17.1
    
  3. 查看镜像
    docker images
    
  4. 启动镜像
     docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -e "discovery.type=single-node" -d -p 9200:9200 -p 9300:9300 --name ES01 515ab4fba870
    
  5. 验证启动成功
    访问:http://192.168.1.XXX:9200,返回以下JSON数据:
    {
      "name" : "5ab04951e8e0",
      "cluster_name" : "docker-cluster",
      "cluster_uuid" : "v_6fQnhsQ8i1S0faXaTo2A",
      "version" : {
        "number" : "7.17.1",
        "build_flavor" : "default",
        "build_type" : "docker",
        "build_hash" : "e5acb99f822233d62d6444ce45a4543dc1c8059a",
        "build_date" : "2022-02-23T22:20:54.153567231Z",
        "build_snapshot" : false,
        "lucene_version" : "8.11.1",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    

二、elasticsearch操作

具体操作参考:
elasticsearch官方文档【中文】
或者关注后续的elasticsearch学习文档

三、SpringBoot整合Elasticsearch

1.引入maven依赖

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
 </dependency>

2. 自动配置

  1. SpringBoot默认支持两种技术和ES进行交互;
    在SpringBoot1.X版本默认使用spring-data-elasticsearch和jest两种客户端和elasticsearch进行交互,但是随着jest在2018年停止维护,以及elasticsearch官方大力推荐RestHighLevel API对elasticsearch进行操作,spring官方在2.X版本移除了jest并加入了RestHighLevelClient;
  2. org.springframework.boot.autoconfigure在自动配置包下的data.elasticsearchelasticsearch下分别对应两种ES加护技术;
  3. org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration中引入了ElasticsearchRestTemplateElasticsearchRepository
  4. org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration引入RestHighLevelClient

3. yml配置文件

spring:
  elasticsearch:
    uris: http://192.168.1.132:9200

4.RestHighLevelClient操作elasticsearch

    @Autowired
    RestHighLevelClient restHighLevelClient;
    
    @Test
    void contextLoads() throws IOException {

        final GetRequest getRequest = new GetRequest();
        getRequest.id("1");
        getRequest.index("book");
        final GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        final String sourceAsString = documentFields.getSourceAsString();
        System.out.println("sourceAsString = " + sourceAsString);
    }

5. ElasticsearchRestTemplate 操作elasticsearch

    @Autowired
    ElasticsearchRestTemplate template;
    
    @Test
    void contextLoads3() throws IOException {
        final Book book = template.get("1", Book.class);
        System.out.println("book = " + book);
    }

6.ElasticsearchRepository操作elasticsearch

  1. 编写某个bean的关联ElasticsearchRepository
    自定义规则参考spring-data官方文档
public interface BookRepository extends ElasticsearchRepository<Book,Long> {

    List<Book> findByAuthorLike(String author);
}
  1. 操作方法
    @Autowired
    BookRepository repository;
        @Test
    void contextLoads() {
        /*Book book = new Book(1L,"红楼梦","曹雪芹");
        repository.save(book);*/

        for (Book book1 : repository.findAll()) {
            System.out.println("book1 = " + book1);
        }
    }

有关十、SpringBoot与检索(Elasticsearch)的更多相关文章

  1. ruby-on-rails - 使用 HTTP.get_response 检索 Facebook 访问 token 时出现 Rails EOF 错误 - 2

    我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token

  2. ruby - Rails Elasticsearch 聚合 - 2

    不知何故,我似乎无法获得包含我的聚合的响应...使用curl它按预期工作:HBZUMB01$curl-XPOST"http://localhost:9200/contents/_search"-d'{"size":0,"aggs":{"sport_count":{"value_count":{"field":"dwid"}}}}'我收到回复:{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":90,"max_score":0.0,"hits":[]},"a

  3. elasticsearch源码关于TransportSearchAction【阶段三】 - 2

    1.回顾.TransportServicepublicclassTransportServiceextendsAbstractLifecycleComponentTransportService:方法:1publicfinalTextendsTransportResponse>voidsendRequest(finalTransport.Connectionconnection,finalStringaction,finalTransportRequestrequest,finalTransportRequestOptionsoptions,TransportResponseHandlerT>

  4. ruby-on-rails - 使用 Rails (Tire) 和 ElasticSearch 进行模糊字符串匹配 - 2

    我有一个Rails应用程序,现在设置了ElasticSearch和Tiregem以在模型上进行搜索,我想知道我应该如何设置我的应用程序以对模型中的某些索引进行模糊字符串匹配。我将我的模型设置为索引标题、描述等内容,但我想对其中一些进行模糊字符串匹配,但我不确定在何处进行此操作。如果您想发表评论,我将在下面包含我的代码!谢谢!在Controller中:defsearch@resource=Resource.search(params[:q],:page=>(params[:page]||1),:per_page=>15,load:true)end在模型中:classResource'Us

  5. ruby-on-rails - 如何检索网站图标? - 2

    我正在使用RubyonRailsv3.0.9,我想检索我设置了链接的每个网站的favicon.ico图像。也就是说,如果在我的应用程序中我设置了http://www.facebook.com/URL,我想检索Facebook的图标并在我的网页中使用\插入它。当然,我也想为所有其他网站这样做。如何以“自动”方式从网站检索favicon.ico图标(“自动”是指在网站中搜索图标并获取它的链接-我认为不是,因为并非所有网站都有一个名为“favicon.ico”的图标。我想以“自动”方式识别它)?P.S.:我想做的是像Facebook在您的Facebook页面中添加链接\URL时所做的那样:它

  6. c - 我如何在 Ruby 的 C 扩展 API 上检索 'standalone' 符号 - 2

    我想从C函数返回多个值,恕我直言,散列是一个不错的选择。我首先使用rb_intern('A_KEY')创建key,但扩展崩溃了。现在,我正在使用rb_str_new2,但我更喜欢符号。如何创建一个新符号,并在不引用类或方法的情况下使用它? 最佳答案 您需要使用ID2SYM宏将从rb_intern获得的标识符转换为ruby​​符号。尝试改变rb_intern('A_KEY')到ID2SYM(rb_intern('A_KEY')) 关于c-我如何在Ruby的C扩展API上检索'standal

  7. 美团外卖搜索基于Elasticsearch的优化实践 - 2

    美团外卖搜索工程团队在Elasticsearch的优化实践中,基于Location-BasedService(LBS)业务场景对Elasticsearch的查询性能进行优化。该优化基于Run-LengthEncoding(RLE)设计了一款高效的倒排索引结构,使检索耗时(TP99)降低了84%。本文从问题分析、技术选型、优化方案等方面进行阐述,并给出最终灰度验证的结论。1.前言最近十年,Elasticsearch已经成为了最受欢迎的开源检索引擎,其作为离线数仓、近线检索、B端检索的经典基建,已沉淀了大量的实践案例及优化总结。然而在高并发、高可用、大数据量的C端场景,目前可参考的资料并不多。因此

  8. 【详解】Docker安装Elasticsearch7.16.1集群 - 2

    开门见山|拉取镜像dockerpullelasticsearch:7.16.1|配置存放的目录#存放配置文件的文件夹mkdir-p/opt/docker/elasticsearch/node-1/config#存放数据的文件夹mkdir-p/opt/docker/elasticsearch/node-1/data#存放运行日志的文件夹mkdir-p/opt/docker/elasticsearch/node-1/log#存放IK分词插件的文件夹mkdir-p/opt/docker/elasticsearch/node-1/plugins若你使用了moba,直接右键新建即可如上图所示依次类推创建

  9. 【Elasticsearch基础】Elasticsearch索引、文档以及映射操作详解 - 2

    文章目录概念索引相关操作创建索引更新副本查看索引删除索引索引的打开与关闭收缩索引索引别名查询索引别名文档相关操作新建文档查询文档更新文档删除文档映射相关操作查询文档映射创建静态映射创建索引并添加映射概念es中有三个概念要清楚,分别为索引、映射和文档(不用死记硬背,大概有个印象就可以)索引可理解为MySQL数据库;映射可理解为MySQL的表结构;文档可理解为MySQL表中的每行数据静态映射和动态映射上面已经介绍了,映射可理解为MySQL的表结构,在MySQL中,向表中插入数据是需要先创建表结构的;但在es中不必这样,可以直接插入文档,es可以根据插入的文档(数据),动态的创建映射(表结构),这就

  10. springboot定时任务 - 2

    如果您希望在Spring中启用定时任务功能,则需要在主类上添加 @EnableScheduling 注解。这样Spring才会扫描 @Scheduled 注解并执行定时任务。在大多数情况下,只需要在主类上添加 @EnableScheduling 注解即可,不需要在Service层或其他类中再次添加。以下是一个示例,演示如何在SpringBoot中启用定时任务功能:@SpringBootApplication@EnableSchedulingpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.ru

随机推荐