报错信息如下:Use ElasticsearchException.getFailedDocuments() for detailed messages [{yjZ8D0oB=ElasticsearchException[Elasticsearch exception [type=cluster_block_exception, reason=index [au_report] blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];]]
分析:看报错信息大概意思就是es的写入操作被阻塞了,索引只能读、删不能修改新增了;我总结了这种错误的解决方案以及步骤,如下:
- curl --location --request PUT 'http://127.0.0.1:9200/au_report/_settings' \
- --header 'Content-Type: application/json' \
- --data-raw '{
- "index.blocks.read_only_allow_delete": null
- }'
read_only_allow_delete表示es只读和允许删除不能做修改操作,当磁盘空间达到95%时自动为true;
curl --location --request GET 'http://127.0.0.1:9200/_cat/allocation?v'
我使用的spring-data-elasticsearch的版本是4.3.4,其实只要在ES系列一之java端API操作;分页查询的测试方法queryPageData()基础上新增HighlightBuilder
设置需要高亮的字段然后设置到返回结果去即可,下面贴上查询代码:
- @Test
- public void queryPageData(){
- // 此处应是查询参数,这里单元测试没有赋值
- TestQuery query = new TestQuery();
- query.setTextKey("张三");
- PaginationModel<EsTestInfo> res = new PaginationModel<>();
- int currentPage=query.getPageIndex()-1;
- int pageSize = query.getPageSize();
- PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
- BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
- BoolQueryBuilder textKeyBqb = new BoolQueryBuilder();
- if (StringUtils.isNotBlank(query.getTextKey())) {
- /**
- * 由于下面会用到must查询,所以此处用textKeyBqb再封装一个builder出来,否则
- * 和must同时查询此处会出现0匹配也返回结果的情况
- * 如果不想封装textKeyBqb,加上boolQueryBuilder.minimumShouldMatch(1)强制使es
- * 最少满足一个should子句才能返回结果也行
- */
- textKeyBqb.should(QueryBuilders.matchQuery("id", query.getTextKey()))
- .should(QueryBuilders.matchQuery("name", query.getTextKey()))
- .should(QueryBuilders.matchQuery("desc", query.getTextKey()));
- }
- if (Objects.nonNull(query.getStartDate()) && Objects.nonNull(query.getEndDate())) {
- RangeQueryBuilder timeRangeQuery = QueryBuilders.rangeQuery("publishDt")
- .gte(query.getStartDate().getTime())
- .lte(query.getEndDate().getTime());
- boolQueryBuilder.must(timeRangeQuery);
- }
- if (Objects.nonNull(query.getRptStatus())) {
- boolQueryBuilder.must(QueryBuilders.matchQuery("rptStatus", query.getRptStatus()));
- }
- // 将上面封装的子句加入到主查询条件中
- boolQueryBuilder.must(textKeyBqb);
- log.info("<<<<<<<<<<<<<<<<<
,boolQueryBuilder); - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
- .withQuery(boolQueryBuilder).withPageable(pageRequest)
- // 设置需要高亮的字段
- .withHighlightFields(
- new HighlightBuilder.Field("name")
- ,new HighlightBuilder.Field("desc"))
- // 设置高亮的html格式
- .withHighlightBuilder(new HighlightBuilder().preTags("").postTags("")).build();
- // 解决es一次只返回10000条数据的问题
- searchQuery.setTrackTotalHits(true);
- SearchHits<EsTestInfo> search = elasticsearchRestTemplate.search(searchQuery, EsTestInfo.class);
- List<EsTestInfo> list = new ArrayList<>();
- for (SearchHit<EsTestInfo> productSearchHit : search) {
- EsTestInfo pro = productSearchHit.getContent();
- //获取高亮的字段集合
- Map<String, List<String>> highlightFields = productSearchHit.getHighlightFields();
- //将高亮的内容填充到pro中
- pro.setName(highlightFields.get("name")==null ? pro.getName():highlightFields.get("name").get(0));
- pro.setDesc(highlightFields.get("desc")==null ? pro.getDesc():highlightFields.get("desc").get(0));
- System.out.println("pro = " + pro);
- list.add(pro);
- }
- res.setList(list);
- res.setTotal(search.getTotalHits());
- res.setPageIndex(query.getPageIndex());
- res.setPageSize(query.getPageSize());
- System.out.println("res = " + res);
- }
C# 折叠 复制 全屏
测试结果如下:
这次更新的问题到此结束,后续遇到新的问题会继续补充......