ElasticSearch常用操作

1、java-api

        SearchRequest request = new SearchRequest(index);
        // 构建一个SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 添加查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if(StringUtils.isNotBlank(basyName)){
            boolQueryBuilder.must(QueryBuilders.matchQuery("content", basyName).analyzer(ANALYZER)); // 替换为你的字段名和查询关键词
        }
        boolQueryBuilder.must(QueryBuilders.termQuery("hdfsType",StatusEnum.TYPE_SHENJIYIJU.getFlag()));
        searchSourceBuilder.query(
                boolQueryBuilder.must(QueryBuilders.termQuery("status",StatusEnum.STATUS_YES.getFlag()))
                        .must(QueryBuilders.termQuery("fileSourceType",StatusEnum.FILE_SOURCE_TYPE_HDFS.getFlag()))
        );
        /*高亮查询*/
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<span style='color:red;'>");
        highlightBuilder.postTags("</span>");
        highlightBuilder.field("content");
        /*需要highlightBuilder,前面创建好*/
        searchSourceBuilder.highlighter(highlightBuilder);
        // 将SearchSourceBuilder设置到SearchRequest中
        request.source(searchSourceBuilder);
        request.scroll(TimeValue.timeValueMinutes(1L));
        SearchResponse search = null;
        try {
            search = client.search(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SearchHits hits = search.getHits();
        //总条数
        long totalLong =hits.getTotalHits().value;

        SearchHit[] hitsArr = hits.getHits();
        List<FileContentInf> contentInfs = new ArrayList<>();
        for (SearchHit documentFields : hitsArr) {
            String json = documentFields.getSourceAsString();
            FileContentInf inf = JSONObject.parseObject(json, FileContentInf.class);
            if(documentFields.getHighlightFields()!=null && documentFields.getHighlightFields().get("content")!=null){
                Text[] contents = documentFields.getHighlightFields().get("content").getFragments();
                if(contents.length>0){
                    inf.setContentInfHtml(contents[0].toString());
                }
            }
            contentInfs.add(inf);
        }
        String scrollId = search.getScrollId();
        while (hitsArr!=null && hitsArr.length>0){
            // 创建一个新的SearchScrollRequest
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(TimeValue.timeValueMinutes(1L)); // 设置scroll超时

            // 执行scroll查询
            SearchResponse scrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
            scrollId = scrollResponse.getScrollId();
            hitsArr = scrollResponse.getHits().getHits();
            for (SearchHit documentFields : hitsArr) {
                String json = documentFields.getSourceAsString();
                FileContentInf inf = JSONObject.parseObject(json, FileContentInf.class);
                if(documentFields.getHighlightFields()!=null && documentFields.getHighlightFields().get("content")!=null){
                    Text[] contents = documentFields.getHighlightFields().get("content").getFragments();
                    if(contents.length>0){
                        inf.setContentInfHtml(contents[0].toString());
                    }
                }
                contentInfs.add(inf);
            }
        }
        // 清除scroll
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.addScrollId(scrollId);
        client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
// 设置排序规则为降序
                searchSourceBuilder.sort("updateTime.keyword", SortOrder.DESC); // 替换为你要排序的字段名
                // 设置分页参数
                searchSourceBuilder.from((pageResult.getPageNum()-1) * pageResult.getPageSize());
                searchSourceBuilder.size(pageResult.getPageSize());

 2、kibana常用命令

按照条件查找

POST /file_content_inf/_search
{
  "query": {
    "match": {
      "fileId": "f8883aed62c27abd6b20013be23db408"
    }
  }
}

GET /file_content_inf/_search
{
  "query": {
    "match": {
      "content": "文件"
    }
  }
}
 

按照条件删除:
POST /file_content_inf/_delete_by_query
{
  "query": {
    "match": {
      "fileId": "f8883aed62c27abd6b20013be23db408"
    }
  }
}
 

删除索引:

DELETE /file_content_inf
 

上一篇:java八股文面试题


下一篇:计算机系统简述