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

java 操作es 的基本操作

這篇具有很好參考價值的文章主要介紹了java 操作es 的基本操作。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

  • 創(chuàng)建索引
  • 創(chuàng)建索引別名
  • 索引的相關設置
  • 查詢索引數(shù)據(jù)
  • bulk 導入數(shù)據(jù)
  • 持續(xù)更新中~

pom的坐標文章來源地址http://www.zghlxwxcb.cn/news/detail-807261.html

        <!--es相關-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.10.2</version> <!-- 請根據(jù)需要選擇合適的版本 -->
        </dependency>

package es;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class EsConnectionExample {

    public static void main(String[] args) throws IOException {
        // 創(chuàng)建客戶端
        // 連接es的地址
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("ip", 9200, "http"))); // 修改為你的ES地址和端口

        //創(chuàng)建索引
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index"); // 創(chuàng)建名為"my_index"的索引
        createIndexRequest.settings(Settings.builder()
                .put("index.number_of_shards", 3) // 設置分片數(shù)
                .put("index.number_of_replicas", 2) // 設置副本數(shù)
        );
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println("索引創(chuàng)建成功:" + createIndexResponse.isAcknowledged());


        //給索引插入數(shù)據(jù)
        Map<String, Object> data = new HashMap<>();
        data.put("name", "huangi"); // 設置文檔標題
        data.put("age", "18"); // 設置文檔內(nèi)容
        IndexRequest indexRequest = new IndexRequest("my_index"); // 將數(shù)據(jù)插入到"my_index"索引中
        indexRequest.id("1"); // 設置文檔ID為1
        indexRequest.source(data, XContentType.JSON); // 將數(shù)據(jù)轉(zhuǎn)換為JSON格式并設置到索引請求中
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println("數(shù)據(jù)插入成功:" + indexResponse.getResult().name().toLowerCase());

        
        //---------------------------------------------------------------------                             
        //查詢一個名叫my_index的索引下列是age,值為25的數(shù)據(jù),不是格式化展示                                                            
        // 構建查詢請求                                                                                          
        // 創(chuàng)建搜索索引請求
        SearchRequest searchRequest = new SearchRequest("my_index"); // "posts" 是 index 的名字
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // 查詢所有文檔
        //searchSourceBuilder.sort("created_at", SortOrder.DESC); // 按 created_at 字段降序排序
        searchSourceBuilder.highlighter(new HighlightBuilder().field("content").preTags("<em>").postTags("</em>")); // 對 content 字段進行高亮顯示
        searchRequest.source(searchSourceBuilder);

        // 執(zhí)行搜索并獲取響應
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("Total hits: " + searchResponse.getHits().getTotalHits().value);
        Map<String, HighlightField> highlightFields = searchResponse.getHits().getHits()[0].getHighlightFields();
        for (HighlightField highlightField : highlightFields.values()) {
            System.out.println("輸出:"+highlightField.fragments()[0].string()); // 輸出高亮顯示的內(nèi)容片段
        }


        //---------------------------------------------------------------------
        //查詢一個名叫my_index的索引下列是age,值為25的數(shù)據(jù),并格式化展示
        // 構建查詢請求
        SearchRequest searchRequest1 = new SearchRequest("my_index"); // 將"your_index"替換為您要查詢的索引名稱
        SearchSourceBuilder searchSourceBuilder1 = new SearchSourceBuilder();
        searchSourceBuilder1.query(QueryBuilders.matchQuery("age", "25")); // 將"field_name"替換為您要查詢的字段名稱,將"query_value"替換為您要匹配的查詢值
        searchRequest1.source(searchSourceBuilder1);

        // 執(zhí)行查詢請求并獲取響應
        SearchResponse searchResponse1 = client.search(searchRequest1, RequestOptions.DEFAULT);

        // 處理響應數(shù)據(jù)(例如打印結果)
        System.out.println("Total hits: " + searchResponse1.getHits().getTotalHits().value);
        System.out.println("Hits: " + JSONObject.toJSONString(searchResponse1.getHits().getHits()));

        // 處理響應數(shù)據(jù)(例如打印結果)
        for (SearchHit hit : searchResponse1.getHits()) {
            HighlightField highlightField = hit.getHighlightFields().get("field_name"); // 將"field_name"替換為您要高亮的字段名稱
            String highlightedValue = highlightField != null ? highlightField.getFragments()[0].string() : hit.getSourceAsString(); // 如果存在高亮結果,則獲取第一個片段的值,否則獲取原始字段的值
            System.out.println(highlightedValue); // 打印字段的值
        }

        //---------------------------------------------------------------------
        //創(chuàng)建索引別名
        // 索引名稱和別名
        String indexName = "my_index";
        String aliasName = "my_index_alias";

        // 創(chuàng)建一個別名請求
        IndicesAliasesRequest request = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD)
                .index(indexName)
                .alias(aliasName);
        request.addAliasAction(aliasAction);

        // 執(zhí)行請求
        AcknowledgedResponse indicesAliasesResponse = client.indices().updateAliases(request, RequestOptions.DEFAULT);

        // 檢查操作是否成功
        boolean acknowledged = indicesAliasesResponse.isAcknowledged();
        if (acknowledged) {
            System.out.println("Alias was successfully added to the index.");
        } else {
            System.out.println("Alias addition was not acknowledged.");
        }

        //---------------------------------------------------------------------
        //bulk 導入
        try (BufferedReader reader = new BufferedReader(new FileReader("user.json"))) {
            String line;

            // 構造 BulkRequest 對象并添加要導入的文檔
            BulkRequest request1 = new BulkRequest();
            while ((line = reader.readLine()) != null) {
                XContentBuilder builder = XContentFactory.jsonBuilder()
                        .startObject()
                        .field("name", line)
                        .field("age", line)
                        .field("sex" , line)
                        .field("telephone", line)
                        .endObject();
                IndexRequest indexRequest1 = new IndexRequest("my_index")
                        .source(builder);
                request1.add(indexRequest1);
            }

            // 發(fā)送 BulkRequest 請求
            BulkResponse response = client.bulk(request1, RequestOptions.DEFAULT);

            if (response.hasFailures()) {
                System.out.println("Failed to import documents.");
            } else {
                System.out.println("Documents imported successfully!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉 ElasticSearch 客戶端連接
            client.close();
        }


        // 關閉客戶端連接
        client.close();
    }
}

