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

相关推荐
极客数模5 小时前
【2026美赛赛题初步翻译F题】2026_ICM_Problem_F
大数据·c语言·python·数学建模·matlab
编程彩机8 小时前
互联网大厂Java面试:从分布式架构到大数据场景解析
java·大数据·微服务·spark·kafka·分布式事务·分布式架构
vx-bot5556668 小时前
企业微信接口在多租户SaaS平台中的集成架构与数据隔离实践
大数据·架构·企业微信
bubuly10 小时前
软件开发全流程注意事项:从需求到运维的全方位指南
大数据·运维·数据库
xixixi7777712 小时前
基于零信任架构的通信
大数据·人工智能·架构·零信任·通信·个人隐私
Hello.Reader13 小时前
Flink 自适应批执行(Adaptive Batch Execution)让 Batch 作业“边跑边优化”
大数据·flink·batch
LaughingZhu14 小时前
Product Hunt 每日热榜 | 2026-01-31
大数据·人工智能·经验分享·搜索引擎·产品运营
babe小鑫14 小时前
中专学历进入快消大厂终端销售岗位的可行性分析
大数据
samFuB14 小时前
【工具变量】区县5A级旅游景区DID数据集(2000-2025年)
大数据
百夜﹍悠ゼ14 小时前
数据治理DataHub安装部署
大数据·数据治理