Elasticsearch 的使用

一、简介 

1.Shard(分片)
数据分散集群的架构模式,Elasticsearch 将一个 Index(索引)中的数据切为多个 Shard(分片),分布在不同服务器节点上。
默认每个索引会分配5个主分片和1个副本分片,可根据需要调整主分片和副本分片的数量。

2.Replica(副本)
主从架构模式,每个Shard(分片)创建多个备份——Replica副本,保证数据不丢失。

1.主分片和副本分片数量的调整
PUT /my-index/_settings
{
  "number_of_shards": 3,
  "number_of_replicas": 2
}

2.新建索引时设置分片
PUT /my-index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}

1.1、数据类型 

1.1.1、常见数据类型 

字符串型:text(分词)、keyword(不分词)

数值型:long、integer、short、byte、double、float、half_float、scaled_float

日期类型:date

布尔类型:boolean

二进制类型:binary

范围类型:integer_range、float_range、long_range、double_range、date_range

1.1.2、复杂数据类型 

数组类型:array

对象类型:object

嵌套类型:nested object

1.1.3、特殊数据类型 

地理位置数据类型:geo_point(点)、geo_shape(形状) 

记录IP地址ip 

实现自动补全completion 

记录分词数:token_count 

记录字符串hash值murmur3 

多字段特性multi-fields 

1.2、工作流程 

1.2.1、路由 

ES采用 hash 路由算法,对 document 的 id 标识进行计算,产生 shard 序号,通过序号可立即确定shard。

1.2.2、写入流程 

1.A节点接到请求,计算路由,转发"对应节点"。
2."对应节点"处理完数据后,数据同步到副本节点。
3.A节点收到"对应节点"的响应,将结果返回给调用者。

1.2.3、读取流程 

1.协调节点接到请求,计算路由,用round-robin算法,在对应的primary shard及其所有replica中随机选择一个发送请求。
3.协调节点收到"对应节点"的响应,将结果返回给调用者。

二、工作原理流程 

2.1、到排序索引 

 

到排序索引

2.2、分词器 

ES内置分词器:standard analyzer、simple analyzer、whitespace analyzer、language analyzer 

对于document中的不同字段类型,ES采用不同的分词器进行处理,如date类型不会分词要完全匹配,text类型会分词。

2.2.1、常用的中文分词器:IK分词器 

7.6.0版本的IK:https://github.com/medcl/elasticsearch-analysis-ik/releases
解压缩放到YOUR_ES_ROOT/plugins/ik/目录下,重启Elasticsearch即可。 

1、IK分词器的两种分词模式(一般用 ik_max_word) 

ik_max_word:会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”等等,会穷尽各种可能的组合。
ik_smart:只做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。

PUT /my_index 
{"mappings": {"properties": {"text": {"type": "text","analyzer": "ik_max_word"}}}
}

2、配置文件 

IK的配置文件存在于YOUR_ES_ROOT/plugins/ik/config目录下
main.dic: IK原生内置的中文词库,总共有27万多条,只要是这些单词,都会被分在一起;
quantifier.dic: 放了一些单位相关的词;
suffix.dic: 放了一些后缀;
surname.dic: 中国的姓氏;
stopword.dic: 英文停用词。 

2.3、数据同步机制 

one、all、quorum(默认),可在请求时带上consistency参数表明采用哪种模式。

one 模式 
有一个primary shard是active活跃可用,操作算成功。

all 模式 
必须所有的primary shard和replica shard都是活跃的,操作算成功。

quorum 模式 
确保大多数shard可用,不满足条件时,会默认等1分钟,超间就报timeout错,可在写时加timeout
PUT /index/type/id?timeout=30

2.4、数据持久化策略 

