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

相关推荐
jianghx10242 小时前
Docker部署ES,开启安全认证并且设置账号密码(已运行中)
安全·elasticsearch·docker·es账号密码设置
IT小哥哥呀2 小时前
电池制造行业数字化实施
大数据·制造·智能制造·数字化·mom·电池·信息化
Xi xi xi3 小时前
苏州唯理科技近期也正式发布了国内首款神经腕带产品
大数据·人工智能·经验分享·科技
yumgpkpm3 小时前
华为鲲鹏 Aarch64 环境下多 Oracle 、mysql数据库汇聚到Cloudera CDP7.3操作指南
大数据·数据库·mysql·华为·oracle·kafka·cloudera
UMI赋能企业4 小时前
制造业流程自动化提升生产力的全面分析
大数据·人工智能
TDengine (老段)4 小时前
TDengine 数学函数 FLOOR 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
派可数据BI可视化7 小时前
商业智能BI 浅谈数据孤岛和数据分析的发展
大数据·数据库·数据仓库·信息可视化·数据挖掘·数据分析
jiedaodezhuti7 小时前
Flink性能调优基石:资源配置与内存优化实践
大数据·flink
半梦半醒*7 小时前
搭建Jenkins
linux·运维·centos·tomcat·jenkins·运维开发
Lx3528 小时前
Flink窗口机制详解:如何处理无界数据流
大数据