Elasticsearch中RestClient使用

这篇具有很好参考价值的文章主要介绍了Elasticsearch中RestClient使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

🍓 简介:java系列技术分享(👉持续更新中…🔥)
🍓 初衷:一起学习、一起进步、坚持不懈
🍓 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正🙏
🍓 希望这篇文章对你有所帮助,欢迎点赞 👍 收藏 ⭐留言 📝

🍓 更多文章请点击
Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据

简介及安装请查看这篇:Elasticsearch中倒排索引、分词器、DSL语法使用介绍

一、RestClient操作索引库

这些客户端的本质就是组装DSL语句,通过Http请求发送给ES,官方地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据

各种操作查看下方文档Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据

二、初始化JavaRestClient

具体使用还需查看对应文档,这里简单使用介绍,可能不全

2.1 引入依赖

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.12.1</version>
        </dependency>

2.2 初始化RestHighLevelClient

第一种

   @Bean
   public RestHighLevelClient restHighLevelClient(){
       return new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));
   }

第二种

spring:
  elasticsearch:
    rest:
      uris: localhost:9200

三、索引库操作

建议对应上篇中的DSL语句进行操作

3.1 创建

	@Autowired
	private RestHighLevelClient client;
	
	//创建索引库
	@Test
	public void testCreateHotelIndex() throws IOException {
	    //1.创建Request对象
	    CreateIndexRequest request=new CreateIndexRequest("hotel");
	    //2.请求参数,MAPPING_TEMPLATE是静态常量字符串,内容是创建索引库的DSL语句
	    request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
	    //3.发送请求
	   client.indices().create(request,RequestOptions.DEFAULT);
	}

3.2 删除

    //删除索引库
    @Test
    public void testDeleteHotelIndex() throws IOException {
        //创建Request对象
        DeleteIndexRequest request= new DeleteIndexRequest("hotel");
        //发送请求
        client.indices().delete(request,RequestOptions.DEFAULT);
    }

3.3 判断索引库是否存在

    @Test
    public void  testExistsHotelIndex() throws IOException {
        //创建request对象
        GetIndexRequest request= new GetIndexRequest("hotel");
        //发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        //输出
        System.out.println(exists ? "索引库已经存在":"索引库不存在");
    }

四、文档操作

4.1 新增文档

@Autowired
private RestHighLevelClient client;

@Test
public void testAddDocument() throws IOException {
    //1.根据id查询酒店数据
    Hotel hotel = service.getById(61073l);
    //2.转换为文档类型
    HotelDoc hotelDoc = new HotelDoc(hotel);
    //3.将HotelDoc转为json
    String json = JSON.toJSONString(hotelDoc);
    //4.准备request对象
    IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());

    //5.准备json文档
    request.source(json, XContentType.JSON);
    //6.发送请求
    client.index(request, RequestOptions.DEFAULT);
}

4.2 根据id查询数据

@Autowired
private RestHighLevelClient client;


@Test
public void testGetDocumentById() throws IOException {
    //1.准备request对象
    GetRequest request = new GetRequest("hotel" ,"61083");
    //2.发送请求,得到响应
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    //3.解析响应结果
    String json = response.getSourceAsString();
    HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
    System.out.println(hotelDoc);

}

4.3 根据id修改数据

@Autowired
private RestHighLevelClient client;