1.数据先写入 in-memory buffer(应用内存)中,同时写入 translog 日志文件(日志内存每5秒刷到磁盘)。
2.每隔1秒,ES会执行一次 refresh 操作:将buffer中的数据refresh到filesystem cache的(os cache系统内存)中的segment file中(可被检索到)。
3 每隔30分钟将内存数据flush到磁盘,或者translog大到一定程度时,会触发 flush 操作。
可设置index的index.translog.durability参数,使每次写入一条数据,都写入buffer,同时fsync写入磁盘上的translog文件。

三、使用 

2.1、语法规则 

2.2、ES的 DSL 语法

1、创建索引(HTTP请求)
shopping:索引名称

# 1.创建索引(等于创建数据库,PUT请求)
http://127.0.0.1:9200/shopping# 2.获取索引(GET请求)
http://127.0.0.1:9200/shopping# 3.删除索引(DELETE请求)
http://127.0.0.1:9200/shopping

2、文档数据的创建

# 1.往索引里新增数据(不自定义ID:POST,传JSON)
http://127.0.0.1:9200/shopping/_doc/# 2.往索引里新增数据(自定义ID:POST、PUT,传JSON)
http://127.0.0.1:9200/shopping/_doc/123
http://127.0.0.1:9200/shopping/_create/123

3.修改

# 1.全量修改(PUT、POST)
http://127.0.0.1:9200/shopping/_doc/123
{"name":"haige","age",123
}
# 2.局部修改(POST)
http://127.0.0.1:9200/shopping/_update/123
{"doc" :{"name":"haige",}
}

4、主键查询 & 全查询

# 查询主键单数据(GET)
http://127.0.0.1:9200/shopping/_doc/123
# 查询全部数据(GET)
http://127.0.0.1:9200/shopping/_search

5.多条件查询,范围查询

http://127.0.0.1:9200/shopping/_search
{"query":{"bool" :{"should" :[{"match" :{"name":"测试"}}],"filter" :{"range":{"age":{"gt" : 20}}}}}
}

6.分页查询、排序(且只显示name字段)

http://127.0.0.1:9200/shopping/_search
{"query":{"match":{"name":"哈喽"}},"from":0,"size":2,"_source" : ["name"],"sort" : {"age" : {"order" : "desc"}}
}

2.3、org.elasticsearch.client 客户端 

2.3.1、引入依赖 

<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.5.0</version><exclusions><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.5.0</version>
</dependency>
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.5.0</version>
</dependency>

 2.3.2、SearchRequest 、SearchSourceBuilder 、QueryBuilder 、SearchResponse 、SearchHit组件常用设置

