最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Data?Elasticsearch使用方式(Elasticsearch)

 更新時間:2025年04月17日 10:04:37   作者:1  
這篇文章主要介紹了Spring?Data?Elasticsearch使用方式(Elasticsearch),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

1. 項目部署

1.1 添加依賴

在項目的 pom.xml 中引?Spring Data Elasticsearch的啟動器。

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

1.2 配置application.yml 文件

spring:
 data:
   elasticsearch:
     cluster-name: csdn-elastic
     cluster-nodes: 127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303

需要注意的是,Spring Data Elasticsearch底層使?的不是Elasticsearch提供的RestHighLevelClient,?是 TransportClient,并不采?HTTP協(xié)議通信,?是訪問Elasticsearch對外開放的TCP端?。

我們在之前集群配置 中,設(shè)置的端?分別是:9301、9302、9303。

1.3 測試是否注入成功

在test測試?件夾下的com.csdn.es包下創(chuàng)建?個SpringDataESTests測試類。通過@Autowired注解對 ElasticsearchTemplate進(jìn)?注?,測試對象是否可以獲取到。

package com.csdn.es;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESTests {
 @Autowired
 private ElasticsearchTemplate template;
 @Test
 public void check() {
 System.err.println(template);
 }
}

注: 使用 Spring Data Elasticsearch 是springboot 版本不能太高,建議使用 2.1.6.RELEASE 版本。如果有 Rest Client 版本太高也會注入失敗,建議使用 6.4.3 。

2. 操作

2.1 索引庫操作

2.1.1 實體類注解

1.我們在Product實體類上添加下?的?些注解。

package com.csdn.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "csdn", type = "product", shards = 3, replicas = 1)
public class Product {
 @Id
 private Long id;
 @Field(type = FieldType.Text, analyzer = "ik_max_word")
 private String title; // 標(biāo)題
 @Field(type = FieldType.Keyword)
 private String category; // 分類
 @Field(type = FieldType.Keyword)
 private String brand; // 品牌
 @Field(type = FieldType.Double)
 private Double price; // 價格
 @Field(type = FieldType.Keyword, index = false)
 private String images; // 圖?地址
}

2.在SpringDataESTests類中定義創(chuàng)建索引的createIndex()?法。

@Test
public void createIndex() {
 // 創(chuàng)建索引庫,并制定實體類的字節(jié)碼
 template.createIndex(Product.class);
}

2.1.2 創(chuàng)建映射

剛才的注解已經(jīng)把映射關(guān)系也配置上了,所以創(chuàng)建映射只需要這樣:

@Test
public void createMapping() {
 template.putMapping(Product.class);
}

2.2 索引數(shù)據(jù)CRUD操作

SDE的索引數(shù)據(jù)CRUD操作并沒有封裝在ElasticsearchTemplate類中,?是有?個叫做ElasticsearchRepository的 接?中。

在com.csdn.respository包下?定義ProductRepository接?,并繼承ElasticsearchRespository接?。

package com.csdn.respository;
import com.yx.pojo.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ProductRepository extends ElasticsearchRepository<Product, Long> {
 
}

2.2.1 創(chuàng)建索引數(shù)據(jù)

1.先來看單個創(chuàng)建。在SpringDataESTests類中定義addDocument()?法。

@Autowired
private ProductRepository productRepository;
@Test
public void addDocument() {
 Product product = new Product(1L, "???機(jī)Mix", "?機(jī)", "??", 2899.00,
"http://image.yx.com/12479122.jpg");
 // 添加索引數(shù)據(jù)
 productRepository.save(product);
}

2.再來看批量創(chuàng)建。在SpringDataESTests類中定義addDocuments()?法。

@Test
public void addDocuments() {
 // 準(zhǔn)備?檔數(shù)據(jù)
 List<Product> list = new ArrayList<>();
 list.add(new Product(2L, "堅果?機(jī)R1", "?機(jī)", "錘?", 3699.00,
"http://image.yx.com/12479122.jpg"));
 list.add(new Product(3L, "華為META20", "?機(jī)", "華為", 4499.00,
"http://image.yx.com/12479122.jpg"));
 list.add(new Product(4L, "??Pro", "?機(jī)", "??", 4299.00,
"http://image.yx.com/12479122.jpg"));
 list.add(new Product(5L, "榮耀V20", "?機(jī)", "華為", 2799.00,
"http://image.yx.com/12479122.jpg"));
 // 添加索引數(shù)據(jù)
 productRepository.saveAll(list);
}

2.2.2 查詢索引數(shù)據(jù)

1.根據(jù)id查詢數(shù)據(jù)

@Test
public void findById() {
 Optional<Product> optional = productRepository.findById(1L);
 Product defaultProduct = new Product();
 defaultProduct.setTitle("默認(rèn)商品數(shù)據(jù)");
 // orElse(T other)?法:如果Optional對象中封裝的泛型為null,則將orElse()?法的參數(shù)作為返回值
 Product product = optional.orElse(defaultProduct);
 System.err.println(product);
}

2.查詢所有數(shù)據(jù)

