ElasticSearch6.x 基于SpringBoot 实现ElasticSearch匹配查询

官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-term-level-queries.html

SpringBoot 功能封装涉及ElasticSearch文档匹配查询,约定方法如下:

在上一篇文中说到:ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装,将约定的方法填充到ElasticSearchIndexUtil.java 工具类中。

/**
	 * 功能描述:匹配检索之term query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param fieldNames
	 *            文档属性数组
	 * @param text
	 *            文档属性值
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchTermQuery(String[] indexs, String[] types, String name, Object value)
			throws InterruptedException, ExecutionException {
		TermQueryBuilder termQuery = QueryBuilders.termQuery(name, value);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(termQuery).execute().get();
		return response;
	}

	/**
	 * 功能描述:匹配检索之terms query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param values
	 *            文档属性值数组
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchTermsQuery(String[] indexs, String[] types, String name, Object[] values)
			throws InterruptedException, ExecutionException {
		TermsQueryBuilder termsQuery = QueryBuilders.termsQuery(name, values);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(termsQuery).execute().get();
		return response;
	}

	/**
	 * 功能描述:匹配检索之range query(基于时间类型)
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param from
	 *            文档属性值大于指定值
	 * @param to
	 *            文档属性值小于指定值
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchRangeQueryDate(String[] indexs, String[] types, String name, Object from, Object to)
			throws InterruptedException, ExecutionException {
		RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(name).from(from).to(to).includeLower(true)
				.includeUpper(true);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(rangeQuery).execute().get();
		return response;
	}

	/**
	 * 功能描述:匹配检索之range query(基于整数类型)
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param from
	 *            文档属性值大于指定值
	 * @param to
	 *            文档属性值小于指定值
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchRangeQueryInteger(String[] indexs, String[] types, String name, Object from, Object to)
			throws InterruptedException, ExecutionException {
		RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(name).gte(from).lt(to);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(rangeQuery).execute().get();
		return response;
	}
	
	/**
	 * 功能描述:匹配检索之exists query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchExistsQuery(String[] indexs, String[] types, String name)
			throws InterruptedException, ExecutionException {
		ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery(name);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(existsQuery).execute().get();
		return response;
	}
	
	
	/**
	 * 功能描述:匹配检索之prefix query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param prefix
	 *            文档属性值前缀
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchPrefixQuery(String[] indexs, String[] types, String name, String prefix)
			throws InterruptedException, ExecutionException {
		PrefixQueryBuilder prefixQuery = QueryBuilders.prefixQuery(name, prefix);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(prefixQuery).execute().get();
		return response;
	}
	
	
	/**
	 * 功能描述:匹配检索之wildcard query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param query
	 *            文档通配符属性值
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchWildcardQuery(String[] indexs, String[] types, String name, String query)
			throws InterruptedException, ExecutionException {
		WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery(name, query);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(wildcardQuery).execute().get();
		return response;
	}
	
	
	/**
	 * 功能描述:匹配检索之regexp query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param regexp
	 *            文档属性正则表达式
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchRegexpQuery(String[] indexs, String[] types, String name, String regexp)
			throws InterruptedException, ExecutionException {
		RegexpQueryBuilder regexpQuery = QueryBuilders.regexpQuery(name, regexp);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(regexpQuery).execute().get();
		return response;
	}
	
	
	/**
	 * 功能描述:匹配检索之fuzzy query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param name
	 *            文档属性
	 * @param value
	 *            文档属性值
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchFuzzyQuery(String[] indexs, String[] types, String name, Object value)
			throws InterruptedException, ExecutionException {
		FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery(name, value);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(fuzzyQuery).execute().get();
		return response;
	}
	
	
	/**
	 * 功能描述:匹配检索之ids query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param ids
	 *            文档id数组
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchTypeQuery(String[] indexs, String[] types, String[] ids)
			throws InterruptedException, ExecutionException {
		IdsQueryBuilder idsQuery = QueryBuilders.idsQuery(ids);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(idsQuery).execute().get();
		return response;
	}
	
	
	/**
	 * 功能描述:匹配检索之type query
	 * 
	 * @param indexs
	 *            索引数组
	 * @param types
	 *            类型数组
	 * @param fieldNames
	 *            文档属性数组
	 * @param text
	 *            文档属性值
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public SearchResponse searchTypeQuery(String[] indexs, String[] types, String type)
			throws InterruptedException, ExecutionException {
		TypeQueryBuilder fuzzyQuery = QueryBuilders.typeQuery(type);
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(fuzzyQuery).execute().get();
		return response;
	}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值