到了這里,關于java 操作es 的基本操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關文章

  • java 操作es 的基本操作

    創(chuàng)建索引 創(chuàng)建索引別名 索引的相關設置 查詢索引數(shù)據(jù) bulk 導入數(shù)據(jù) 持續(xù)更新中~ pom的坐標

    2024年01月20日
    瀏覽(18)
  • elasticsearch 7.9.3知識歸納整理(二)之 es基本原理及使用kibana操作es的常見命令

    elasticsearch 7.9.3知識歸納整理(二)之 es基本原理及使用kibana操作es的常見命令

    一、es的基本原理與基礎概念 1.1 倒排索引 倒排索引 源于實際應用中需要根據(jù)屬性的值來查找記錄。這種索引表中的每一項都包括一個屬性值和具有該屬性值的各記錄的地址。由于不是由記錄來確定屬性值,而是由屬性值來確定記錄的位置,因而稱為倒排索引(inverted index)。帶

    2024年02月12日
    瀏覽(16)
  • ElasticSearch Java API 基本操作

    ElasticSearch Java API是ES官方在8.x版本推出的新java api,也可以適用于7.17.x版本的es。 本文主要參考了相關博文,自己手動編寫了下相關操作代碼,包括更新mappings等操作的java代碼。 代碼示例已上傳github。 elasticsearch 版本: 7.17.9 ,修改 /elasticsearch-7.17.9/config/elasticsearch.yml ,新增

    2024年02月08日
    瀏覽(22)
  • 原生語言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文檔的基本操作,es的高級查詢.查詢結果處理. 數(shù)據(jù)聚合.相關性系數(shù)打分

    原生語言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文檔的基本操作,es的高級查詢.查詢結果處理. 數(shù)據(jù)聚合.相關性系數(shù)打分

    ? Elasticsearch 是一個分布式、高擴展、高實時的搜索與數(shù)據(jù)分析引擎。它能很方便的使大量數(shù)據(jù)具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸縮性,能使數(shù)據(jù)在生產(chǎn)環(huán)境變得更有價值。Elasticsearch 的實現(xiàn)原理主要分為以下幾個步驟,首先用戶將數(shù)據(jù)提交到Elasti

    2024年02月05日
    瀏覽(124)
  • Elasticsearch之java的基本操作一

    摘要 ??接觸ElasticSearch已經(jīng)有一段了。在這期間,遇到很多問題,但在最后自己的不斷探索下解決了這些問題??吹骄W(wǎng)上或多或少的都有一些介紹ElasticSearch相關知識的文檔,但個人覺得都不是很全面。因此就有了寫ElasticSearch開發(fā)教程的想法,將學習到的技術經(jīng)驗分享出來,

    2024年02月05日
    瀏覽(24)
  • ElasticSearch 中的中文分詞器以及索引基本操作詳解,Java高并發(fā)編程詳解深入理解pdf

    ElasticSearch 中的中文分詞器以及索引基本操作詳解,Java高并發(fā)編程詳解深入理解pdf

    PUT book/_settings { “number_of_replicas”: 2 } 修改成功后,如下: 更新分片數(shù)也是一樣。 2.3 修改索引的讀寫權限 索引創(chuàng)建成功后,可以向索引中寫入文檔: PUT book/_doc/1 { “title”:“三國演義” } 寫入成功后,可以在 head 插件中查看: 默認情況下,索引是具備讀寫權限的,當然這

    2024年04月09日
    瀏覽(26)
  • 【ES】---ES的基本操作

    ES有4種客戶端,分別是:Jest client、Rest client、Transport client、Node client。 ES支持兩種協(xié)議 HTTP協(xié)議,支持的客戶端有Jest client和Rest client Native Elasticsearch binary協(xié)議,也就是Transport client【7.0棄用】和Node client【2.3棄用】 Jest client非官方支持,在ES5.0之前官方提供的客戶端只有Trans

    2024年02月08日
    瀏覽(21)
  • es基本操作使用

    es映射關系(對應數(shù)據(jù)庫) ———————————————— 1.創(chuàng)建索引 創(chuàng)建索引時指定屬性名,即映射關系 2.修改索引 3.刪除索引 eg:delete /test/type1/1,表示刪除test索引庫中類型(表)為type1中id屬性為1的document(記錄)。 4.查詢索引 表示查詢某一個屬性名為name的值為張

    2024年02月15日
    瀏覽(15)
  • 一些es的基本操作

    一些es的基本操作

    用postMan: 給名為population_portrait_hash_seven的索引增加了一個text類型的字段。 用chrome插件Elasticvue 的Rest接口,本質(zhì)上應該也是發(fā)HTTP請求: 給這個接口增加了一個keyword類型的字段。 好像直接刪除是不支持的。要建個新索引,再使用 Reindex API 將數(shù)據(jù)從舊索引復制到新索引,排除不

    2024年01月25日
    瀏覽(17)
  • Es的索引操作(代碼中的基本操作)

    // 1.創(chuàng)建客戶端對象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost(\\\"localhost\\\", 9200, \\\"http\\\")) ); (1)創(chuàng)建索引 // 創(chuàng)建索引 - 請求對象 CreateIndexRequest request = new CreateIndexRequest(\\\"user\\\"); // 發(fā)送請求,獲取響應 CreateIndexResponse response = client.indices().create(request, RequestOp

    2024年02月13日
    瀏覽(17)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領取紅包

二維碼2

領紅包