注册
登录
使用POST方式 from指定起始位置 size指定要查询的条数
POST http://192.168.199.126:9200/film/dongzuo/_search/
{
"from": 0,
"size": 1
}
注意:必须使用POST方式请求 否则参数不起作用

创建TransportClient对象 参考 http://etjava.cn/blog/article/62
部分代码
private TransportClient client;
private static String host="192.168.199.126";
private static int port=9300; // 程序连接的端口
// 配置settings 集群相关
private static Settings.Builder settings = Settings.builder().put("cluster.name","my-application");
@Before
public void init() throws UnknownHostException {
client = new PreBuiltTransportClient(settings.build())
.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
}
@After
public void close(){
if(client!=null){
client.close();
}
}
/*
* 查询索引 带分页
* */
@Test
public void searchPaging()throws Exception{
// prepareSearch 可以同时查询多个索引名称,使用逗号隔开
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery()) // 查询所有
.setFrom(1).setSize(2) // 设置分页条件 默认0
.execute().actionGet();// 执行查询
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
