elasticsearch 查询 - 简单条件查询
方式1 restful api
POST http://192.168.199.126:9200/film/dongzuo/_search/
{
"query": {
"match": {
"title": "星球"
}
}
}

方式2 Java接口实现
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();
}
}
/**
* 条件查询
* @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)// 参数2 不包含指定的列
.execute()
.actionGet(); // 分页排序所有
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
