elasticsearch简单条件查询restful api以及java代码实现
restful api实现:
post http://192.168.1.111:9200/film/dongzuo/_search/
{
"query":{
"match":{"title":"战"}
}
}
Java代码实现:
/** * 条件查询 * @throws Exception */ @Test public void searchByCondition()throws Exception{ SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo"); SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title", "战")) .setFetchSource(new String[]{"title","price"}, null) .execute() .actionGet(); // 分页排序所有 SearchHits hits=sr.getHits(); for(SearchHit hit:hits){ System.out.println(hit.getSourceAsString()); } }
运行结果:
{"price":"38","title":"战狼2"}
{"price":"55","title":"星球大战8:最后的绝地武士"}