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

相关推荐
L***一1 小时前
大数据技术专业中专生职业发展路径探析
大数据
woshikejiaih1 小时前
**播客听书与有声书区别解析2026指南,适配不同场景的音频
大数据·人工智能·python·音视频
无忧智库1 小时前
某市“十五五“智慧气象防灾减灾精准预报系统建设方案深度解读 | 从“看天吃饭“到“知天而作“的数字化转型之路(WORD)
大数据·人工智能
AllData公司负责人1 小时前
AllData数据中台-数据同步平台【Seatunnel-Web】整库同步MySQL同步Doris能力演示
大数据·数据库·mysql·开源
acrelwwj1 小时前
智慧照明新引擎,ASL600 4GWJ开启城市照明精细化管理新时代
大数据·经验分享·物联网
2501_943695332 小时前
高职大数据技术专业,怎么参与开源数据分析项目积累经验?
大数据·数据分析·开源
Dxy12393102163 小时前
别再让 ES 把你拖垮!5 个实战技巧让搜索性能提升 10 倍
大数据·elasticsearch·搜索引擎
2501_943695333 小时前
大专市场调查与统计分析专业,怎么辨别企业招聘的“画饼”岗位?
大数据
七夜zippoe3 小时前
CANN Runtime跨进程通信 共享设备上下文的IPC实现
大数据·cann
威胁猎人3 小时前
【黑产大数据】2025年全球电商业务欺诈风险研究报告
大数据