国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序)

這篇具有很好參考價值的文章主要介紹了微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

索引庫操作

mapping屬性

mapping是對文檔的約束,常見約束屬性包括:

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

創(chuàng)建索引庫

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

#創(chuàng)建索引庫
PUT /heima
{
  "mappings": {
    "properties": {
      "info":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email":{
        "type": "keyword",
        "index": false
      },
      "name":{
        "type": "object",
        "properties": {
          "firstName":{
            "type":"keyword"
          },
          "lastName":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

?如下所示,成功創(chuàng)建一個索引庫微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

查詢,刪除,修改索引庫

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?在es中不允許修改索引庫,但可以添加新的字段

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

#添加新字段
PUT /heima/_mapping  
{
  "properties":{
    "age":{
      "type":"integer"
    }
  }
}

然后重新獲取就有新的字段出現(xiàn)了

文檔操作

新增文檔

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

在索引庫插入一個規(guī)定格式的數(shù)據就像在一個數(shù)據庫表里面插入一條數(shù)據。?

POST /heima/_doc/1
{
  "info":"北嶺山腳鼠鼠",
  "email":"yhy@beiling.cn",
  "name":{
    "firstName":"云",
    "lastName":"趙"
  }
}

查詢和刪除

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

修改文檔

有兩種修改方式——全量修改

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?全量修改與新增文檔幾乎一模一樣就是方法名不同?

#全量修改文檔

PUT /heima/_doc/1
{
  "info":"北嶺鼠鼠",
  "email":"yhy@beiling.cn",
  "name":{
    "firstName":"云",
    "lastName":"趙"
  }
}
#局部修改文檔字段
POST /heima/_doc/1
{
  "doc":{
    "email":"123@123.cn"
  }
}

RestClient操作索引庫(java)

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

入門案例

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

導入Demo

創(chuàng)建一個名為heima的數(shù)據庫運行給好的SQL文件就可以得到如下。

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?然后導入準備好的項目

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?分析數(shù)據結構

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?id屬性較為特殊,在索引庫里面是字符串,并且不分詞,所以用的是keyword類型

address不參與搜索,選擇index置為false.

starName使用駝峰命名法。

地理坐標較為特殊,由兩個字段組成,使用一個location字段表示

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?有多個字段同時參與搜索,但是通常一個字段的查詢效率比較高,解決方法如下,用一個all字段包含多個字段就可以根據其中一個字段搜索到多個字段的內容了。并且all字段不參與倒排索引

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

定義對應的Mapping映射

PUT /hotel
{
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word"
        , "copy_to": "all"
      },
      "address":{
        "type":"keyword",
        "index": false
      },
      "price":{
        "type":"integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type": "keyword"        
      },
      "starName":{
        "type": "keyword"
      },
      "business":{
        "type": "keyword",
        "copy_to": "all"
      },
      "location":{
        "type":"geo_point"
      },
      "pic":{
        "type":"keyword",
        "index": false
      },
      "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

初始化JavaRestClient

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

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

不指定版本的話里面有子依賴會用默認7.6版本。

創(chuàng)建新的測試類

使用Before注解在最開始創(chuàng)建,之后可以直接使用。

public class HotelIndexTest {
    private RestHighLevelClient client;
    @Test
    void testInit(){
        System.out.println(client);
    }
    
    //執(zhí)行前初始化
    @BeforeEach
    void setUp(){
        this.client=new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
              //HttpHost.create("http://xxx.xxx.xxx.xxx:9200") 集群時添加更多的地址
        ));
    }

    //銷毀
    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}

創(chuàng)建索引庫

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?準備一個實體對象靜態(tài)屬性

public class HotelConstants {
    public static final String MAPPING_TEMPLATE="{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "        , \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"address\":{\n" +
            "        \"type\":\"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"price\":{\n" +
            "        \"type\":\"integer\"\n" +
            "      },\n" +
            "      \"score\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"brand\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"city\":{\n" +
            "        \"type\": \"keyword\"        \n" +
            "      },\n" +
            "      \"starName\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"business\":{\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"location\":{\n" +
            "        \"type\":\"geo_point\"\n" +
            "      },\n" +
            "      \"pic\":{\n" +
            "        \"type\":\"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"all\":{\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
}
    @Test
    void createHotelIndex() throws IOException {
        //1.創(chuàng)建Request對象
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        //2.準備請求的參數(shù)
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        //3.發(fā)送請求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

運行后可以查詢到對應的索引庫

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?刪除和判斷索引庫

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

    @Test
    void testDeleteHotelIndex() throws IOException {
        //1.創(chuàng)建Request對象
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        //2.發(fā)送請求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }
    @Test
    void testExistsHotelIndex() throws IOException {
        //1.創(chuàng)建Request對象
        GetIndexRequest request = new GetIndexRequest("hotel");
        //2.發(fā)送請求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        //3.輸出
        System.out.println(exists?"索引庫已存在":"索引庫不存在");
    }

總結:

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?RestClient操作文檔

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

初始化和上面一樣

?添加文檔

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?這里將查詢數(shù)據庫的數(shù)據插入到索引庫當中,但是索引庫中的location是將兩個數(shù)據拼到一起的,所以這里有兩個實體類,一個對應數(shù)據庫,一個對應索引庫,從數(shù)據庫查出來之后還要轉到索引庫數(shù)據類型。

然后再序列化層JSON風格的數(shù)據。

@SpringBootTest
public class HotelDocumentTest {
    private RestHighLevelClient client;


    @Autowired
    private IHotelService hotelService;
    @Test
    void testAddDocument() throws IOException {

        //根據id查詢酒店數(shù)據
        Hotel hotel = hotelService.getById(61083L);
        //轉換為文檔類型
        HotelDoc hotelDoc = new HotelDoc(hotel);

        //1.準備Request對象
        IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
        //2.準備JSON文檔
        request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
        //3.發(fā)送請求
        client.index(request,RequestOptions.DEFAULT);
    }
    //執(zhí)行前初始化
    @BeforeEach
    void setUp(){
        this.client=new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
              //HttpHost.create("http://xxx.xxx.xxx.xxx:9200") 集群時添加更多的地址
        ));
    }

    //銷毀
    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}

這里使用給定的項目代碼在數(shù)據庫配置中有錯誤

改成如下

url: jdbc:mysql://localhost:3306/heima?allowPublicKeyRetrieval=true&useSSL=false

成功弄執(zhí)行后便可查詢到?

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

?查詢文檔

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

    @Test
    void testGetDocumentById() throws IOException {
        //1.準備request
        GetRequest request = new GetRequest("hotel", "61083");
        //2.發(fā)送請求,得到響應
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //3.解析響應結果
        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json,  HotelDoc.class);
        System.out.println(hotelDoc);
    }

成功查詢得到

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

更新文檔

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

    @Test
    void testUpdate() throws IOException {
        //1.準備request
        UpdateRequest request = new UpdateRequest("hotel", "61083");
        //2.準備請求參數(shù)
        request.doc(
                "price","961",
                "starName","三轉"
        );
        //3.發(fā)送請求
        client.update(request,RequestOptions.DEFAULT);
    }

刪除文檔?


    @Test
    void testDeleteDocument() throws IOException {
        //1.準備request
        DeleteRequest request = new DeleteRequest("hotel", "61083");
        //2.發(fā)送請求
        client.delete(request,RequestOptions.DEFAULT);
    }

?總結

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

批量導入文檔

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es

    @Test
    void testBulkRequest() throws IOException {
        //批量查詢酒店數(shù)據
        List<Hotel> hotels = hotelService.list();

        //1.創(chuàng)建request
        BulkRequest request = new BulkRequest();
        //2.準備參數(shù),添加多個新增的request
        for (Hotel hotel : hotels) {
            //轉換為文檔類型
            HotelDoc hotelDoc = new HotelDoc(hotel);
            //創(chuàng)建新增文檔的Request對象
            request.add(new IndexRequest("hotel")
                    .id(hotel.getId().toString())
                    .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }

        //3.發(fā)送請求
        client.bulk(request,RequestOptions.DEFAULT);
    }

?然后可以查詢得到201條數(shù)據

微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序),SpringBoot,java,服務器,linux,es文章來源地址http://www.zghlxwxcb.cn/news/detail-637504.html

到了這里,關于微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 微服務學習|初識elasticsearch、操作索引庫、文檔操作、RestClient操作索引庫、RestClient操作文檔

    微服務學習|初識elasticsearch、操作索引庫、文檔操作、RestClient操作索引庫、RestClient操作文檔

    elasticsearch是一款非常強大的開源搜索引擎,可以幫助我們從海量數(shù)據中快速找到需要的內容。 elasticsearch結合kibana、Logstash、Beats,也就是elastic stack (ELK)。被廣泛應用在日志數(shù)據分析、實時監(jiān)控等領域 elasticsearch是elastic stack的核心,負責存儲、搜索、分析數(shù)據 Lucene是一個jav

    2024年01月18日
    瀏覽(64)
  • 【ElasticSearch】基于 Java 客戶端 RestClient 實現(xiàn)對 ElasticSearch 索引庫、文檔的增刪改查操作,以及文檔的批量導入

    【ElasticSearch】基于 Java 客戶端 RestClient 實現(xiàn)對 ElasticSearch 索引庫、文檔的增刪改查操作,以及文檔的批量導入

    ElasticSearch 官方提供了各種不同語言的客戶端,用來操作 ES。這些客戶端的本質就是組裝 DSL 語句,通過 HTTP 請求發(fā)送給 ES 服務器。 官方文檔地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html。 在本文中,我們將著重介紹 ElasticSearch Java 客戶端中的 RestClient,并演示如何

    2024年02月08日
    瀏覽(30)
  • ElasticSearch索引庫、文檔、RestClient操作

    ElasticSearch索引庫、文檔、RestClient操作

    es中的索引是指相同類型的文檔集合 ,即mysql中表的概念 映射:索引中文檔字段的約束,比如名稱、類型 mapping映射是對索引庫中文檔的約束。類似mysql對表單字段的約束 type :字段數(shù)據類型,常見的類型有: 字符串:text(可分詞的文本)、keyword(不可分詞的文本,例如國家

    2024年02月10日
    瀏覽(24)
  • ElasticSearch基礎1——索引和文檔。Kibana,RestClient操作索引和文檔+黑馬旅游ES庫導入

    ElasticSearch基礎1——索引和文檔。Kibana,RestClient操作索引和文檔+黑馬旅游ES庫導入

    導航: 【黑馬Java筆記+踩坑匯總】JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外賣+SpringCloud/SpringCloudAlibaba+黑馬旅游+谷粒商城 黑馬旅游源碼:? https://wwmg.lanzouk.com/ikjTE135ybje 目錄 1.初識彈性搜索elasticsearch 1.1.了解ES 1.1.1.elasticsearch的作用 1.1.2.ELK彈性棧 1.1.3.elasticsearch和lucene 1.1.4.搜索引擎技術

    2024年02月01日
    瀏覽(50)
  • Elasticsearch索引庫和文檔的相關操作

    Elasticsearch索引庫和文檔的相關操作

    前言:最近一直在復習Elasticsearch相關的知識,公司搜索相關的技術用到了這個,用公司電腦配了環(huán)境,借鑒網上的課程進行了總結。希望能夠加深自己的印象以及幫助到其他的小伙伴兒們????。 如果文章有什么需要改進的地方還請大佬不吝賜教????。 小威在此先感謝各位

    2024年02月02日
    瀏覽(37)
  • 微服務分布式搜索引擎 Elastic Search RestClient 操作文檔

    微服務分布式搜索引擎 Elastic Search RestClient 操作文檔

    本文參考黑馬 分布式Elastic search Elasticsearch是一款非常強大的開源搜索引擎,具備非常多強大功能,可以幫助我們從海量數(shù)據中快速找到需要的內容 初始化RestHighLevelClient 為了與索引庫操作分離,我們再次參加一個測試類,做兩件事情: 初始化RestHighLevelClient 我們的酒店數(shù)據

    2024年01月24日
    瀏覽(25)
  • 【ElasticSearch】使用 Java 客戶端 RestClient 實現(xiàn)對文檔的查詢操作,以及對搜索結果的排序、分頁、高亮處理

    【ElasticSearch】使用 Java 客戶端 RestClient 實現(xiàn)對文檔的查詢操作,以及對搜索結果的排序、分頁、高亮處理

    在 Elasticsearch 中,通過 RestAPI 進行 DSL 查詢語句的構建通常是通過 HighLevelRestClient 中的 resource() 方法來實現(xiàn)的。該方法包含了查詢、排序、分頁、高亮等所有功能,為構建復雜的查詢提供了便捷的接口。 RestAPI 中構建查詢條件的核心部分是由一個名為 QueryBuilders 的工具類提供

    2024年01月16日
    瀏覽(41)
  • 【SpringBoot】整合Elasticsearch 操作索引及文檔

    【SpringBoot】整合Elasticsearch 操作索引及文檔

    官網操作文檔:Elasticsearch Clients | Elastic ????? ???????? 踩坑太多了。。。這里表明一下Spring Boot2.4以上版本可能會出現(xiàn)問題,所以我降到了2.2.1.RELEASE。對于現(xiàn)在2023年6月而言,Es版本已經到了8.8,而SpringBoot版本已經到了3.x版本。如果是高版本的Boot在配置類的時候會發(fā)現(xiàn)

    2024年02月09日
    瀏覽(58)
  • 【Elasticsearch篇】詳解使用RestClient操作索引庫的相關操作

    【Elasticsearch篇】詳解使用RestClient操作索引庫的相關操作

    Elasticsearch是一個開源的分布式搜索和分析引擎, 用于實時搜索、分析和存儲大規(guī)模數(shù)據 。它基于Apache Lucene庫構建,提供了一個簡單而強大的分布式搜索解決方案。 Elasticsearch具有以下特點: 分布式架構:Elasticsearch可以在多個節(jié)點上分布數(shù)據,并自動處理數(shù)據的復制、故障

    2024年01月24日
    瀏覽(25)
  • 【ElasticSearch】JavaRestClient實現(xiàn)索引庫和文檔的增刪改查

    【ElasticSearch】JavaRestClient實現(xiàn)索引庫和文檔的增刪改查

    ES官方提供了各種不同語言的客戶端,用來操作ES,即RestClient。這些 客戶端的本質就是組裝DSL語句,通過http請求發(fā)送給ES。 數(shù)據庫信息如下: 導入demo工程,基本結構如下: ES的mapping要考慮的點主要有: 字段名(name) 字段類型(type) 是否參與搜索(index) 是否分詞(typ

    2024年02月13日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包