标签:rand else let parser uil prepare ati cts Lucene
通常的搜索引擎,都会根据用户的输入,实时给予匹配的提示。 那么这个功能在elasticsearch中如何实现呢?
Elasticsearch里设计了4种类别的Suggester,分别是:
我是采用Completion Suggester来实现的。
原理的文章网上有很多,这里不多说。我们直接上代码:
1 package com.cs99lzzs.elasticsearch.service.imp; 2 3 import java.text.DecimalFormat; 4 import java.util.ArrayList; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 9 import javax.annotation.Resource; 10 11 import org.apache.commons.lang.StringUtils; 12 import org.apache.log4j.Logger; 13 import org.apache.lucene.queryparser.classic.QueryParser; 14 import org.elasticsearch.action.search.SearchRequestBuilder; 15 import org.elasticsearch.action.search.SearchResponse; 16 import org.elasticsearch.action.search.SearchType; 17 import org.elasticsearch.client.Client; 18 import org.elasticsearch.index.query.BoolQueryBuilder; 19 import org.elasticsearch.index.query.QueryBuilder; 20 import org.elasticsearch.index.query.QueryBuilders; 21 import org.elasticsearch.search.SearchHit; 22 import org.elasticsearch.search.aggregations.Aggregation; 23 import org.elasticsearch.search.aggregations.AggregationBuilders; 24 import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; 25 import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; 26 import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; 27 import org.elasticsearch.search.sort.SortBuilder; 28 import org.elasticsearch.search.sort.SortBuilders; 29 import org.elasticsearch.search.sort.SortOrder; 30 import org.elasticsearch.search.suggest.Suggest; 31 import org.elasticsearch.search.suggest.SuggestBuilder; 32 import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; 33 import org.springframework.beans.factory.annotation.Value; 34 import org.springframework.stereotype.Service; 35 36 37 @Service("productSearchService") 38 public class ProductSearchServiceImp implements ProductSearchService { 39 40 private static Logger logger = Logger.getLogger(ProductSearchService.class); 41 42 private static String aggrationBrandName = "brand_count"; 43 44 private static String suggestZhName = "suggestName"; 45 46 @Resource(name="esClient") 47 Client esClient; 48 49 @Value("${elasticsearch.index}") 50 private String CLUSTER_INDEX; 51 52 @Value("${elasticsearch.type}") 53 private String CLUSTER_TYPE; 54 55 56 /** 57 * @return 58 */ 59 @Override 60 public List<String> getSuggestList(String keyword, int number) { 61 List<String> suggestList = new ArrayList<String>(); 62 63 CompletionSuggestionBuilder suggestZhNameBuilder = new CompletionSuggestionBuilder(suggestZhName).prefix(keyword).size(100); 64 SuggestBuilder suggestBuilder = new SuggestBuilder() 65 .addSuggestion(suggestZhName, suggestZhNameBuilder); 66 SearchResponse suggestResponse = esClient.prepareSearch(CLUSTER_INDEX) 67 .setTypes(CLUSTER_TYPE) 68 // .setQuery(QueryBuilders.matchAllQuery()) 69 .suggest(suggestBuilder) 70 .execute() 71 .actionGet(); 72 73 List<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> result1 = suggestResponse 74 .getSuggest().getSuggestion(suggestZhName).getEntries(); 75 for (Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> op : result1) { 76 List<? extends Suggest.Suggestion.Entry.Option> options = op.getOptions(); 77 if (suggestList.size() < number) { 78 for (Suggest.Suggestion.Entry.Option pp : options) { 79 if(suggestList.size() < number && !suggestList.contains(pp.getText().toString())){ 80 suggestList.add(pp.getText().toString()); 81 } 82 } 83 }else{ 84 break; 85 } 86 } 87 88 return suggestList; 89 } 90 91 }
Elasticsearch 5.4.3实战--Java API调用:搜索建议
标签:rand else let parser uil prepare ati cts Lucene
原文地址:http://www.cnblogs.com/cs99lzzs/p/7212600.html