ElasticSearch6.x 基于SpringBoot 实现ElasticSearch全文查询

符号 阅读:738 2021-03-31 17:02:54 评论:0

elasticsearch6.x 官网文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-full-text-queries.html

SpringBoot 功能封装涉及ElasticSearch的全文检索方法约定如下:

public SearchResponse searchMatchQuery(String[] indexs, String[] types,String name, Object text)

public SearchResponse searchMultiMatchQuery(String[] indexs, String[] types,String[] fieldNames, Object text)

public SearchResponse searchCommonTermsQuery(String[] indexs, String[] types,String fieldName, Object text)

public SearchResponse searchQueryStringQuery(String[] indexs, String[] types,String queryString)

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

@Component 
public class ElasticSearchIndexUtil { 
	// 引入 Ela 连接实列化对象 
	@Autowired 
	private TransportClient client; 
 
/** 
	 * 功能描述:全文检索之match query 
	 * @param indexs  索引数组 
	 * @param types   类型数组 
	 * @param name    文档属性名称 
	 * @param text    文档属性值 
	 * @return 
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */ 
	public SearchResponse searchMatchQuery(String[] indexs, String[] types,String name, Object text) throws InterruptedException, ExecutionException{ 
		MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(name, text); 
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(matchQuery).execute().get(); 
		return response; 
	} 
	 
	 
	/** 
	 * 功能描述:全文检索之multi_match query 
	 * @param indexs  索引数组 
	 * @param types   类型数组 
	 * @param fieldNames    文档属性数组 
	 * @param text    文档属性值 
	 * @return 
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */ 
	public SearchResponse searchMultiMatchQuery(String[] indexs, String[] types,String[] fieldNames, Object text) throws InterruptedException, ExecutionException{ 
		MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(text, fieldNames); 
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(multiMatchQuery).execute().get(); 
		return response; 
	} 
	 
	 
	/** 
	 * 功能描述:全文检索之common_terms query 
	 * @param indexs  索引数组 
	 * @param types   类型数组 
	 * @param fieldNames    文档属性数组 
	 * @param text    文档属性值 
	 * @return 
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */ 
	public SearchResponse searchCommonTermsQuery(String[] indexs, String[] types,String fieldName, Object text) throws InterruptedException, ExecutionException{ 
		CommonTermsQueryBuilder commonTermsQuery = QueryBuilders.commonTermsQuery(fieldName, text); 
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(commonTermsQuery).execute().get(); 
		return response; 
	} 
	 
	 
	/** 
	 * 功能描述:全文检索之query_string query 
	 * @param indexs  索引数组 
	 * @param types   类型数组 
	 * @param fieldNames    文档属性数组 
	 * @param text    文档属性值 
	 * @return 
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */ 
	public SearchResponse searchQueryStringQuery(String[] indexs, String[] types,String queryString) throws InterruptedException, ExecutionException{ 
		QueryStringQueryBuilder queryStringQuery = QueryBuilders.queryStringQuery(queryString); 
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(queryStringQuery).execute().get(); 
		return response; 
	} 
	 
	 
	/** 
	 * 功能描述:全文检索之simple_query_string 
	 * @param indexs  索引数组 
	 * @param types   类型数组 
	 * @param fieldNames    文档属性数组 
	 * @param text    文档属性值 
	 * @return 
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */ 
	public SearchResponse searchSimpleQueryStringQuery(String[] indexs, String[] types,String queryString) throws InterruptedException, ExecutionException{ 
		SimpleQueryStringBuilder simpleQueryString = QueryBuilders.simpleQueryStringQuery(queryString); 
		SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(simpleQueryString).execute().get(); 
		return response; 
	} 
 
}

功能的测试方法就不进行补充了

标签:Spring Boot
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号