@Test
public void testUpdateDocument() throws IOException {
    //1.准备request
    UpdateRequest request=new UpdateRequest("hotel","61083");
    //2.准备请求参数
    request.doc(
            "price","987",
            "starName","四钻"
    );
    //3.发送请求
   

4.4 删除数据

@Autowired
private RestHighLevelClient client;


@Test
public void testDeleteDocument() throws IOException {
    //1.准备Request
    DeleteRequest request=new DeleteRequest("hotel","61083");
    //2.发送请求
    client.delete(request,RequestOptions.DEFAULT);
}

4.5 批量新增


@Autowired
private RestHighLevelClient client;


@Test
public void testBulkRequest() throws IOException {
    //批量查询酒店数据
    List<Hotel> hotels = service.list();

    //1.创建request对象
    BulkRequest request = new BulkRequest();
    //2.准备参数
    for (Hotel hotel : hotels) {
        //.转换文档类型
        HotelDoc doc = new HotelDoc(hotel);
        //.创建新增文档的request对象
        request.add(new IndexRequest("hotel")
                .id(hotel.getId().toString())
                .source(JSON.toJSONString(doc), XContentType.JSON));
    }
    //3.发送请求
    client.bulk(request,RequestOptions.DEFAULT);
}

五、DSL语法

个人介绍可能不太详细,查询及结果解析具体使用请查看下方文档
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据

六、拼音分词

想要实现如下,根据拼音也能查到对应数据 那么需要安装拼音分词器

Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据

6.1 安装

可以在该文档下载拼音分词器或者在我的资源库进行下载

  1. 根据第一篇的Elasticsearch简介及安装安装我们知道,我是通过docker安装,挂载有数据卷,那么首先查看安装位置

    docker volume inspect es-plugins
    

    Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据找到对应位置进行安装
    Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据

  2. 重启容器

    	# 4、重启容器
    	docker restart es
    
  3. 测试
    Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据
    Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据Elasticsearch中RestClient使用,Elasticsearch,elasticsearch,jenkins,大数据文章来源地址https://www.toymoban.com/news/detail-694084.html

到了这里,关于Elasticsearch中RestClient使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringCloud(十)——ElasticSearch简单了解(一)初识ElasticSearch和RestClient

    Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsea

    2024年02月10日
    浏览(28)
  • Elasticsearch --- RestAPI、RestClient操作文档

    ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:Elasticsearch Clients | Elastic 其中的Java Rest Client又包括两种:   创建数据库 创建项目 mapping映射分析 创建索引库,最关键的是mapping映射,而mapping映射要

    2024年02月07日
    浏览(33)
  • ElasticSearch索引库、文档、RestClient操作

    es中的索引是指相同类型的文档集合 ,即mysql中表的概念 映射:索引中文档字段的约束,比如名称、类型 mapping映射是对索引库中文档的约束。类似mysql对表单字段的约束 type :字段数据类型,常见的类型有: 字符串:text(可分词的文本)、keyword(不可分词的文本,例如国家

    2024年02月10日
    浏览(34)
  • 17、全文检索 -- Elasticsearch -- 使用 反应式 RestClient (ReactiveElasticsearchClient)操作 Es 服务器(增、删、查 :索引库和文档)

    Elasticsearch 所提供 RestHighLevelClient 本身提供了 【同步编程】 和 【异步编程】两种模型。 Elasticsearch 官方并未提供反应式的 RestClient : 因此 Spring Data Elasticsearch 额外补充了一个 ReactiveElasticsearchClient,用于提供反应式API支持, ReactiveElasticsearchClient 相当于 RestHighLevelClient 的反应式

    2024年04月28日
    浏览(36)
  • SpringCloud:ElasticSearch之RestClient查询文档

    文档的查询同样适用 RestHighLevelClient 对象,基本步骤包括: 1)准备 Request 对象 2)准备请求参数 3)发起请求 4)解析响应 我们以 match_all 查询为例 1.1.发起查询请求 代码解读: 第一步,创建 SearchRequest 对象,指定索引库名 第二步,利用 request.source() 构建DSL,DSL中可以包含查

    2024年02月03日
    浏览(33)
  • Elasticsearch常用查询语法及RestClient操作

    match查询 :全文检索查询的一种,会对用户内容分词,然后去倒排索引库检索,语法。 multi_match :和match类似,但是它允许多个字段进行查询 解释:在 hotel索引库 中按照 address 和 name 两个字段搜索值 包含北京 的文档 精准查询一般是查找keyword,数值,日期,boolean等 不可分

    2024年04月25日
    浏览(42)
  • ElasticSearch之RestClient操作索引库和文档

    前言:上文介绍了使用DSL语言操作索引库和文档,本篇文章将介绍使用Java中的RestClient来对索引库和文档进行操作。 希望能够加深自己的印象以及帮助到其他的小伙伴儿们😉😉。 如果文章有什么需要改进的地方还请大佬不吝赐教👏👏。 小威在此先感谢各位大佬啦~~🤞🤞

    2024年02月06日
    浏览(25)
  • Elasticsearch的DSL和在RestClient中的应用

      Elasticsearch提供了基于JSON的DSL来定义查询。常见的查询类型包括: 查询所有 :查询出所有数据,一般测试用。例如: match_all:匹配所有文档并返回它们; 全文检索(full text)查询 :利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如: match_query:针对单个

    2023年04月27日
    浏览(29)
  • 微服务学习|初识elasticsearch、操作索引库、文档操作、RestClient操作索引库、RestClient操作文档

    elasticsearch是一款非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容。 elasticsearch结合kibana、Logstash、Beats,也就是elastic stack (ELK)。被广泛应用在日志数据分析、实时监控等领域 elasticsearch是elastic stack的核心,负责存储、搜索、分析数据 Lucene是一个jav

    2024年01月18日
    浏览(37)
  • Elasticsearch --- DSL、RestClient查询文档、搜索结果处理

    elasticsearch的查询依然是基于JSON风格的DSL来实现的。   Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括: 查询所有 :查询出所有数据,一般测试用。例如:match_all 全文检索(full text)查询 :利用分词器对用户输入内容分词,然后去倒排

    2024年02月05日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包