@Test
public void findAll() {
 Iterable<Product> list = productRepository.findAll();
 list.forEach(System.err::println);
}

2.2.3 ?定義?法查詢

在ProductRepository接?中定義根據(jù)商品的價格區(qū)間查詢商品數(shù)據(jù)的findByPriceBetween()?法。

/**
* 根據(jù)商品的價格區(qū)間查詢商品數(shù)據(jù)
* @param from 開始價格
* @param to 結(jié)束價格
* @return 符合條件的商品數(shù)據(jù)
*/
List<Product> findByPriceBetween(Double from, Double to);

?需寫實現(xiàn),SDE會?動幫我們實現(xiàn)該?法,我們只需要?即可。在SpringDataESTests類中定義 findByPriceBetween()?法。

@Test
public void findByPriceBetween() {
 List<Product> products = productRepository.findByPriceBetween(3000d, 5000d);
 products.forEach(System.err::println);
}

2.3 原?查詢案例

在com.csdn.mapper包下?定義搜索結(jié)果映射ProductSearchResultMapper類。

package com.csdn.esclient;

import com.google.gson.Gson;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 1.接口 SearchResultMapper 表示用來自定義查詢結(jié)果映射,將結(jié)果按照開發(fā)者的配置,進(jìn)行重組,然后返回客戶端
 * 2.重寫 mapResults
 */
/** ?定義查詢結(jié)果映射,?于處理?亮顯示 */
public class ProductSearchResulMapper implements SearchResultMapper {
    @Override
    public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {
        // 記錄總條數(shù)
        long totalHits = searchResponse.getHits().getTotalHits();
        // 記錄列表(泛型) - 構(gòu)建Aggregate使?
        List<T> list = new ArrayList<>();
        // 獲取搜索結(jié)果(真正的的記錄)
        SearchHits hits = searchResponse.getHits();
        Gson gson = new Gson();
        for (SearchHit hit : hits) {
            if (hits.getHits().length <= 0) {
                return null;
            }
            // 將原本的JSON對象轉(zhuǎn)換成Map對象
            Map<String, Object> map = hit.getSourceAsMap();
            // 獲取?亮的字段Map
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            for (Map.Entry<String, HighlightField> highlightField : highlightFields.entrySet()) {
                // 獲取?亮的Key
                String key = highlightField.getKey();
                // 獲取?亮的Value
                HighlightField value = highlightField.getValue();
                // 實際fragments[0]就是?亮的結(jié)果,?需遍歷拼接
                Text[] fragments = value.getFragments();
                // 因為?亮的字段必然存在于Map中,就是key值
                map.put(key, fragments[0].toString());
            }
            // 把Map轉(zhuǎn)換成對象
            T item = gson.fromJson(gson.toJson(map), aClass);
            list.add(item);
        }
        // 返回的是帶分?的結(jié)果
        return new AggregatedPageImpl<>(list, pageable, totalHits);
    }
}

Java測試類:

package com.csdn.es;

