ElasticSearch封装查询、多条件查询、模糊查询工具类

这个EsTool类是根据自身业务写成的,因为业务暂时想法不难。

需要的主要是:1、精确查询  (单/多条件)   

                            2、模糊查询  (单/多条件)

前提:Mapping全部设置"index":"not_analys",阻止分词器去进行分析字符串从而达到精确查询的效果。


public class EsTool {
	public String index ="gtbdc2";
	public TransportClient getClient(){
		try {
			//设置集群名称
		        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
		        //创建client
		        @SuppressWarnings({ "resource", "unchecked" })
				TransportClient client = new PreBuiltTransportClient(settings)
		                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("0.0.0.0"), 9300));
		        return client;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据文档名、字段名、字段值查询某一条记录的详细信息;query查询
	 * @param type  文档名,相当于oracle中的表名,例如:ql_xz;
	 * @param key 字段名,例如:bdcqzh
	 * @param value  字段值,如:“”
	 * @return  List
	 * @author Lixin
	 */
	public List getQueryDataBySingleField(String type,String key,String value){
		TransportClient client = getClient();
		QueryBuilder qb = QueryBuilders.termQuery(key, value);
		SearchResponse response = client.prepareSearch(index).setTypes(type)
		                .setQuery(qb)  
		                .setFrom(0).setSize(10000).setExplain(true)  
		                .execute()  
		                .actionGet();
		return responseToList(client,response);
	}
	
	
	/**
	 * 多条件  文档名、字段名、字段值,查询某一条记录的详细信息
	 * @param type 文档名,相当于oracle中的表名,例如:ql_xz
	 * @param map 字段名:字段值 的map
	 * @return List
	 * @author Lixin
	 */
	public List getBoolDataByMuchField(String type,Map map){
		TransportClient client = getClient();
		 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		 for (String in : map.keySet()) {
			             //map.keySet()返回的是所有key的值
			              String str = map.get(in);//得到每个key多对用value的值
			              boolQueryBuilder.must(QueryBuilders.termQuery(in,str));
			         }
		 SearchResponse response = client.prepareSearch(index).setTypes(type)
			        .setQuery(boolQueryBuilder)     
			        .setFrom(0).setSize(10000).setExplain(true)     
			        .execute()     
			        .actionGet();
		 return responseToList(client,response);
	}
	
	
	/**
	 * 单条件 模糊查询
	 * @param type 文档名,相当于oracle中的表名,例如:ql_xz
	 * @param key 字段名,例如:bdcqzh
	 * @param value 字段名模糊值:如 *123* ;?123*;?123?;*123?;
	 * @return List
	 * @author Lixin
	 */
	public List getDataByillegible(String type,String key,String value){
		TransportClient client = getClient();
		WildcardQueryBuilder wBuilder = QueryBuilders.wildcardQuery(key, value);
		SearchResponse response = client.prepareSearch(index).setTypes(type)
                .setQuery(wBuilder)  
                .setFrom(0).setSize(10000).setExplain(true)  
                .execute()  
                .actionGet();
		return responseToList(client,response);
	}
	
	/**
	 * 多条件 模糊查询
	 * @param type  type 文档名,相当于oracle中的表名,例如:ql_xz
	 * @param map   包含key:value 模糊值键值对
	 * @return List
	 * @author Lixin
	 */
	public List getDataByMuchillegible(String type,Map map){
		TransportClient client = getClient();
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		 for (String in : map.keySet()) {
			             //map.keySet()返回的是所有key的值
			              String str = map.get(in);//得到每个key多对用value的值
			              boolQueryBuilder.must(QueryBuilders.wildcardQuery(in,str));
			         }
		 SearchResponse response = client.prepareSearch(index).setTypes(type)
	                .setQuery(boolQueryBuilder)  
	                .setFrom(0).setSize(10000).setExplain(true)  
	                .execute()  
	                .actionGet();
		 
		 return responseToList(client,response);
	}
	
	
	/**
	 * 将查询后获得的response转成list
	 * @param client
	 * @param response
	 * @return
	 */
	public List responseToList(TransportClient client,SearchResponse response){
		SearchHits hits = response.getHits();
		List> list = new ArrayList>();
		for (int i = 0; i < hits.getHits().length; i++) {
			Map map = hits.getAt(i).getSource();
			list.add(map);
		}
		client.close();
		return list;
	}
}


你可能感兴趣的:(elasticsearch)