1、查询index1中content字段包含工厂的文档
@Testpublic void testMatch() throwsIOException, InterruptedException, ExecutionException {//指定集群
Settings settings = Settings.builder().put("cluster.name","my-application").build();//创建客户端
TransportClient client = newPreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));//创建查询对象
QueryBuilder qb = QueryBuilders.matchQuery("content","工厂");//执行查询
SearchResponse sr = client.prepareSearch("index1")
.setQuery(qb)
.setSize(3)//查询出3个
.get();//获取结果
SearchHits hits =sr.getHits();for(SearchHit hit:hits) {//输出json
System.out.println(hit.getSourceAsString());//输出json的key与value
Map map =hit.getSourceAsMap();for(String key:map.keySet()) {
System.out.println(key+"="+map.get(key));
}
}
client.close();
}
2、查询index1中title字段或者content字段包含设计的文档
@Testpublic void testMultiMatch() throwsIOException, InterruptedException, ExecutionException {//指定集群
Settings settings = Settings.builder().put("cluster.name","my-application").build();//创建客户端
TransportClient client = newPreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));//创建查询对象,查询title或者content包含设计的
QueryBuilder qb = QueryBuilders.multiMatchQuery("设计","title","content");//执行查询
SearchResponse sr = client.prepareSearch("index1")
.setQuery(qb)
.setSize(3)//查询出3个
.get();//获取结果
SearchHits hits =sr.getHits();for(SearchHit hit:hits) {//输出json
System.out.println(hit.getSourceAsString());//输出json的key与value
Map map =hit.getSourceAsMap();for(String key:map.keySet()) {
System.out.println(key+"="+map.get(key));
}
}
client.close();
}
3、查询index1中title字段包含设计的
@Testpublic void testTermQuery() throwsIOException, InterruptedException, ExecutionException {//指定集群
Settings settings = Settings.builder().put("cluster.name","my-application").build();//创建客户端
TransportClient client = newPreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151