看,灰机...

es查询多个index时,如果一个index不存在,或者无数据怎么办?

匿名 | 发布于2016年01月11日 | 阅读数:7716

es查询多个index时,如果一个index不存在,或者无数据怎么办?
 
想要查询出结果,也就是存在的index里面有数据  ,另一个无数据。  结果返回 没有数据。   
 
这是什么原因呢?
    SearchRequestBuilder srb = null;
            System.out.println(indexNameDates);
            srb = client.prepareSearch(indexNameDates.toArray(new String[] {}));
            srb.setTypes(type);
            srb.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); 
            TermsBuilder collecttimeTermsBuilder = AggregationBuilders.terms("collecttime").field("collecttime")
                    .subAggregation(AggregationBuilders.sum("failnum").field("failnum"))
                    .subAggregation(AggregationBuilders.sum("total").field("total"));  
            srb.addAggregation(collecttimeTermsBuilder); 
            collecttimeTermsBuilder.size(Integer.MAX_VALUE);  
已邀请:

esuser20000

赞同来自:

有index的情况下 是可以的。   

zttech

赞同来自:

做agg之前为什么不先判断一下呢?给你个简单的例子
IndicesExistsResponse indicesExistsResponse = client.admin().indices().prepareExists("cars").execute().actionGet();
        if(indicesExistsResponse.isExists()) {
            IndicesStatsResponse response = client.admin().indices().prepareStats("cars").execute().actionGet();
            long count = response.getIndex("cars").getTotal().docs.getCount();
            System.out.println(count);
        }

lsh

赞同来自:

ignore

要回复问题请先登录注册