ElasticSearch多条件查询java api例子程序。
public class EsMulConditionQuery {
private static String index = "imgdata_index";
private static String type = "imgdata_type";
public static void main(String[] args) {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch")
.put("client.transport.sniff", true)//开启ES嗅探功能,确保集群连上多个节点
.build();
//创建客户端
TransportClient transportClient = new TransportClient(settings);
//添加es的节点信息,可以添加1个或多个
TransportAddress transportAddress = new InetSocketTransportAddress("218.193.126.32", 9300);
transportClient.addTransportAddresses(transportAddress);
// 连接到的节点
ImmutableList<DiscoveryNode> connectedNodes = transportClient.connectedNodes();
for (DiscoveryNode discoveryNode : connectedNodes) {
System.out.println(discoveryNode.getHostAddress());
}
// 多条件查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//多条件设置
boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("PolicyCode", "5674504720"));
boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("ServiceId", "SE2"));
//查询建立
SearchResponse searchResponse = transportClient.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder)
.get();
//获取查询结果
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
System.out.println("总数目=" + totalHits);
SearchHit[] hits2 = hits.getHits();
for (SearchHit searchHit : hits2) {
System.out.println(searchHit.getSourceAsString());
}
}