/**
* 不带关键词,将全文分页搜出来
* @throws UnknownHostException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void essearchAlOne() throws UnknownHostException, InterruptedException, ExecutionException {
Settings settings=Settings.builder().put("cluster.name", "cluster-elasticsearch-prod").build();
TransportClient client=new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.107"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.14"), 9300));
SearchResponse response = client.prepareSearch("tgdsm")
.setSize(1000).execute().actionGet();
/*.addSort("id", SortOrder.ASC);*/
// 从第几条开始,查询多少条
/*SearchResponse response = setQuery.setFrom(0).setSize(2).get();*/
SearchHits searchHits = response.getHits();
System.out.println("一共的记录数: " + searchHits.getTotalHits());
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()){
SearchHit searchHit = iterator.next();
System.out.println(searchHit.getSourceAsString());
}
}
下面是将不把全部信息查出来 只包含指定的部分字段
/**
* 不带关键词,将全文分页搜出来
* @throws UnknownHostException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void essearchAlOne() throws UnknownHostException, InterruptedException, ExecutionException {
Settings settings=Settings.builder().put("cluster.name", "cluster-elasticsearch-prod").build();
TransportClient client=new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.107"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.14"), 9300));
SearchResponse response = client.prepareSearch("tgdsm").setFetchSource(new String[] {"FileName","FileSize","User_ID","LastChangerTime"}, null).setFrom(0).setSize(10)
.setSize(1000).execute().actionGet();
/*.addSort("id", SortOrder.ASC);*/
HashMap<String,Object> hashMap = new HashMap<String,Object>();
ArrayList<Object> arrayList = new ArrayList<>();
SearchHits searchHits = response.getHits();
System.out.println("一共的记录数: " + searchHits.getTotalHits());
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()){
SearchHit searchHit = iterator.next();
arrayList.add(searchHit.getSourceAsString());
}
System.out.println(arrayList);
}