public static void testRequest()throws Exception{// 创建请求对象,设置查询多个文档库,也可指定单个文档库。SearchRequest request = new SearchRequest("index01","index02","index03");// 也可通过 indices 方法指定文档库中request.indices("posts01","posts02", "posts03");// 设置指定查询的路由分片request.routing("routing");// 指定优先去某个分片上去查询(默认的是随机先去某个分片)request.preference("_local");// 设置缓存request.requestCache();// 取出查询语句request.toString();
}public static void testSource()throws Exception{//创建源SearchSourceBuilder source= new SearchSourceBuilder();// 第几页source.from(0);// 每页多少条数据(默认是10条)source.size(100);// 设置排序规则source.sort(new ScoreSortBuilder().order(SortOrder.DESC));source.sort(new FieldSortBuilder("id").order(SortOrder.ASC));//获取的字段(列)和不需要获取的列String[] includeFields = new String[]{"birthday","name"};String[] excludeFields = new String[]{"age","address"};source.fetchSource(includeFields,excludeFields);// 设置超时时间source.timeout(new TimeValue(60, TimeUnit.SECONDS));source.highlighter();// 高亮source.aggregation(AggregationBuilders.terms("by_company"));// 聚合//分词查询source.profile(true);source.query();
}public static void testBuilder()throws Exception{//全匹配(查出全部)MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();//匹配查询MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("","").analyzer("");//匹配文本查询MatchPhraseQueryBuilder matchPhraseQuery = QueryBuilders.matchPhraseQuery("","");//匹配文本前缀查询MatchPhrasePrefixQueryBuilder matchPhrasePrefixQuery = QueryBuilders.matchPhrasePrefixQuery("","");//判断莫子是否有值(String)ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery("");//前缀查询PrefixQueryBuilder prefixQuery = QueryBuilders.prefixQuery("","");//精确查询TermQueryBuilder termQuery = QueryBuilders.termQuery("","");//范围查询RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("birthday").from("2016-01-01 00:00:00");QueryStringQueryBuilder queryBuilder009 = QueryBuilders.queryStringQuery("");QueryBuilders.disMaxQuery();HighlightBuilder highlightBuilder = new HighlightBuilder();HighlightBuilder.Field highlightTitle =new HighlightBuilder.Field("title");highlightTitle.highlighterType("unified");highlightBuilder.field(highlightTitle);HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");highlightBuilder.field(highlightUser);// 组合器BoolQueryBuilder builder = QueryBuilders.boolQuery();//过滤builder.filter();//且builder.must();//非builder.mustNot();//或builder.should();
}public static void testResponse()throws Exception {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest searchRequest = new SearchRequest("user");// 同步SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);RestStatus status = response.status();TimeValue took = response.getTook();Boolean terminatedEarly = response.isTerminatedEarly();boolean timedOut = response.isTimedOut();int totalShards = response.getTotalShards();int successfulShards = response.getSuccessfulShards();int failedShards = response.getFailedShards();for (ShardSearchFailure failure : response.getShardFailures()) {// failures should be handled here}// 异步ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {@Overridepublic void onResponse(SearchResponse searchResponse) {}@Overridepublic void onFailure(Exception e) {}};client.searchAsync(searchRequest, RequestOptions.DEFAULT, listener);
}public static void testHits()throws Exception {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest searchRequest = new SearchRequest("user");// 同步SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);SearchHits hits = response.getHits();TotalHits totalHits = hits.getTotalHits();//总数long numHits = totalHits.value;//TotalHits.Relation relation = totalHits.relation;float maxScore = hits.getMaxScore();SearchHit[] searchHits = hits.getHits();for (SearchHit hit : searchHits) {String index = hit.getIndex();String id = hit.getId();float score = hit.getScore();String sourceAsString = hit.getSourceAsString();Map<String, Object> sourceAsMap = hit.getSourceAsMap();String documentTitle = (String) sourceAsMap.get("title");List<Object> users = (List<Object>) sourceAsMap.get("user");Map<String, Object> innerObject =(Map<String, Object>) sourceAsMap.get("innerObject");}// 高亮获取for (SearchHit hit : response.getHits()) {Map<String, HighlightField> highlightFields = hit.getHighlightFields();HighlightField highlight = highlightFields.get("title");Text[] fragments = highlight.fragments();String fragmentString = fragments[0].string();}// 获取聚合结果Aggregations aggregations = response.getAggregations();Terms byCompanyAggregation = aggregations.get("by_company");Terms.Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic");Avg averageAge = elasticBucket.getAggregations().get("average_age");double avg = averageAge.getValue();// 获取大量聚合结果Map<String, Aggregation> aggregationMap = aggregations.getAsMap();Terms companyAggregation = (Terms) aggregationMap.get("by_company");List<Aggregation> aggregationList = aggregations.asList();for (Aggregation agg : aggregations) {String type = agg.getType();if (type.equals(TermsAggregationBuilder.NAME)) {Terms.Bucket elasticBucket2 = ((Terms) agg).getBucketByKey("Elastic");long numberOfDocs = elasticBucket2.getDocCount();}}
}

2.3.3、 增删改

//单条增
public static void addDocment()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));//Map提供供文档源Map<String, Object> jsonMap = new HashMap<>();jsonMap.put("name", "小红");jsonMap.put("sex", "女");jsonMap.put("age", 22);jsonMap.put("birthDay", new Date());jsonMap.put("message", "测试");IndexRequest indexRequest1 = new IndexRequest("user2", "doc", "5").source(jsonMap);// 同步执行IndexResponse indexResponse1 =client.index(indexRequest1,RequestOptions.DEFAULT);client.close();//XContentBuilder提供供文档源XContentBuilder builder = XContentFactory.jsonBuilder();builder.startObject();{builder.field("name", "South");builder.timeField("birthDay", new Date());builder.field("message", "第二个小demo");}builder.endObject();IndexRequest indexRequest2 = new IndexRequest("user", "doc", "2").source(builder);// 同步执行IndexResponse indexResponse2 =client.index(indexRequest2,RequestOptions.DEFAULT);String index = indexResponse1.getIndex();String type = indexResponse1.getType();String id = indexResponse1.getId();long version = indexResponse1.getVersion();RestStatus restStatus = indexResponse1.status();DocWriteResponse.Result result = indexResponse1.getResult();ReplicationResponse.ShardInfo shardInfo = indexResponse1.getShardInfo();client.close();
}//删
public void deleteTest()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));DeleteRequest request = new DeleteRequest("posts","1");DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
}//单个改
public static void updateDocment()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));Map<String, Object> jsonMap = new HashMap<>();jsonMap.put("name", "JunSouth");UpdateRequest updateRequest = new UpdateRequest("user","doc","6").doc(jsonMap);UpdateResponse updateResponse  =client.update(updateRequest,RequestOptions.DEFAULT);String index = updateResponse.getIndex();String type = updateResponse.getType();String id = updateResponse.getId();long version = updateResponse.getVersion();System.out.println("index:"+index+"  type:"+type+"   id:"+id+"   version:"+version);if(updateResponse.getResult() == DocWriteResponse.Result.CREATED) {System.out.println("文档已创建");}else if(updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {System.out.println("文档已更新");}else if(updateResponse.getResult() == DocWriteResponse.Result.DELETED) {System.out.println("文档已删除");}else if(updateResponse.getResult() == DocWriteResponse.Result.NOOP) {System.out.println("文档不受更新的影响");}client.close();
}//批量操作
public static void bulkDocment()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));BulkRequest bulkRequest = new BulkRequest();bulkRequest.add(new IndexRequest("user","doc","5").source(XContentType.JSON,"name", "test")); // 将第一个 IndexRequest 添加到批量请求中bulkRequest.add(new IndexRequest("user","doc","6").source(XContentType.JSON,"name","test")); // 第二个BulkResponse bulkResponse = client.bulk(bulkRequest,RequestOptions.DEFAULT);boolean falgs = bulkResponse.hasFailures();    // true 表示至少有一个操作失败System.out.println("falgs: "+falgs);for (BulkItemResponse bulkItemResponse : bulkResponse) { // 遍历所有的操作结果DocWriteResponse itemResponse = bulkItemResponse.getResponse(); // 获取操作结果的响应,可以是 IndexResponse,UpdateResponse or DeleteResponse,它们都可以惭怍是 DocWriteResponse 实例。if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {IndexResponse indexResponse = (IndexResponse) itemResponse;System.out.println("index 操作后的响应结果");}else if(bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {UpdateResponse updateResponse = (UpdateResponse) itemResponse;System.out.println("update 操作后的响应结果");}else if(bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {DeleteResponse deleteResponse = (DeleteResponse) itemResponse;System.out.println("delete 操作后的响应结果");}}for (BulkItemResponse bulkItemResponse : bulkResponse) {if (bulkItemResponse.isFailed()) {                                      // 检测给定的操作是否失败BulkItemResponse.Failure failure = bulkItemResponse.getFailure();System.out.println("获取失败信息: "+failure);}}client.close();
}

2.3.4、查

