• 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<String,String> 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<String,String> 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<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
    		for (int i = 0; i < hits.getHits().length; i++) {
    			Map<String, Object> map = hits.getAt(i).getSource();
    			list.add(map);
    		}
    		client.close();
    		return list;
    	}
    }
  • 相关阅读:
    java中的成员变量和局部变量
    多线程实现输出当前时间,和猜数字游戏
    JDBC
    jQuery和原生JS的对比
    JavaScript有趣的知识点
    MySQL的数据类型
    行级元素和块级元素
    重定向和请求转发的区别
    JSP九大内置对象
    Python练习
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317477.html
Copyright © 2020-2023  润新知