ES多条件查询(1)


方案:(1)

 Client client = new TransportClient().addTransportAddresses(new InetSocketTransportAddress("127.0.0.1", 9300));
						
		BoolQueryBuilder boleanQueryBuilder = QueryBuilders.boolQuery();
			
		boleanQueryBuilder.must(QueryBuilders.termsQuery("SRC_ADDRESS", "<span style="font-size:24px;">192.168.100.1</span>","<span style="font-size:24px;">192.168.100.2</span>"));
		boleanQueryBuilder.must(QueryBuilders.termsQuery("DEST_PORT", "8110","80"));
			
		SearchResponse searchResponse = client.prepareSearch("myindex").setTypes("logs")
				.setQuery(boleanQueryBuilder)
				.setFrom(10)
				.setSize(20)
				.execute().actionGet();
		
		
		System.out.println(searchResponse.toString());
		SearchHits hits = searchResponse.getHits();
		
		if (null == hits || hits.totalHits() == 0) {

			System.out.println("对不起,您没有查询到结果!!");

		} else {

			for (SearchHit hit : hits) {

				String json = hit.getSourceAsString();
				
				System.out.println(json);
			}
		}
方案2:
Client client = ESUtils.getEsClient();
		
		Iterator<String> iterator = newProtolPortSet.iterator();
		List<SearchRequestBuilder> list = new ArrayList<SearchRequestBuilder>();		
		SearchRequestBuilder srb;
		while (iterator.hasNext()) {
	
			String[]  str = iterator.next().split(":");
			
			if(!str[0].equals("*")&&!str[1].equals("*")){
				
				 srb = client.prepareSearch("myindex")
						.setTypes("logs")
						.setPostFilter(FilterBuilders.rangeFilter("begin_Time").from(timeMap.get("fromTime")).to(timeMap.get("toTime")))	
						.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("APP_PROTOCOL_NAME", str[0])))
						.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("DEST_PORT", str[1])));
			}else if(str[0].equals("*")&&!str[1].equals("*")){
				
				 srb = client.prepareSearch("myindex")
						.setTypes("logs")
						.setPostFilter(FilterBuilders.rangeFilter("begin_Time").from(timeMap.get("fromTime")).to(timeMap.get("toTime")))	
						.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("DEST_PORT", str[1])));
			}else if(!str[0].equals("*")&&str[1].equals("*")){
				
				 srb = client.prepareSearch("myindex")
						.setTypes("logs")
						.setPostFilter(FilterBuilders.rangeFilter("begin_Time").from(timeMap.get("fromTime")).to(timeMap.get("toTime")))	
						.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("DEST_PORT", str[1])));
			}else{
				
				 srb = client.prepareSearch("myindex")
						.setTypes("logs")
						.setPostFilter(FilterBuilders.rangeFilter("begin_Time").from(timeMap.get("fromTime")).to(timeMap.get("toTime")));	
			}
			
			list.add(srb);
		}
		
				
		MultiSearchRequestBuilder multiSearchRequestBuilder= client.prepareMultiSearch();
		
		for(int i = 0; i < list.size();i++){
			
			multiSearchRequestBuilder.add(list.get(i));
		}
		
		if(null!=list&&list.size()>0){//有查询条件
			
			MultiSearchResponse sr = multiSearchRequestBuilder
				     
					.execute().actionGet();
			
			
		}else{//没有查询条件
			
			
		}


你可能感兴趣的:(ES多条件查询(1))