import com.qf.esclient.ProductSearchResulMapper;
import com.qf.pojo.Product;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringDataESTests {

    @Autowired
    private ElasticsearchTemplate template;

    @Test
    public void test01() {
        // 1.創(chuàng)建原生查詢語句構(gòu)建
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        // 2.通過NativeSearchQueryBuilder指定過濾器條件
        builder.withSourceFilter(new FetchSourceFilter(null,null));
        // 3.添加詞條搜索條件
        builder.withQuery(QueryBuilders.matchQuery("title","手機(jī)"));
        // 4.分頁操作:當(dāng)前頁其實位置的下標(biāo)
        builder.withPageable(PageRequest.of(0, 2, Sort.by(Sort.Direction.ASC,
                "price")));
        // 5.高亮顯示 TODO
        HighlightBuilder.Field field = new HighlightBuilder.Field("title");
        field.preTags("<span style='color:red'>");
        field.postTags("</span>");
        builder.withHighlightFields(field);

        // 6.聚合:分組。參數(shù)解析:terms()方法指定分組的名稱(聚合名稱)
        builder.addAggregation(AggregationBuilders.terms("brandAgg").field("brand"));
        // 7.構(gòu)建查詢的條件,并且執(zhí)行查詢
        AggregatedPage<Product> packages = template.queryForPage(builder.build(), Product.class,new ProductSearchResulMapper());


        long total = packages.getTotalElements();
        int totalPages = packages.getTotalPages();
        List<Product> list = packages.getContent();
        System.out.println("總條數(shù):" + total);
        System.out.println("總?數(shù):" + totalPages);
        System.out.println("數(shù)據(jù):");
        list.forEach(System.out::println);


        // 聚合
        /**
        Aggregations aggregations = packages.getAggregations();
        // 導(dǎo)包org.elasticsearch.search.aggregations.bucket.terms.Terms
        Terms terms = aggregations.get("brandAgg");
        terms.getBuckets().forEach(b -> {
            System.err.println("品牌:" + b.getKeyAsString());
            System.err.println("count:" + b.getDocCount());
        });
         */
    }

}

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Boot JPA如何把ORM統(tǒng)一起來

    Spring Boot JPA如何把ORM統(tǒng)一起來

    Spring Data JPA 是 Spring 基于 ORM 框架、JPA 規(guī)范的基礎(chǔ)上封裝的一套JPA應(yīng)用框架,可使開發(fā)者用極簡的代碼即可實現(xiàn)對數(shù)據(jù)的訪問和操作,本文給大家詳細(xì)介紹了Spring Boot JPA如何把ORM統(tǒng)一起來,感興趣的朋友一起看看吧
    2018-04-04
  • Maven多倉庫多鏡像源配置過程

    Maven多倉庫多鏡像源配置過程

    本文詳細(xì)介紹了Maven中的倉庫(Repository)和鏡像(Mirror)配置,包括它們的區(qū)別、分類、配置位置、注意事項以及協(xié)同工作流程,通過多倉庫配置,可以解決依賴來源多樣性問題;通過多鏡像配置,可以提高下載效率并統(tǒng)一管理
    2025-11-11
  • java中XML的使用全過程

    java中XML的使用全過程

    這篇文章主要介紹了java中XML的使用全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • 解決項目版本沖突之maven-shade插件使用方式

    解決項目版本沖突之maven-shade插件使用方式

    本文介紹了在Maven項目中如何處理三方依賴與已有依賴沖突的問題,使用maven-shade-plugin插件實現(xiàn)依賴隔離,同時避免版本不一致導(dǎo)致項目異常,通過重命名依賴包名實現(xiàn)不同版本共存,解決三方依賴版本不一致問題,另外還提到了在使用過程中一些坑點和解決方案
    2026-05-05
  • Java四大常用JSON解析工具性能對比(Hutool、Fastjson2、Gson與Jackson)

    Java四大常用JSON解析工具性能對比(Hutool、Fastjson2、Gson與Jackson)

    JSON?是現(xiàn)代軟件開發(fā)中常用的數(shù)據(jù)交換格式,尤其在微服務(wù)和前后端分離的架構(gòu)中更是必不可少,本文將對?Java?中四大主流?JSON?解析庫進(jìn)行性能測試和對比分析,希望對大家有所幫助
    2025-05-05
  • Java常見異常全面梳理(含分類+含義+典型場景)

    Java常見異常全面梳理(含分類+含義+典型場景)

    異常是指程序在執(zhí)行過程中,出現(xiàn)的非正常情況,可能由程序員錯誤、系統(tǒng)錯誤或用戶輸入錯誤引起,下面這篇文章主要介紹了Java常見異常全面梳理的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2026-03-03
  • SpringBoot如何啟動自動加載自定義模塊yml文件(PropertySourceFactory)

    SpringBoot如何啟動自動加載自定義模塊yml文件(PropertySourceFactory)

    這篇文章主要介紹了SpringBoot如何啟動自動加載自定義模塊yml文件(PropertySourceFactory),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Springboot使用CXF進(jìn)行WSDL轉(zhuǎn)換及WebService服務(wù)調(diào)用過程

    Springboot使用CXF進(jìn)行WSDL轉(zhuǎn)換及WebService服務(wù)調(diào)用過程

    本文介紹如何利用Apache CXF將WSDL轉(zhuǎn)換為Java代碼,提供bat命令和Maven插件兩種方法,并詳解生成類的屬性(如name、targetNamespace)及調(diào)用接口的參數(shù)傳遞方式,助力高效集成Web服務(wù)
    2025-09-09
  • Java使用黑盒方式模擬實現(xiàn)內(nèi)網(wǎng)穿透

    Java使用黑盒方式模擬實現(xiàn)內(nèi)網(wǎng)穿透

    這篇文章主要介紹了Java使用黑盒方式模擬實現(xiàn)內(nèi)網(wǎng)穿透,內(nèi)網(wǎng)穿透,也即 NAT 穿透,進(jìn)行 NAT 穿透是為了使具有某一個特定源 IP 地址和源端口號的數(shù)據(jù)包不被 NAT 設(shè)備屏蔽而正確路由到內(nèi)網(wǎng)主機(jī),需要的朋友可以參考下
    2023-05-05
  • 深入理解Java編程線程池的實現(xiàn)原理

    深入理解Java編程線程池的實現(xiàn)原理

    這篇文章主要介紹了深入理解Java編程線程池的實現(xiàn)原理,涉及ThreadPoolExecutor類,線程池實現(xiàn)原理及示例等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11

最新評論

游戏| 资溪县| 莆田市| 五指山市| 蒙山县| 玛沁县| 淄博市| 通海县| 湘西| 涞水县| 黔西县| 汉源县| 长泰县| 河间市| 拉萨市| 卓尼县| 旌德县| 东明县| 米易县| 嘉荫县| 南川市| SHOW| 潍坊市| 永仁县| 营口市| 伊金霍洛旗| 鱼台县| 新安县| 中江县| 泾阳县| 山西省| 平度市| 华容县| 祁连县| 杭锦后旗| 上思县| 常德市| 手游| 称多县| 柳河县| 荆门市|