通常的搜索引擎,都会根据用户的输入,实时给予匹配的提示。 那么这个功能在elasticsearch中如何实现呢?
Elasticsearch里设计了4种类别的Suggester,分别是:
- Term Suggester
- Phrase Suggester
- Completion Suggester
- Context Suggester
我是采用Completion Suggester来实现的。
原理的文章网上有很多,这里不多说。我们直接上代码:
package com.cs99lzzs.elasticsearch.service.imp; import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; @Service("productSearchService")
public class ProductSearchServiceImp implements ProductSearchService { private static Logger logger = Logger.getLogger(ProductSearchService.class); private static String aggrationBrandName = "brand_count"; private static String suggestZhName = "suggestName"; @Resource(name="esClient")
Client esClient; @Value("${elasticsearch.index}")
private String CLUSTER_INDEX; @Value("${elasticsearch.type}")
private String CLUSTER_TYPE; /**
* @return
*/
@Override
public List<String> getSuggestList(String keyword, int number) {
List<String> suggestList = new ArrayList<String>(); CompletionSuggestionBuilder suggestZhNameBuilder = new CompletionSuggestionBuilder(suggestZhName).prefix(keyword).size(100);
SuggestBuilder suggestBuilder = new SuggestBuilder()
.addSuggestion(suggestZhName, suggestZhNameBuilder);
SearchResponse suggestResponse = esClient.prepareSearch(CLUSTER_INDEX)
.setTypes(CLUSTER_TYPE)
// .setQuery(QueryBuilders.matchAllQuery())
.suggest(suggestBuilder)
.execute()
.actionGet(); List<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> result1 = suggestResponse
.getSuggest().getSuggestion(suggestZhName).getEntries();
for (Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> op : result1) {
List<? extends Suggest.Suggestion.Entry.Option> options = op.getOptions();
if (suggestList.size() < number) {
for (Suggest.Suggestion.Entry.Option pp : options) {
if(suggestList.size() < number && !suggestList.contains(pp.getText().toString())){
suggestList.add(pp.getText().toString());
}
}
}else{
break;
}
} return suggestList;
} }