...
|
...
|
@@ -49,7 +49,10 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SknImageVectorsServiceImpl.class);
|
|
|
|
|
|
private static final String VECTORS_KEY = "vectors";
|
|
|
private static final String VECTORS_32_KEY = "vectors_32";
|
|
|
private static final String VECTORS_128_KEY = "vectors_128";
|
|
|
|
|
|
private static final String SCRIPT_NAME = "yoho_hamming_score";
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
...
|
...
|
@@ -80,7 +83,16 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
logger.info("[func=searchSknByPhoto][param={}][begin={}]", paramMap.toString(), begin);
|
|
|
|
|
|
// 1.构造查询参数
|
|
|
String vectors = paramMap.get(VECTORS_KEY);
|
|
|
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"));
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
...
|
...
|
@@ -93,7 +105,7 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
searchParam.setPage(page);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
searchParam.setQuery(buildFunctionScoreQueryBuilder(vectors));
|
|
|
searchParam.setQuery(buildFunctionScoreQueryBuilder(vectorsFieldName,vectorsValue));
|
|
|
|
|
|
//2.根据特征,查询SknImageVectors索引
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_IMAGE_VECTORS;
|
...
|
...
|
@@ -157,24 +169,24 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl |
|
|
return new SearchApiResult().setData(photoListData);
|
|
|
}
|
|
|
|
|
|
private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectors) {
|
|
|
private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectorsFieldName,String vectorsValue) {
|
|
|
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 = vectors.split(",");
|
|
|
String[] feaArray = vectorsValue.split(",");
|
|
|
Long[] fea = new Long[feaArray.length];
|
|
|
for (int i = 0; i < feaArray.length; i++) {
|
|
|
String string = feaArray[i];
|
|
|
fea[i] = Long.valueOf(string);
|
|
|
}
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
|
params.put("f", VECTORS_KEY);
|
|
|
params.put("f", vectorsFieldName);
|
|
|
params.put("fea", fea);
|
|
|
params.put("verbose", true);
|
|
|
String inlineScript = "yoho_hamming_score";
|
|
|
String inlineScript = SCRIPT_NAME;
|
|
|
Script script = new Script(inlineScript, ScriptService.ScriptType.INLINE, "native", params);
|
|
|
ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(script);
|
|
|
//function_score
|
...
|
...
|
|