Authored by wangnan

【图片搜索】 fix

... ... @@ -24,7 +24,8 @@ public class SknImageVectorsController {
private static final Logger logger = LoggerFactory.getLogger(SknImageVectorsController.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";
@Autowired
private ISknImageVectorsService sknImageVectorsService;
... ... @@ -33,12 +34,14 @@ public class SknImageVectorsController {
@ResponseBody
public SearchApiResult searchSknByPhoto(@RequestBody JSONObject jsonObject) {
try {
String vectors = jsonObject.get(VECTORS_KEY).toString();
if (StringUtils.isBlank(vectors)) {
return new SearchApiResult().setCode(400).setMessage("vectors不能为空");
String vectors_32 = jsonObject.get(VECTORS_32_KEY)==null?null:jsonObject.get(VECTORS_32_KEY).toString();
String vectors_128 = jsonObject.get(VECTORS_128_KEY)==null?null:jsonObject.get(VECTORS_128_KEY).toString();
if (StringUtils.isBlank(vectors_32)&&StringUtils.isBlank(vectors_128)) {
return new SearchApiResult().setCode(400).setMessage("vectors_32与vectors_128不能都为空");
}
Map<String, String> paramMap = new HashMap<>();
paramMap.put(VECTORS_KEY, vectors);
paramMap.put(VECTORS_32_KEY, vectors_32);
paramMap.put(VECTORS_128_KEY, vectors_128);
paramMap.put("viewNum", jsonObject.get("viewNum")==null?null:jsonObject.get("viewNum").toString());
paramMap.put("page", jsonObject.get("page")==null?null:jsonObject.get("page").toString());
return sknImageVectorsService.searchSknByPhoto(paramMap);
... ...
... ... @@ -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
... ...