ElasticSearch 获取文档总数 6.0.x

news/2024/7/7 5:54:15 标签: elasticsearch, java, 大数据

前言​

欢迎大家来到我的博客,请各位看客们点赞、收藏、关注三连!

欢迎大家关注我的知识库,Java之从零开始·语雀

你的关注就是我前进的动力!

CSDN专注于问题解决的博客记录,语雀专注于知识的收集与汇总,包括分享前沿技术。

主体

需求
因为要做elk日志,并且要在页面上展示效果,故要做一个分页的效果。
使用的依赖过低,但是又无法升上去。以至于count命令无效,执行就报错!
想过做表格懒加载,但是百度了一下,都跟那个总数有关系。
可关键是我就是查不出总数,懒加载就显得毫无意义。
又想过百度、谷歌都是分页显示最多73页,我也这么搞。
现实却是只有几页时,下面的分页数还是有73个选项,也就是说这玩意也需要总数。
没办法!只能谷歌。
首先找到了几个思路的链接:
https://blog.csdn.net/zhuchunyan_aijia/article/details/122901472
https://facingissuesonit.com/2017/05/10/elasticsearch-rest-java-client-to-get-index-details-list/
只要可以执行命令,那这一切就简单了。

            <!--elasticsearch-->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.4.3</version>
            </dependency>

java实现

