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;
	}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章