|
|
package com.yoho.search.service.servicenew.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.ConvertUtils;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.service.cache.SearchCacheFactory;
|
|
|
import com.yoho.search.service.cache.model.SearchCache;
|
|
|
import com.yoho.search.service.service.SearchCacheService;
|
|
|
import com.yoho.search.service.service.SearchCommonService;
|
|
|
import com.yoho.search.service.service.base.ProductIndexBaseService;
|
|
|
import com.yoho.search.service.servicenew.ISknImageVectorsService;
|
|
|
import com.yoho.search.service.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.service.vo.SearchApiResult;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.common.lucene.search.function.CombineFunction;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.MatchAllQueryBuilder;
|
...
|
...
|
@@ -18,7 +26,6 @@ import org.elasticsearch.index.query.QueryBuilders; |
|
|
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
|
|
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
|
|
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
|
|
|
import org.elasticsearch.script.Script;
|
|
|
import org.elasticsearch.script.ScriptService;
|
|
|
import org.elasticsearch.search.sort.SortBuilder;
|
|
|
import org.elasticsearch.search.sort.SortBuilders;
|
...
|
...
|
@@ -30,22 +37,15 @@ import org.springframework.context.ApplicationEventPublisher; |
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.service.cache.SearchCacheFactory;
|
|
|
import com.yoho.search.service.cache.model.SearchCache;
|
|
|
import com.yoho.search.service.service.SearchCacheService;
|
|
|
import com.yoho.search.service.service.SearchCommonService;
|
|
|
import com.yoho.search.service.service.base.ProductIndexBaseService;
|
|
|
import com.yoho.search.service.servicenew.ISknImageVectorsService;
|
|
|
import com.yoho.search.service.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.service.vo.SearchApiResult;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.script.Script;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2017/7/28.
|
...
|
...
|
@@ -60,6 +60,8 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
|
|
|
private static final String SCRIPT_NAME = "yoho_hamming_score";
|
|
|
|
|
|
private static final int VECTORS_32_QUERY_BACK_NUM = 100;
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Autowired
|
...
|
...
|
@@ -91,13 +93,6 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
// 1.构造查询参数
|
|
|
String vectors_32 = paramMap.get(VECTORS_32_KEY);
|
|
|
String vectors_128 = paramMap.get(VECTORS_128_KEY);
|
|
|
//两个向量入参都不为空,默认用32位查询
|
|
|
String vectorsFieldName=VECTORS_32_KEY;
|
|
|
String vectorsValue=vectors_32;
|
|
|
if(StringUtils.isBlank(vectors_32)&&StringUtils.isNotBlank(vectors_128)){
|
|
|
vectorsFieldName=VECTORS_128_KEY;
|
|
|
vectorsValue=vectors_128;
|
|
|
}
|
|
|
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
...
|
...
|
@@ -108,24 +103,40 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
if (pageSize > 50) {
|
|
|
pageSize = 50;
|
|
|
}
|
|
|
searchParam.setPage(page);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
searchParam.setQuery(buildFunctionScoreQueryBuilder(vectorsFieldName,vectorsValue));
|
|
|
searchParam.setSize(VECTORS_32_QUERY_BACK_NUM);
|
|
|
searchParam.setQuery(buildFunctionScoreQueryBuilder(VECTORS_32_KEY, vectors_32, null));
|
|
|
|
|
|
//2.根据特征,查询SknImageVectors索引
|
|
|
//2.1第一次查询,用32维向量粗查询,召回一些skn
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_IMAGE_VECTORS;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
SearchApiResult searchApiResult = new SearchApiResult();
|
|
|
if (CollectionUtils.isNotEmpty(searchResult.getResultList())) {
|
|
|
List<Map<String, Object>> sknList = getSknImageMap(searchResult.getResultList());
|
|
|
List<String> querySknList = new ArrayList<>();
|
|
|
String sknFilterString = "";
|
|
|
for (Map<String, Object> map : sknList) {
|
|
|
querySknList.add(map.get("productSkn").toString());
|
|
|
sknFilterString += map.get("productSkn").toString() + ",";
|
|
|
}
|
|
|
//2.2第二次查询,用128维向量粗查询这些skn
|
|
|
searchParam.setPage(page);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
//todo 先用32维的,后面要改成128的
|
|
|
searchParam.setQuery(buildFunctionScoreQueryBuilder(VECTORS_32_KEY, vectors_32, sknFilterString));
|
|
|
searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (CollectionUtils.isNotEmpty(searchResult.getResultList())) {
|
|
|
sknList = getSknImageMap(searchResult.getResultList());
|
|
|
List<String> querySknList = new ArrayList<>();
|
|
|
for (Map<String, Object> map : sknList) {
|
|
|
querySknList.add(map.get("productSkn").toString());
|
|
|
}
|
|
|
//3.根据返回的skn列表,查询ProductIndex
|
|
|
searchApiResult = searchProductList(paramMap, querySknList, page, pageSize);
|
|
|
logger.info("[func=searchSknByPhoto][cost={}]", System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
//3.根据返回的skn列表,查询ProductIndex
|
|
|
searchApiResult = searchProductList(paramMap, querySknList, page, pageSize);
|
|
|
}
|
|
|
logger.info("[func=searchSknByPhoto][cost={}]", System.currentTimeMillis() - begin);
|
|
|
return searchApiResult;
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_SEARCHSKNBYPHOTO.getEventName(), EventReportEnum.SEARCHCONTROLLER_SEARCHSKNBYPHOTO.getFunctionName(),
|
...
|
...
|
@@ -173,12 +184,10 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
return new SearchApiResult().setData(photoListData);
|
|
|
}
|
|
|
|
|
|
private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectorsFieldName,String vectorsValue) {
|
|
|
private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectorsFieldName, String vectorsValue, String sknFilterString) {
|
|
|
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery().boost(1.0f);
|
|
|
//query
|
|
|
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(matchAllQueryBuilder);
|
|
|
//filter
|
|
|
QueryBuilder filter = QueryBuilders.matchAllQuery().boost(1.0f);
|
|
|
//script_score
|
|
|
String[] feaArray = vectorsValue.split(",");
|
|
|
Long[] fea = new Long[feaArray.length];
|
...
|
...
|
@@ -194,7 +203,16 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
Script script = new Script(inlineScript, ScriptService.ScriptType.INLINE, "native", params);
|
|
|
ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(script);
|
|
|
//function_score
|
|
|
functionScoreQueryBuilder.add(filter, scriptBuilder);
|
|
|
//filter,sknFilterString为空是第一次粗查询,不为空是第二次精查询
|
|
|
if (StringUtils.isBlank(sknFilterString)) {
|
|
|
QueryBuilder filter = QueryBuilders.matchAllQuery().boost(1.0f);
|
|
|
functionScoreQueryBuilder.add(filter, scriptBuilder);
|
|
|
} else {
|
|
|
int[] sknArray = ConvertUtils.stringToIntArray(sknFilterString, ",");
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
boolFilter.must(QueryBuilders.termsQuery("productSkn", sknArray));
|
|
|
functionScoreQueryBuilder.add(boolFilter, scriptBuilder);
|
|
|
}
|
|
|
functionScoreQueryBuilder.scoreMode("sum");
|
|
|
functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE);
|
|
|
functionScoreQueryBuilder.maxBoost(3.4028235E38F);
|
...
|
...
|
|