|
|
package com.yoho.search.service.index;
|
|
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
import com.google.common.cache.LoadingCache;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.core.es.model.SearchField;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.es.utils.SearchFieldUtils;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Component
|
|
|
public class SearchFieldBoostConfigService {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
private static final String CACHE_KEY = "SearchFieldBoostConfigCacheKey";
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
//Guava Cache
|
|
|
private LoadingCache<String, Map<String, SearchField>> searchFieldBoostConfigCache = CacheBuilder.newBuilder()
|
|
|
.maximumSize(100).expireAfterWrite(1, TimeUnit.MINUTES).build(new CacheLoader<String, Map<String, SearchField>>() {
|
|
|
public Map<String, SearchField> load(String key) {
|
|
|
return querySearchFieldBoostConfig();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
private Map<String, SearchField> querySearchFieldBoostConfig() {
|
|
|
try {
|
|
|
//1、构造返回参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(1000);
|
|
|
|
|
|
//2、执行搜索
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CS_SEARCH_FIELD_BOOST_CONFIG, searchParam);
|
|
|
|
|
|
//3、构造返回结果
|
|
|
List<Map<String, Object>> results = searchResult.getResultList();
|
|
|
if (CollectionUtils.isEmpty(results)) {
|
|
|
return new HashMap<>();
|
|
|
}
|
|
|
Map<String, SearchField> result = new HashMap<>();
|
|
|
for (Map<String, Object> esMap : results) {
|
|
|
SearchField searchField = this.getSearchField(esMap);
|
|
|
result.put(searchField.getEsField(), searchField);
|
|
|
}
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
return new HashMap<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchField getSearchField(Map<String, Object> esMap) {
|
|
|
SearchField searchField = new SearchField();
|
|
|
searchField.setEsField(MapUtils.getString(esMap, "field", ""));
|
|
|
searchField.setEsFieldDesc(MapUtils.getString(esMap, "fieldDes", ""));
|
|
|
searchField.setBoost(MapUtils.getInteger(esMap, "boost", 0));
|
|
|
return searchField;
|
|
|
}
|
|
|
|
|
|
private Map<String, SearchField> getSearchFieldBoostConfig(boolean useCache) {
|
|
|
try {
|
|
|
if(useCache){
|
|
|
return searchFieldBoostConfigCache.get(CACHE_KEY);
|
|
|
}else{
|
|
|
return querySearchFieldBoostConfig();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new HashMap<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List<SearchField> queryFuzzySearchFields(boolean useCache) {
|
|
|
List<SearchField> searchFields = new ArrayList<>(SearchFieldUtils.getFuzzySearchFields());
|
|
|
Map<String, SearchField> searchFieldMap = getSearchFieldBoostConfig(useCache);
|
|
|
Iterator<SearchField> iterator = searchFields.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
SearchField searchField = iterator.next();
|
|
|
SearchField searchFieldConfig = searchFieldMap.get(searchField.getEsField());
|
|
|
if (searchFieldConfig == null || searchFieldConfig.getBoost() == null) {
|
|
|
continue;
|
|
|
}
|
|
|
if (searchFieldConfig.getBoost() <= 0) {
|
|
|
iterator.remove();
|
|
|
continue;
|
|
|
}
|
|
|
searchField.setBoost(searchFieldConfig.getBoost());
|
|
|
searchField.setEsFieldDesc(searchFieldConfig.getEsFieldDesc());
|
|
|
}
|
|
|
return searchFields;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|