ES6.6.2 使用小结 - 全量查询数据
1.通过这篇文章来讲讲如何进行全量查询。
2.假设当前ES存在如下数据(两条)。
3.全量查询源码如下:
package com.bas.demo;
import com.bas.util.ESUtil;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.util.Map;
public class MatchQueryDemo {
public static void main(String[] args) {
TransportClient client = ESUtil.getClient();
//构造查询对象
QueryBuilder query = QueryBuilders.matchAllQuery();
//搜索结果存入SearchResponse
SearchResponse response = client.prepareSearch("app_account")
.setQuery(query) //设置查询器
.setSize(3) //一次查询文档数
.get();
SearchHits hits = response.getHits();
int count = 1;
for (SearchHit hit : hits) {
System.out.println("--------------------" + count++);
System.out.println("source:" + hit.getSourceAsString());
System.out.println("index:" + hit.getIndex());
System.out.println("type:" + hit.getType());
System.out.println("id:" + hit.getId());
//遍历文档的每个字段
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
System.out.println("--------------------\n");
}
}
}
其中,ES工具类位于:ES6.6.2 使用小结 - ES工具类使用
4.执行MatchQueryDemo.main(),如图: