在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    如何利用 SpringBoot 在 ES 中實(shí)現(xiàn)類似鏈表的查詢?

    一、摘要

    在上篇文章中,我們?cè)敿?xì)地介紹了如何在 ES 中精準(zhǔn)地實(shí)現(xiàn)嵌套json對(duì)象查詢?

    那么問題來了,我們?nèi)绾卧诤蠖送ㄟ^技術(shù)方式快速地實(shí)現(xiàn) es 中內(nèi)嵌對(duì)象的數(shù)據(jù)查詢呢?

    為了方便更容易掌握技術(shù),本文主要以上篇文章中介紹的通過商品找訂單為案例,利用 SpringBoot 整合 ES 實(shí)現(xiàn)這個(gè)業(yè)務(wù)需求,向大家介紹具體的技術(shù)實(shí)踐方案,存入es中的json數(shù)據(jù)結(jié)構(gòu)如下:

    { “orderId”:”1″, “orderNo”:”123456″, “orderUserName”:”張三”, “orderItems”:[ { “orderItemId”:”12234″, “orderId”:”1″, “productName”:”火腿腸”, “brandName”:”雙匯”, “sellPrice”:”28″ }, { “orderItemId”:”12235″, “orderId”:”1″, “productName”:”果凍”, “brandName”:”匯源”, “sellPrice”:”12″ } ]}

    廢話也不多說了,直接上代碼!

    二、項(xiàng)目實(shí)踐

    2.1、添加依賴

    在SpringBoot項(xiàng)目中,添加rest-high-level-client客戶端,方便與 ES 服務(wù)器連接通信,在這里需要注意一下,推薦客戶端的版本與 ES 服務(wù)器的版本號(hào)一致,不然會(huì)出現(xiàn)接口請(qǐng)求錯(cuò)誤等異常!

    小編本次安裝的ES服務(wù)端版本號(hào)為6.8.2,因此客戶端也保持6.8.2,與之一致!

    org.elasticsearch elasticsearch 6.8.2 org.elasticsearch.client elasticsearch-rest-client 6.8.2 org.elasticsearch.client elasticsearch-rest-high-level-client 6.8.2

    2.2、配置 es 客戶端

    為了更加方便的使用 es,我們可以將其各個(gè)配置類進(jìn)行封裝,方便后續(xù)進(jìn)行維護(hù)。

    • 在application.properties配置文件中,定義 es 配置連接地址

    # 設(shè)置es參數(shù)elasticsearch.scheme=httpelasticsearch.address=127.0.0.1:9200elasticsearch.userName=elasticsearch.userPwd=elasticsearch.socketTimeout=5000elasticsearch.connectTimeout=5000elasticsearch.connectionRequestTimeout=5000

    • 創(chuàng)建ElasticSearch配置類,方便SpringBoot啟動(dòng)時(shí)注入

    import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.impl.client.BasicCredentialsProvider;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.Arrays;import java.util.Objects;@Configurationpublic class ElasticSearchConfiguration { private static final Logger log = LoggerFactory.getLogger(ElasticSearchConfiguration.class); private static final int ADDRESS_LENGTH = 2; @Value(“${elasticsearch.scheme:http}”) private String scheme; @Value(“${elasticsearch.address}”) private String address; @Value(“${elasticsearch.userName}”) private String userName; @Value(“${elasticsearch.userPwd}”) private String userPwd; @Value(“${elasticsearch.socketTimeout:5000}”) private Integer socketTimeout; @Value(“${elasticsearch.connectTimeout:5000}”) private Integer connectTimeout; @Value(“${elasticsearch.connectionRequestTimeout:5000}”) private Integer connectionRequestTimeout; /** * 初始化客戶端 * @return */ @Bean(name = “restHighLevelClient”) public RestHighLevelClient restClientBuilder() { HttpHost[] hosts = Arrays.stream(address.split(“,”)) .map(this::buildHttpHost) .filter(Objects::nonNull) .toArray(HttpHost[]::new); RestClientBuilder restClientBuilder = RestClient.builder(hosts); // 異步參數(shù)配置 restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setDefaultCredentialsProvider(buildCredentialsProvider()); return httpClientBuilder; }); // 異步連接延時(shí)配置 restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout); requestConfigBuilder.setSocketTimeout(socketTimeout); requestConfigBuilder.setConnectTimeout(connectTimeout); return requestConfigBuilder; }); return new RestHighLevelClient(restClientBuilder); } /** * 根據(jù)配置創(chuàng)建HttpHost * @param s * @return */ private HttpHost buildHttpHost(String s) { String[] address = s.split(“:”); if (address.length == ADDRESS_LENGTH) { String ip = address[0]; int port = Integer.parseInt(address[1]); return new HttpHost(ip, port, scheme); } else { return null; } } /** * 構(gòu)建認(rèn)證服務(wù) * @return */ private CredentialsProvider buildCredentialsProvider(){ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, userPwd)); return credentialsProvider; }}

    • 封裝ElasticSearch客戶端服務(wù)類,方便公共調(diào)用處理

    import com.fasterxml.jackson.databind.ObjectMapper;import org.example.es.exception.CommonException;import org.apache.commons.lang3.StringUtils;import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;import org.elasticsearch.action.admin.indices.get.GetIndexRequest;import org.elasticsearch.action.admin.indices.get.GetIndexResponse;import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;import org.elasticsearch.action.delete.DeleteRequest;import org.elasticsearch.action.delete.DeleteResponse;import org.elasticsearch.action.get.GetRequest;import org.elasticsearch.action.get.GetResponse;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.action.update.UpdateRequest;import org.elasticsearch.action.update.UpdateResponse;import org.elasticsearch.client.GetAliasesResponse;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.xcontent.XContentType;import org.elasticsearch.search.builder.SearchSourceBuilder;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.io.IOException;import java.util.Collections;import java.util.Map;import java.util.Set;@Componentpublic class ElasticSearchClient { private static final Logger log = LoggerFactory.getLogger(ElasticSearchClient.class); private static final ObjectMapper objectMapper = new ObjectMapper(); @Autowired private RestHighLevelClient client; /** * 查詢?nèi)?span id="zcxvfzm" class="wpcom_tag_link">索引 * @return */ public Set getAlias(){ try { GetAliasesRequest request = new GetAliasesRequest(); GetAliasesResponse response = client.indices().getAlias(request, RequestOptions.DEFAULT); return response.getAliases().keySet(); } catch (IOException e) { log.error(“向es發(fā)起查詢?nèi)克饕畔⒄?qǐng)求失敗”, e); } return Collections.emptySet(); } /** * 檢查索引是否存在 * @param indexName * @return */ public boolean existsIndex(String indexName){ try { // 創(chuàng)建請(qǐng)求 GetIndexRequest request = new GetIndexRequest().indices(indexName); // 執(zhí)行請(qǐng)求,獲取響應(yīng) boolean response = client.indices().exists(request, RequestOptions.DEFAULT); return response; } catch (Exception e) { log.error(“向es發(fā)起查詢索引是否存在請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); } return false; } /** * 查詢索引 * @param indexName * @return */ public String getIndex(String indexName){ try { // 創(chuàng)建請(qǐng)求 GetIndexRequest request = new GetIndexRequest().indices(indexName); // 執(zhí)行請(qǐng)求,獲取響應(yīng) GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT); return response.toString(); } catch (Exception e) { log.error(“向es發(fā)起查詢索引請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); } return StringUtils.EMPTY; } /** * 創(chuàng)建索引 * @param indexName * @param mapping * @return */ public void createIndex(String indexName, Map mapping){ try { CreateIndexRequest request = new CreateIndexRequest(); //索引名稱 request.index(indexName); //索引配置 Settings settings = Settings.builder() .put(“index.number_of_shards”, 3) .put(“index.number_of_replicas”, 1) .put(“index.max_inner_result_window”, 5000) .build(); request.settings(settings); //索引結(jié)構(gòu) request.mapping(“_doc”,mapping); //執(zhí)行請(qǐng)求,獲取響應(yīng) CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); if(!response.isAcknowledged()){ throw new CommonException(“向es發(fā)起創(chuàng)建索引請(qǐng)求失敗”); } log.info(“向es發(fā)起創(chuàng)建索引請(qǐng)求成功,返回參數(shù):{}”, response.index()); } catch (Exception e) { log.error(“向es發(fā)起創(chuàng)建索引請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); throw new CommonException(“向es發(fā)起創(chuàng)建索引請(qǐng)求失敗”); } } /** * 刪除索引 * @param indexName * @return */ public void deleteIndex(String indexName){ try { DeleteIndexRequest request = new DeleteIndexRequest(indexName); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); if(!response.isAcknowledged()){ throw new CommonException(“向es發(fā)起刪除索引請(qǐng)求失敗”); } log.info(“向es發(fā)起刪除索引請(qǐng)求成功,請(qǐng)求參數(shù):{}”, indexName); } catch (Exception e) { log.error(“向es發(fā)起刪除索引請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); throw new CommonException(“向es發(fā)起刪除索引請(qǐng)求失敗”); } } /** * 查詢索引映射字段 * @param indexName * @return */ public String getMapping(String indexName){ try { GetMappingsRequest request = new GetMappingsRequest().indices(indexName).types(“_doc”); GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT); return response.toString(); } catch (Exception e) { log.error(“向es發(fā)起查詢索引映射字段請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); } return StringUtils.EMPTY; } /** * 添加索引映射字段 * @param indexName * @return */ public void addMapping(String indexName, Map mapping){ try { PutMappingRequest request = new PutMappingRequest(); request.indices(indexName); request.type(“_doc”); //添加字段 request.source(mapping); AcknowledgedResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT); if(!response.isAcknowledged()){ throw new CommonException(“向es發(fā)起添加索引映射字段請(qǐng)求失敗”); } log.info(“向es發(fā)起添加索引映射字段請(qǐng)求成功,請(qǐng)求參數(shù):{}”, toJson(request)); } catch (Exception e) { log.error(“向es發(fā)起添加索引映射字段請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); throw new CommonException(“向es發(fā)起添加索引映射字段請(qǐng)求失敗”); } } /** * 向索引中添加文檔 * @param indexName * @param id * @param obj */ public void addDocument(String indexName, String id, Object obj){ try { //向索引中添加文檔 IndexRequest request = new IndexRequest(); // 外層參數(shù) request.id(id); request.index(indexName); request.type(“_doc”); // 存入對(duì)象 request.source(toJson(obj), XContentType.JSON); // 發(fā)送請(qǐng)求 IndexResponse response = client.index(request, RequestOptions.DEFAULT); if(response.status().getStatus() >= 400){ log.warn(“向es發(fā)起添加文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):{},返回參數(shù):{}”, request.toString(), response.toString()); throw new CommonException(“向es發(fā)起添加文檔數(shù)據(jù)請(qǐng)求失敗”); } } catch (Exception e) { log.error(“向es發(fā)起添加文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); throw new CommonException(“向es發(fā)起添加文檔數(shù)據(jù)請(qǐng)求失敗”); } } /** * 修改索引中的文檔數(shù)據(jù) * @param indexName * @param id * @param obj */ public void updateDocument(String indexName, String id, Map obj){ try { //修改索引中的文檔數(shù)據(jù) UpdateRequest request = new UpdateRequest(); // 外層參數(shù) request.id(id); request.index(indexName); request.type(“_doc”); // 存入對(duì)象 request.doc(obj); request.doc(toJson(obj), XContentType.JSON); // 發(fā)送請(qǐng)求 UpdateResponse response = client.update(request, RequestOptions.DEFAULT); if(response.status().getStatus() >= 400){ log.warn(“向es發(fā)起修改文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):{},返回參數(shù):{}”, request.toString(), response.toString()); throw new CommonException(“向es發(fā)起修改文檔數(shù)據(jù)請(qǐng)求失敗”); } } catch (Exception e) { log.error(“向es發(fā)起修改文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); throw new CommonException(“向es發(fā)起修改文檔數(shù)據(jù)請(qǐng)求失敗”); } } /** * 刪除索引中的文檔數(shù)據(jù) * @param indexName * @param id */ public void deleteDocument(String indexName, String id){ try { //刪除索引中的文檔數(shù)據(jù) DeleteRequest request = new DeleteRequest(); // 外層參數(shù) request.id(id); request.index(indexName); request.type(“_doc”); // 發(fā)送請(qǐng)求 DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); if(response.status().getStatus() >= 400){ log.warn(“向es發(fā)起刪除文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):{},返回參數(shù):{}”, request.toString(), response.toString()); throw new CommonException(“向es發(fā)起刪除文檔數(shù)據(jù)請(qǐng)求失敗”); } } catch (Exception e) { log.error(“向es發(fā)起刪除文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); throw new CommonException(“向es發(fā)起刪除文檔數(shù)據(jù)請(qǐng)求失敗”); } } /** * 查詢索引中的文檔數(shù)據(jù) * @param indexName * @param id */ public String getDocumentById(String indexName, String id){ try { GetRequest request = new GetRequest(); // 外層參數(shù) request.id(id); request.index(indexName); request.type(“_doc”); // 發(fā)送請(qǐng)求 GetResponse response = client.get(request, RequestOptions.DEFAULT); response.getSourceAsString(); } catch (Exception e) { log.error(“向es發(fā)起查詢文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):” + indexName, e); } return StringUtils.EMPTY; } /** * 索引高級(jí)查詢 * @param indexName * @param source * @return */ public SearchResponse searchDocument(String indexName, SearchSourceBuilder source){ //搜索 SearchRequest searchRequest = new SearchRequest(); searchRequest.indices(indexName); searchRequest.source(source); try { // 執(zhí)行請(qǐng)求 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); return response; } catch (Exception e) { log.warn(“向es發(fā)起查詢文檔數(shù)據(jù)請(qǐng)求失敗,請(qǐng)求參數(shù):” + searchRequest.toString(), e); } return null; } /** * 將對(duì)象格式化成json,并保持原字段類型輸出 * @param object * @return */ private String toJson(Object object) { try { return objectMapper.writeValueAsString(object); } catch (Exception e) { throw new CommonException(e); } }}

    2.3、初始化索引結(jié)構(gòu)

    在使用 es 對(duì)訂單進(jìn)行查詢搜索時(shí),我們需要先定義好對(duì)應(yīng)的訂單索引結(jié)構(gòu),內(nèi)容如下:

    @ActiveProfiles(“dev”)@RunWith(SpringRunner.class)@SpringBootTestpublic class OrderIndexServiceJunit { @Autowired private ElasticSearchClient elasticSearchClient; /** * 初始化索引結(jié)構(gòu) * * @return */ @Test public void initIndex(){ String indexName = “orderIndex-2022-07”; // 創(chuàng)建請(qǐng)求 boolean existsIndex = elasticSearchClient.existsIndex(indexName); if (!existsIndex) { Map properties = buildMapping(); elasticSearchClient.createIndex(indexName, properties); } } /** * 構(gòu)建索引結(jié)構(gòu) * * @return */ private Map buildMapping() { Map properties = new HashMap(); //訂單id 唯一鍵ID properties.put(“orderId”, ImmutableBiMap.of(“type”, “keyword”)); //訂單號(hào) properties.put(“orderNo”, ImmutableBiMap.of(“type”, “keyword”)); //客戶姓名 properties.put(“orderUserName”, ImmutableBiMap.of(“type”, “text”)); //訂單項(xiàng) Map orderItems = new HashMap(); //訂單項(xiàng)ID orderItems.put(“orderItemId”, ImmutableBiMap.of(“type”, “keyword”)); //產(chǎn)品名稱 orderItems.put(“productName”, ImmutableBiMap.of(“type”, “text”)); //品牌名稱 orderItems.put(“brandName”, ImmutableBiMap.of(“type”, “text”)); //銷售金額,單位分*100 orderItems.put(“sellPrice”, ImmutableBiMap.of(“type”, “integer”)); properties.put(“orderItems”, ImmutableBiMap.of(“type”, “nested”, “properties”, orderItems)); //文檔結(jié)構(gòu)映射 Map mapping = new HashMap(); mapping.put(“properties”, properties); return mapping; }}

    2.4、向 es 中同步文檔數(shù)據(jù)

    索引結(jié)構(gòu)創(chuàng)建好之后,我們需要將支持 es 搜索的訂單數(shù)據(jù)同步進(jìn)去。

    將指定的訂單 ID 從數(shù)據(jù)庫查詢出來,并封裝成 es 訂單數(shù)據(jù)結(jié)構(gòu),保存到 es 中!

    @ActiveProfiles(“dev”)@RunWith(SpringRunner.class)@SpringBootTestpublic class OrderIndexServiceJunit { @Autowired private ElasticSearchClient elasticSearchClient; /** * 保存訂單到ES中 * @param request */ @Test public void saveDocument(){ String indexName = “orderIndex-2022-07”; //從數(shù)據(jù)庫查詢最新訂單數(shù)據(jù),并封裝成對(duì)應(yīng)的es訂單結(jié)構(gòu) String orderId = “202202020202”; OrderIndexDocDTO indexDocDTO = buildOrderIndexDocDTO(orderId); //保存數(shù)據(jù)到ES中 elasticSearchClient.addDocument(indexName, indexDocDTO.getOrderId(), indexDocDTO); }}

    2.5、內(nèi)嵌對(duì)象查詢

    內(nèi)嵌對(duì)象查詢分兩種形式,比如,第一種通過商品、品牌、價(jià)格等條件,分頁查詢訂單數(shù)據(jù);第二種是通過訂單ID、商品、品牌、價(jià)格等,分頁查詢訂單項(xiàng)數(shù)據(jù)。具體的實(shí)踐,請(qǐng)看下文。

    • 通過商品、品牌、價(jià)格等條件,分頁查詢訂單數(shù)據(jù)

    @ActiveProfiles(“dev”)@RunWith(SpringRunner.class)@SpringBootTestpublic class OrderIndexServiceJunit { @Autowired private ElasticSearchClient elasticSearchClient; /** * 通過商品、品牌、價(jià)格等條件,分頁查詢訂單數(shù)據(jù) * @param request */ @Test public void search1(){ //查詢索引,支持通配符 String indexName = “orderIndex-*”; String orderUserName = “張三”; String productName = “薯?xiàng)l”; // 條件搜索 SearchSourceBuilder builder = new SearchSourceBuilder(); //組合搜索 BoolQueryBuilder mainBoolQuery = new BoolQueryBuilder(); mainBoolQuery.must(QueryBuilders.matchQuery(“orderUserName”, orderUserName)); //訂單項(xiàng)相關(guān)信息搜索 BoolQueryBuilder nestedBoolQuery = new BoolQueryBuilder(); nestedBoolQuery.must(QueryBuilders.matchQuery(“orderItems.productName”, productName)); //內(nèi)嵌對(duì)象搜索,需要指定path NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery(“orderItems”,nestedBoolQuery, ScoreMode.None); //子表查詢 mainBoolQuery.must(nestedQueryBuilder); //封裝查詢參數(shù) builder.query(mainBoolQuery); //返回參數(shù) builder.fetchSource(new String[]{}, new String[]{}); //結(jié)果集合分頁,從第一頁開始,返回最多四條數(shù)據(jù) builder.from(0).size(4); //排序 builder.sort(“orderId”, SortOrder.DESC); log.info(“dsl:{}”, builder.toString()); // 執(zhí)行請(qǐng)求 SearchResponse response = elasticSearchClient.searchDocument(indexName, builder); // 當(dāng)前返回的總行數(shù) long count = response.getHits().getTotalHits(); // 返回的具體行數(shù) SearchHit[] searchHits = response.getHits().getHits(); log.info(“response:{}”, response.toString()); }}

    • 通過訂單ID、商品、品牌、價(jià)格等,分頁查詢訂單項(xiàng)數(shù)據(jù)

    @ActiveProfiles(“dev”)@RunWith(SpringRunner.class)@SpringBootTestpublic class OrderIndexServiceJunit { @Autowired private ElasticSearchClient elasticSearchClient; /** * 通過訂單ID、商品、品牌、價(jià)格等,分頁查詢訂單項(xiàng)數(shù)據(jù) * @param request */ @Test public void search2(){ //查詢索引,支持通配符 String indexName = “orderIndex-*”; String orderId = “202202020202”; String productName = “薯?xiàng)l”; // 條件搜索 SearchSourceBuilder builder = new SearchSourceBuilder(); //組合搜索 BoolQueryBuilder mainBoolQuery = new BoolQueryBuilder(); mainBoolQuery.must(QueryBuilders.termQuery(“_id”, orderId)); //訂單項(xiàng)相關(guān)信息搜索 BoolQueryBuilder nestedBoolQuery = new BoolQueryBuilder(); nestedBoolQuery.must(QueryBuilders.matchQuery(“orderItems.productName”, productName)); //內(nèi)嵌對(duì)象搜索,需要指定path NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery(“orderItems”,nestedBoolQuery, ScoreMode.None); //內(nèi)嵌對(duì)象分頁查詢 InnerHitBuilder innerHitBuilder = new InnerHitBuilder(); //結(jié)果集合分頁,從第一頁開始,返回最多四條數(shù)據(jù) innerHitBuilder.setFrom(0).setSize(4); //只返回訂單項(xiàng)id innerHitBuilder.setFetchSourceContext(new FetchSourceContext(true, new String[]{“orderItems.orderItemId”}, new String[]{})); innerHitBuilder.addSort(SortBuilders.fieldSort(“orderItems.orderItemId”).order(SortOrder.DESC)); nestedQueryBuilder.innerHit(innerHitBuilder); //子表查詢 mainBoolQuery.must(nestedQueryBuilder); //封裝查詢參數(shù) builder.query(mainBoolQuery); //返回參數(shù) builder.fetchSource(new String[]{}, new String[]{}); //結(jié)果集合分頁,從第一頁開始,返回最多四條數(shù)據(jù) builder.from(0).size(4); //排序 builder.sort(“orderId”, SortOrder.DESC); log.info(“dsl:{}”, builder.toString()); // 執(zhí)行請(qǐng)求 SearchResponse response = elasticSearchClient.searchDocument(indexName, builder); // 當(dāng)前返回的訂單主表總行數(shù) long count = response.getHits().getTotalHits(); // 返回的訂單主表數(shù)據(jù) SearchHit[] searchHits = response.getHits().getHits(); // 返回查詢的的訂單項(xiàng)分頁數(shù)據(jù) Map = searchHit[0].getInnerHits(); log.info(“response:{}”, response.toString()); }}

    三、小結(jié)

    本文主要以通過商品名稱查詢訂單數(shù)據(jù)為案例,介紹利用 SpringBoot 整合 es 實(shí)現(xiàn)數(shù)據(jù)的高效搜索,內(nèi)容如果難免有些遺漏,歡迎網(wǎng)友指出!

    原文鏈接:https://mp.weixin.qq.com/s/ERwwG9gBY1apk1Q6_9Sr0w

    鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
    用戶投稿
    上一篇 2022年7月13日 18:25
    下一篇 2022年7月13日 18:25

    相關(guān)推薦

    • 淘寶工廠店的東西是正品嗎?淘寶工廠店為什么便宜

      淘工廠直營(yíng)店其實(shí)就是鏈接淘寶賣家與工廠的平臺(tái),直接對(duì)接工廠型的商家,店鋪里的東西大部分價(jià)格都會(huì)很低。那么,淘工廠直營(yíng)店靠譜嗎? 淘工廠直營(yíng)店是靠譜的,但售后問題可能得不到很好的保障…

      2022年11月27日
    • 存儲(chǔ)過程語法(sql server存儲(chǔ)過程語法)

      今天小編給各位分享存儲(chǔ)過程語法的知識(shí),其中也會(huì)對(duì)sql server存儲(chǔ)過程語法進(jìn)行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧! oracle存儲(chǔ)過程基本語法…

      2022年11月26日
    • 數(shù)字看亮點(diǎn)!前十月我國(guó)造船三大指標(biāo)繼續(xù)全球領(lǐng)先

      央視網(wǎng)消息:工業(yè)和信息化部最新數(shù)據(jù)顯示,今年1—10月,我國(guó)造船三大指標(biāo)繼續(xù)保持全球領(lǐng)先。隨著生產(chǎn)節(jié)奏加快,船企產(chǎn)業(yè)集中度進(jìn)一步提升。 2022年1—10月,全國(guó)造船完工量、新接訂…

      2022年11月25日
    • 前十個(gè)月我國(guó)造船業(yè)三大指標(biāo)穩(wěn)居世界第一

      今年1—10月,我國(guó)造船業(yè)在國(guó)際市場(chǎng)的份額繼續(xù)穩(wěn)居世界第一。一批高技術(shù)高附加值船舶實(shí)現(xiàn)了批量接單、批量生產(chǎn)。 近日,中國(guó)船舶上海船舶研究設(shè)計(jì)院又新接了韓國(guó)船東三艘汽車運(yùn)輸船的設(shè)計(jì)追…

      2022年11月25日
    • 客服的崗位職責(zé)怎么寫(客服工作內(nèi)容及職責(zé))

      各位小伙伴們大家周一好,又到了每周一給大家分享干貨內(nèi)容的時(shí)候啦~ 本期來跟大家分享一下客服工作管理流程以及客服崗位里面的每項(xiàng)職能崗位的核心細(xì)則,也是干貨滿滿推薦收藏~ 一.補(bǔ)償流程…

      2022年11月25日
    • 商家收到貨才會(huì)退款嗎(淘寶代付款退款錢到哪里了)

      在淘寶上有一些人下單購買商品的時(shí)候是通過代付的形式來支付的,一般情況下是家長(zhǎng)幫助家里的小孩或者長(zhǎng)輩進(jìn)行代付,而代付訂單和普通的訂單沒有太大的區(qū)別,不過如果發(fā)生退款的話,錢是退到哪里…

      2022年11月25日
    • 什么是推廣cpa一篇文章帶你看懂CPA推廣渠道

      CPA渠道 CPA指的是按照指定的行為結(jié)算,可以是搜索,可以是注冊(cè),可以是激活,可以是搜索下載激活,可以是綁卡,實(shí)名認(rèn)證,可以是付費(fèi),可以是瀏覽等等。甲乙雙方可以根據(jù)自己的情況來定…

      2022年11月25日
    • 抖音直播帶貨有哪些方法技巧(抖音直播帶貨有哪些痛點(diǎn))

      如今抖音這個(gè)短視頻的變現(xiàn)能力越來越突顯了,尤其是在平臺(tái)上開通直播,更具有超強(qiáng)的帶貨屬性,已經(jīng)有越來越多的普通人加入到其中了。不過直播帶貨雖然很火,但是也不是每個(gè)人都能做好的,那么在…

      2022年11月24日
    • 銳龍97900x參數(shù)規(guī)格跑分評(píng)測(cè) 銳龍97900x屬于什么檔次

      銳龍9 7900X是銳龍7000系列處理器中性能頂尖的型號(hào)之一,它采用了這一代標(biāo)配的zen4架構(gòu)和5nm制程工藝,那么它具體的參數(shù)跑分如何,在電腦上世紀(jì)發(fā)揮怎么樣呢,下面就來看看銳…

      2022年11月24日
    • 園屬于什么結(jié)構(gòu)(園的結(jié)構(gòu)和部首)

      園 yuán:全包圍結(jié)構(gòu),平穩(wěn)端正中稍帶左收右展。 外部“口” 體態(tài)端莊,稍抗肩,稍帶左輕右重。左豎起筆稍抖,豎身勿重,稍左斜,垂露收筆;第二筆橫折壓著左豎起筆,橫畫稍抗肩,不要重…

      2022年11月24日

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息