java">    public EsPageInfo<List<GxptApiCallLog>> queryPage2(String params) {
        JSONObject jsonObject = JSONObject.parseObject(params);
        String pageIndex = jsonObject.getString("pageIndex");
        String pageSize = jsonObject.getString("pageSize");
        // 条件搜索
        SearchSourceBuilder builder = new SearchSourceBuilder();
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();

        //封装查询参数
        builder.query(queryBuilder);

        // 结果集合分页,从第一页开始,返回最多十条数据
        builder.from(Integer.parseInt(pageIndex) - 1).size(Integer.parseInt(pageSize));

        //排序
        builder.sort("_id", SortOrder.DESC);

        RestHighLevelClient client = ElasticSearchClient.getConnect();
        RestClient lowLevelClient = client.getLowLevelClient();
        IndexInfo[] indexArr = null;
        int count = 0;
        try {
            Response response =  lowLevelClient.performRequest("GET", "/_cat/indices/myIndex-*?format=json",
                    Collections.singletonMap("pretty", "true"));
            HttpEntity entity = response.getEntity();
            ObjectMapper jacksonObjectMapper = new ObjectMapper();
            indexArr = jacksonObjectMapper.readValue(entity.getContent(), IndexInfo[].class);
            count = Arrays.stream(indexArr).map(IndexInfo::getDocumentCount).mapToInt(Integer::parseInt).sum();
            //for(IndexInfo indexInfo:indexArr) {
            //    System.out.println(indexInfo);
            //    indexInfo.getDocumentCount()
            //}
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 执行请求
        SearchResponse response = searchDocument(client, GateWayConstants.ES_ANTU_LOG, builder);
        // 返回的具体行数
        SearchHit[] searchHits = response.getHits().getHits();
        List<GxptApiCallLog> apiCallLogsList = new ArrayList<>();
        for (SearchHit searchHit : searchHits) {
            String sourceAsString = searchHit.getSourceAsString();
            GxptApiCallLog gxptApiCallLog = JSON.parseObject(sourceAsString, GxptApiCallLog.class);
            apiCallLogsList.add(gxptApiCallLog);
        }
        //String json = JSON.toJSONString(apiCallLogsList);
        EsPageInfo<List<GxptApiCallLog>> esPageInfo = new EsPageInfo<>();
        esPageInfo.setCurrentPage(Integer.parseInt(pageIndex));
        esPageInfo.setTotalItems(count);
        esPageInfo.setItemsPrePage(Integer.parseInt(pageSize));
        esPageInfo.setTotalPages(count / Long.parseLong(pageSize) + (long)(count % Long.parseLong(pageSize) != 0L ? 1
                : 0));
        esPageInfo.setItems(apiCallLogsList);


        return esPageInfo;
    }
    
    /**
     * 索引高级查询
     * @param indexName
     * @param source
     * @return
     */
    public SearchResponse searchDocument(RestHighLevelClient client, String indexName, SearchSourceBuilder source){
        //搜索
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(indexName);
        searchRequest.source(source);
        try {
            // 执行请求
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            return searchResponse;
        } catch (Exception e) {
            //log.warn("向es发起查询文档数据请求失败,请求参数:" + searchRequest.toString(), e);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
java">import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class IndexInfo {
	@JsonProperty(value = "health")
	private String health;
	@JsonProperty(value = "index")
	private String indexName;
	@JsonProperty(value = "status")
	private String status;
	@JsonProperty(value = "pri")
	private int shards;
	@JsonProperty(value = "rep")
	private int replica;
	@JsonProperty(value = "pri.store.size")
	private String dataSize;
	@JsonProperty(value = "store.size")
	private String totalDataSize;
	@JsonProperty(value = "docs.count")
	private String documentCount;

	public String getIndexName() {
		return indexName;
	}

	public void setIndexName(String indexName) {
		this.indexName = indexName;
	}

	public int getShards() {
		return shards;
	}

	public void setShards(int shards) {
		this.shards = shards;
	}

	public int getReplica() {
		return replica;
	}

	public void setReplica(int replica) {
		this.replica = replica;
	}

	public String getDataSize() {
		return dataSize;
	}

	public void setDataSize(String dataSize) {
		this.dataSize = dataSize;
	}

	public String getTotalDataSize() {
		return totalDataSize;
	}

	public void setTotalDataSize(String totalDataSize) {
		this.totalDataSize = totalDataSize;
	}

	public String getDocumentCount() {
		return documentCount;
	}

	public void setDocumentCount(String documentCount) {
		this.documentCount = documentCount;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getHealth() {
		return health;
	}

	public void setHealth(String health) {
		this.health = health;
	}
}

最终效果:
在这里插入图片描述


http://www.niftyadmin.cn/n/1638449.html

相关文章

制作VS风格的Toolbox控件

在前些日子里,我写了有十篇关于WinForm 控件的制作教程,在这些教程里使用的例子都十分的简单,并且示例的控件基本没有什么用&#xff0c;是该写一个简单实用的控件的时候了。今天我就来制作一个VS风格的ToolBox工具箱。VS风格的ToolBox和QQ的界面很相似&#xff0c;都是有多个栏…

vue中iframe传参/绕过跨域/绕过src不刷新问题解决

前言​ 欢迎大家来到我的博客&#xff0c;请各位看客们点赞、收藏、关注三连&#xff01; 欢迎大家关注我的知识库&#xff0c;全栈进阶之路语雀 你的关注就是我前进的动力&#xff01; CSDN专注于问题解决的博客记录&#xff0c;语雀专注于知识的收集与汇总&#xff0c;包…

2022年mvnrepository跳过人机验证

前言​ 欢迎大家来到我的博客&#xff0c;请各位看客们点赞、收藏、关注三连&#xff01; 欢迎大家关注我的知识库&#xff0c;Java之从零开始语雀 你的关注就是我前进的动力&#xff01; CSDN专注于问题解决的博客记录&#xff0c;语雀专注于知识的收集与汇总&#xff0c;…

自己写bootloader笔记5---设置u-boot传给内核的参数分析

1、分析 &#xff08;1&#xff09;u-boot要启动内核&#xff0c;把内核从flash读到SDRAM &#xff0c;要调到SDRAM执行&#xff0c;u-boot执行时内核还没执行&#xff0c;内核执行时&#xff0c;u-boot已完结。u-boot传参数给内核&#xff08;双方约定一个位置&#xff09;&a…

px2rem强行保留px,不转换px操作

需求 因为引入这个px2rem&#xff0c;原本是想解决分辨率问题&#xff0c;但是却出现不想要rem转换为px。 怎么解决呢&#xff1f; 超级简单&#xff1a; 将px小写改为大写。

自己写bootloader笔记6---boot.c分析(u-boot向内核传递参数及跳转到内核)

#include "setup.h" extern void uart0_init(void); extern void nand_read(unsigned int addr, unsigned char *buf, unsigned int len); extern void puts(char *str); extern void puthex(unsigned int val); u-boot 传参数给内核分析参考http://blog.csdn.net…

SQL不同类型分组排序

前言​ 欢迎大家来到我的博客&#xff0c;请各位看客们点赞、收藏、关注三连&#xff01; 欢迎大家关注我的知识库&#xff0c;全栈进阶之路 你的关注就是我前进的动力&#xff01; CSDN专注于问题解决的博客记录&#xff0c;语雀专注于知识的收集与汇总&#xff0c;包括分…

ASP.NET中使用javascript(2)

将 JavaScript 添加到服务器控件 将 JavaScript 添加到位于 ASP.NET 页面中的某个特定服务器控件是非常简单的。我们以按钮服务器控件为例。如果您使用任一 Microsoft Visual Studio 2005 将 Button HTML 服务器控件&#xff08;HtmlInputButton 类&#xff09;拖放到某个页面中…