//查询某索引下全部数据
public static void searchAll()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest searchRequest = new SearchRequest("user");  // 设置搜索的 index 。QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(queryBuilder); //设置搜索,可以是任何类型的 QueryBuilder.searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);SearchHits hits = searchResponse.getHits();float maxScore = hits.getMaxScore();for (SearchHit hit : hits.getHits()) {System.out.println("hit: "+hit);String sourceAsString = hit.getSourceAsString();Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");System.out.println("name: "+name);}client.close();//匹配查询器QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy").fuzziness(Fuzziness.AUTO).prefixLength(3).maxExpansions(10);searchSourceBuilder.query(matchQueryBuilder);//高亮HighlightBuilder highlightBuilder = new HighlightBuilder();HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("name"); // title 字段高亮highlightTitle.highlighterType("unified");  // 配置高亮类型highlightBuilder.field(highlightTitle);  // 添加到 builderHighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");highlightBuilder.field(highlightUser);searchSourceBuilder.highlighter(highlightBuilder);
}//普通条件查询
public static void search01()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest searchRequest = new SearchRequest("user");  // 设置搜索的 index 。// 查询器QueryBuilder queryBuilder01 = QueryBuilders.termQuery("name", "test"); //完全匹配QueryBuilder queryBuilder02 =QueryBuilders.fuzzyQuery("name", "t");    //模糊查询QueryBuilder queryBuilder03 =QueryBuilders.prefixQuery("name", "小"); //前缀查询QueryBuilder queryBuilder04 =QueryBuilders.matchQuery("name", "小");    //匹配查询WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name","*jack*");//搜索名字中含有jack文档(name中只要包含jack即可)// 搜索器(排序、分页...)。SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(queryBuilder04);   // 设置搜索条件searchSourceBuilder.from(0); // 起始 indexsearchSourceBuilder.size(5); // 大小 size//  searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 设置搜索的超时时间//  searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // 根据分数 _score 降序排列 (默认行为)//  searchSourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC));  // 根据 id 降序排列searchRequest.source(searchSourceBuilder); // 将 SearchSourceBuilder  添加到 SeachRequest 中。SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);SearchHits hits = searchResponse.getHits();float maxScore = hits.getMaxScore();for (SearchHit hit : hits.getHits()) {String sourceAsString = hit.getSourceAsString();Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");System.out.println("hit: "+hit);System.out.println("name: "+name);}client.close();
}// 聚合查询
public static void search02()throws Exception{RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest searchRequest = new SearchRequest("user2");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// 根据 sex 字段分组TermsAggregationBuilder aggregation = AggregationBuilders.terms("my_sex").field("sex.keyword");aggregation.subAggregation(AggregationBuilders.avg("avg_age").field("age")); // age(统计的字段)需是数值型aggregation.subAggregation(AggregationBuilders.max("max_age").field("age"));aggregation.subAggregation(AggregationBuilders.min("min_age").field("age"));searchSourceBuilder.aggregation(aggregation);searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);Aggregations aggregations = searchResponse.getAggregations();Terms sexTerms = aggregations.get("my_sex");//获取每组的信息for (Terms.Bucket bucket : sexTerms.getBuckets()) {System.out.println("分组的字段名: " + bucket.getKeyAsString());System.out.println("每组数量: " + bucket.getDocCount());}//求平均Terms.Bucket elasticBucket1 = sexTerms.getBucketByKey("女");Avg averageAge1 = elasticBucket1.getAggregations().get("avg_age");double avg1 = averageAge1.getValue();System.out.println("女性平均年龄:"+avg1);Terms.Bucket elasticBucket2 = sexTerms.getBucketByKey("男");Avg averageAge2 = elasticBucket2.getAggregations().get("avg_age");double avg2 = averageAge2.getValue();System.out.println("男性平均年龄:"+avg2);//求最大最小Terms.Bucket elasticBucket3 = sexTerms.getBucketByKey("女");Max maxAge3 = elasticBucket3.getAggregations().get("max_age");double maxAge = maxAge3.getValue();System.out.println("女性最大年龄:"+maxAge);Terms.Bucket elasticBucket4 = sexTerms.getBucketByKey("女");Min maxAge4 = elasticBucket4.getAggregations().get("min_age");double minAge = maxAge4.getValue();System.out.println("女性最大年龄:"+minAge);client.close();
}// 多查询
public static void multiSearch()throws Exception{MultiSearchRequest multiSearchRequest = new MultiSearchRequest();  // 查两个张索引SearchRequest firstSearchRequest = new SearchRequest("user");   SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(QueryBuilders.matchQuery("name", "大黑"));firstSearchRequest.source(searchSourceBuilder);multiSearchRequest.add(firstSearchRequest);SearchRequest secondSearchRequest = new SearchRequest("car");  searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(QueryBuilders.matchQuery("weight", "3T"));secondSearchRequest.source(searchSourceBuilder);multiSearchRequest.add(secondSearchRequest);// 取值1MultiSearchResponse multiSearchResponse = client.msearch(multiSearchRequest,RequestOptions.DEFAULT);MultiSearchResponse.Item firstResponse = multiSearchResponse.getResponses()[0];                                 SearchResponse firstSearchResponse = firstResponse.getResponse();        for (SearchHit hit : firstSearchResponse.getHits()) {Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");}MultiSearchResponse.Item secondResponse = response.getResponses()[1];  SearchResponse secondSearchResponse = secondResponse.getResponse();for (SearchHit hit : secondSearchResponse.getHits()) {Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("weight");}// 取值2for (MultiSearchResponse.Item item : multiSearchResponse.getResponses()) {SearchResponse response = item.getResponse();for (SearchHit hit : response.getHits()) {String index=hit.getIndex();//根据不同索引名作不同的处理。if(index.equals("user")){Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");}else if(index.equals("car")){Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("weight");}}}//滚动查询
public static void scrollSerach()throws Exception{System.out.print("11111111111111111");RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest searchRequest = new SearchRequest("user");  // 设置搜索的 index 。QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(queryBuilder); //设置搜索,可以是任何类型的 QueryBuilder.//设置每次查询数量searchSourceBuilder.size(3);//设置滚动等待时间final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1));searchRequest.scroll(scroll);searchRequest.source(searchSourceBuilder);//第一次获取查询结果SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);String scrollId = searchResponse.getScrollId();SearchHit[] searchHits = searchResponse.getHits().getHits();for (SearchHit hit : searchHits) {Map<String, Object> sourceAsMap = hit.getSourceAsMap();System.out.print("第一次获取查询结果,此处可做一些操作。");String name = (String) sourceAsMap.get("name");System.out.println("name: "+name);}//遍历剩余结果while (searchHits != null && searchHits.length > 0) {SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);scrollRequest.scroll(scroll);searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);scrollId = searchResponse.getScrollId();searchHits = searchResponse.getHits().getHits();for (SearchHit hit : searchHits) {Map<String, Object> sourceAsMap = hit.getSourceAsMap();System.out.print("遍历剩余结果,此处可做一些操作。");String name = (String) sourceAsMap.get("name");System.out.println("name: "+name);}}// 清除游标ClearScrollRequest clearScrollRequest = new ClearScrollRequest();clearScrollRequest.addScrollId(scrollId);ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);boolean succeeded = clearScrollResponse.isSucceeded();client.close();}
}

四、性能调优 

4.1、查询优化 

4.2、分页内部读取流程

4.3、

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/173031.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#的函数

可以发现首字母一般为动词 可以看出void是返回空值 这个就不是放回空值了 例如 函数修饰符 1、如上面的实例一样——无修饰符&#xff1a;如果一个参数没有用参数修饰符标记&#xff0c;则认为它将按值进行传递&#xff0c;这将以为着被调用的方法收到原始数据的一份副本。&am…

谈谈Redis的几种经典集群模式

目录 前言 主从复制 哨兵模式 分片集群 前言 Redis集群是一种通过将多个Redis节点连接在一起以实现高可用性、数据分片和负载均衡的技术。它允许Redis在不同节点上同时提供服务&#xff0c;提高整体性能和可靠性。在Redis中提供集群方案总共有三种&#xff1a;主从复制、…

PyQt6把QTDesigner生成的UI文件转成python源码,并运行

锋哥原创的PyQt6视频教程&#xff1a; 2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~共计18条视频&#xff0c;包括&#xff1a;2024版 PyQt6 Python桌面开发 视频教程(无废话版…

CSDN动态发了但是主页面看不见已发的动态

问题描述&#xff1a; 今天在写csdn动态的时候&#xff0c;发了五个动态&#xff0c;但是主页面的“最近”看不到我发的动态&#xff0c;我还以为是csdn动态每天的发送量有数量限制。去这个地方点我的发现 右上角全是“审核中”的字样 按理说是不可能审核这么久的&#xff08…

Linux常用命令——bc命令

在线Linux命令查询工具 bc 算术操作精密运算工具 补充说明 bc命令是一种支持任意精度的交互执行的计算器语言。bash内置了对整数四则运算的支持&#xff0c;但是并不支持浮点运算&#xff0c;而bc命令可以很方便的进行浮点运算&#xff0c;当然整数运算也不再话下。 语法 …

IO和NIO的区别 BIO,NIO,AIO 有什么区别? Files的常用方法都有哪些?

文章目录 IO和NIO的区别BIO,NIO,AIO 有什么区别?Files的常用方法都有哪些&#xff1f; 今天来对java中的io, nio, bio, aio进行了解&#xff0c;有何区别。 IO和NIO的区别 NIO与IO区别 IO是面向流的&#xff0c;NIO是面向缓冲区的Java IO面向流意味着每次从流中读一个或多个字…

酷开科技 | 酷开系统,让你与家人共度美好时光!

在日渐繁忙的生活中&#xff0c;我们常常会忽略和家人朋友的相处时光&#xff0c;有时候&#xff0c;我们亟需一种休闲方式&#xff0c;让家庭成员能够围坐在一起&#xff0c;享受无忧无虑的温馨和欢笑。酷开科技&#xff0c;致力于为消费者提供舒适的产品和服务内容&#xff0…

岂曰无衣 汉家衣裳再现锦绣美景

——福州第五届1122汉服节出行盛大开幕11月25日下午&#xff0c;闽江之心海丝广场&#xff0c;一场盛大的汉服文化节——福州第五届1122汉服节出行活动在这里隆重开幕。这个被誉为“穿在身上的历史”的传统文化&#xff0c;在这片古老而神秘的土地上焕发出新的生机与活力。据了…

Win10电脑用U盘重装系统的步骤

在Win10电脑中&#xff0c;用户遇到了无法解决的系统问题&#xff0c;用户这时候就可以考虑重装Win10系统&#xff0c;这样即可轻松解决问题&#xff0c;从而满足自己的操作需求。接下来小编给大家详细介绍关于Win10电脑中用U盘重装系统的教程步骤。 准备工作 1. 一台正常联网可…

【古诗生成AI实战】之二——项目架构设计

[1] 项目架构 在我们深入古诗生成AI项目的具体实践之前&#xff0c;让我们首先理解整个项目的架构。本项目的代码流程主要分为三个关键阶段&#xff1a; 1、数据处理阶段&#xff1b;   2、模型训练阶段&#xff1b;   3、文本生成阶段。 第一步&#xff1a;在数据处理阶段…

免费分享一套基于springboot的餐饮美食分享平台系统,挺漂亮的

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的基于springboot的餐饮美食分享平台系统&#xff0c;分享下哈。 项目视频演示 【免费】基于springboot的餐饮美食分享平台 Java毕业设计_哔哩哔哩_bilibili【免费】基于springboot的餐饮美食分享平台 Java毕…

【SQL Server2019SSMS】安装与卸载手册

目录 &#x1f4cb;前言 ⛳️【SQL Serverssms】安装 1. SQL Server自定义安装 2. SSMS安装 ⛳️【SQL Server】卸载 &#x1f4cb;前言 &#x1f308;个人主页&#xff1a;Sarapines Programmer &#x1f525; 系列专栏&#xff1a;本期文章收录在《宝藏工具使用手册》&am…

校园导游程序及通信线路设计(结尾附着总源码)

校园导游程序及通信线路设计 摘  要 新生或来访客人刚到校园&#xff0c;对校园的环境不熟悉。就需要一个导游介绍景点&#xff0c;推荐到下一个景点的最佳路径等。随着科技的发展&#xff0c;社会的进步&#xff0c;人们对便捷的追求也越来越高。为了减少人力和时间。针对对…

skywalking 简单操作文档

1.1. 基础概念 1.1.1. 概述 SkyWalking是 apache基金会下面的一个开源 APM项目&#xff0c;为微服务架构和云原生架构系统设计。它通过探针自动收集所需的指标&#xff0c;并进行分布式追踪。通过这些调用链路以及指标&#xff0c;Skywalking APM会感知应用间关系和服务间关系…

Deep Learning(wu--46)

文章目录 ContentsBeginBasic逻辑回归SGD导数计算图&#xff08;反向传播&#xff09;向量化广播numpy Neural Network向量化激活函数梯度下降深层表示反向传播 Contents Begin Basic 逻辑回归 SGD 导数 计算图&#xff08;反向传播&#xff09; 向量化 广播 numpy Neural Netw…

Elastic Search的RestFul API入门:初识mapping

本节课旨在探讨Elasticsearch中Mapping的使用。在Elasticsearch中&#xff0c;Mapping是定义索引中字段&#xff08;Field&#xff09;的数据类型和属性的过程。它为Elasticsearch提供了一种途径&#xff0c;以解析和处理文档中的各个字段&#xff0c;以便在搜索、排序和聚合等…

ElasticSearch学习笔记(狂神说)

ElasticSearch学习笔记&#xff08;狂神说&#xff09; 视频地址&#xff1a;https://www.bilibili.com/video/BV17a4y1x7zq 在学习ElasticSearch之前&#xff0c;先简单了解一下Lucene&#xff1a; Doug Cutting开发是apache软件基金会 jakarta项目组的一个子项目是一个开放…

【漏洞复现】熊海cms 存在sql注入 附poc

漏洞描述 熊海CMS 是由熊海开发的一款可广泛应用于个人博客,个人网站,企业网站的一套网站综合管理系统。 其采用前后端整合设计思路,php,Apache,mysql,前端使用Bootstrap和少许jquery前端框架开发; 网站样式设计简洁大方,整体功能点并不多,但功能正好够用;拥有一个…

jenkins流水线(pipline)实例

1、pipline 语法介绍 声明式的pipeline语法格式 1. 所有的声明都必须包含在pipeline{}中 2. 块只能有节段&#xff0c;指令&#xff0c;步骤或者赋值语句组成 3. 阶段&#xff1a;agent&#xff0c;stages&#xff0c;post&#xff0c;steps 4. 指令&#xff1a;environment&a…

LiveVIS视图库1400-如何切换数据库?默认使用的数据库是什么?如何切换到Mysql/MariaDB?

LiveVIS视图库1400-如何切换数据库&#xff1f;默认使用的数据库是什么&#xff1f;如何切换到Mysql/MariaDB? 1、切换成Mysql/Mariadb数据库1.1 连接数据库1.2 创建数据库实例1.3 配置.ini文件1.4 重启完成切换 1、切换成Mysql/Mariadb数据库 LiveVIS 默认使用 sqlite3 文件…