Authored by wangnan

【图片搜索】 fix

@@ -24,7 +24,8 @@ public class SknImageVectorsController { @@ -24,7 +24,8 @@ public class SknImageVectorsController {
24 24
25 private static final Logger logger = LoggerFactory.getLogger(SknImageVectorsController.class); 25 private static final Logger logger = LoggerFactory.getLogger(SknImageVectorsController.class);
26 26
27 - private static final String VECTORS_KEY = "vectors"; 27 + private static final String VECTORS_32_KEY = "vectors_32";
  28 + private static final String VECTORS_128_KEY = "vectors_128";
28 29
29 @Autowired 30 @Autowired
30 private ISknImageVectorsService sknImageVectorsService; 31 private ISknImageVectorsService sknImageVectorsService;
@@ -33,12 +34,14 @@ public class SknImageVectorsController { @@ -33,12 +34,14 @@ public class SknImageVectorsController {
33 @ResponseBody 34 @ResponseBody
34 public SearchApiResult searchSknByPhoto(@RequestBody JSONObject jsonObject) { 35 public SearchApiResult searchSknByPhoto(@RequestBody JSONObject jsonObject) {
35 try { 36 try {
36 - String vectors = jsonObject.get(VECTORS_KEY).toString();  
37 - if (StringUtils.isBlank(vectors)) {  
38 - return new SearchApiResult().setCode(400).setMessage("vectors不能为空"); 37 + String vectors_32 = jsonObject.get(VECTORS_32_KEY)==null?null:jsonObject.get(VECTORS_32_KEY).toString();
  38 + String vectors_128 = jsonObject.get(VECTORS_128_KEY)==null?null:jsonObject.get(VECTORS_128_KEY).toString();
  39 + if (StringUtils.isBlank(vectors_32)&&StringUtils.isBlank(vectors_128)) {
  40 + return new SearchApiResult().setCode(400).setMessage("vectors_32与vectors_128不能都为空");
39 } 41 }
40 Map<String, String> paramMap = new HashMap<>(); 42 Map<String, String> paramMap = new HashMap<>();
41 - paramMap.put(VECTORS_KEY, vectors); 43 + paramMap.put(VECTORS_32_KEY, vectors_32);
  44 + paramMap.put(VECTORS_128_KEY, vectors_128);
42 paramMap.put("viewNum", jsonObject.get("viewNum")==null?null:jsonObject.get("viewNum").toString()); 45 paramMap.put("viewNum", jsonObject.get("viewNum")==null?null:jsonObject.get("viewNum").toString());
43 paramMap.put("page", jsonObject.get("page")==null?null:jsonObject.get("page").toString()); 46 paramMap.put("page", jsonObject.get("page")==null?null:jsonObject.get("page").toString());
44 return sknImageVectorsService.searchSknByPhoto(paramMap); 47 return sknImageVectorsService.searchSknByPhoto(paramMap);
@@ -49,7 +49,10 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl @@ -49,7 +49,10 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
49 49
50 private static final Logger logger = LoggerFactory.getLogger(SknImageVectorsServiceImpl.class); 50 private static final Logger logger = LoggerFactory.getLogger(SknImageVectorsServiceImpl.class);
51 51
52 - private static final String VECTORS_KEY = "vectors"; 52 + private static final String VECTORS_32_KEY = "vectors_32";
  53 + private static final String VECTORS_128_KEY = "vectors_128";
  54 +
  55 + private static final String SCRIPT_NAME = "yoho_hamming_score";
53 56
54 private ApplicationEventPublisher publisher; 57 private ApplicationEventPublisher publisher;
55 58
@@ -80,7 +83,16 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl @@ -80,7 +83,16 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
80 logger.info("[func=searchSknByPhoto][param={}][begin={}]", paramMap.toString(), begin); 83 logger.info("[func=searchSknByPhoto][param={}][begin={}]", paramMap.toString(), begin);
81 84
82 // 1.构造查询参数 85 // 1.构造查询参数
83 - String vectors = paramMap.get(VECTORS_KEY); 86 + String vectors_32 = paramMap.get(VECTORS_32_KEY);
  87 + String vectors_128 = paramMap.get(VECTORS_128_KEY);
  88 + //两个向量入参都不为空,默认用32位查询
  89 + String vectorsFieldName=VECTORS_32_KEY;
  90 + String vectorsValue=vectors_32;
  91 + if(StringUtils.isBlank(vectors_32)&&StringUtils.isNotBlank(vectors_128)){
  92 + vectorsFieldName=VECTORS_128_KEY;
  93 + vectorsValue=vectors_128;
  94 + }
  95 +
84 SearchParam searchParam = new SearchParam(); 96 SearchParam searchParam = new SearchParam();
85 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 97 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
86 int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); 98 int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
@@ -93,7 +105,7 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl @@ -93,7 +105,7 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
93 searchParam.setPage(page); 105 searchParam.setPage(page);
94 searchParam.setOffset((page - 1) * pageSize); 106 searchParam.setOffset((page - 1) * pageSize);
95 searchParam.setSize(pageSize); 107 searchParam.setSize(pageSize);
96 - searchParam.setQuery(buildFunctionScoreQueryBuilder(vectors)); 108 + searchParam.setQuery(buildFunctionScoreQueryBuilder(vectorsFieldName,vectorsValue));
97 109
98 //2.根据特征,查询SknImageVectors索引 110 //2.根据特征,查询SknImageVectors索引
99 final String indexName = ISearchConstants.INDEX_NAME_IMAGE_VECTORS; 111 final String indexName = ISearchConstants.INDEX_NAME_IMAGE_VECTORS;
@@ -157,24 +169,24 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl @@ -157,24 +169,24 @@ public class SknImageVectorsServiceImpl implements ISknImageVectorsService, Appl
157 return new SearchApiResult().setData(photoListData); 169 return new SearchApiResult().setData(photoListData);
158 } 170 }
159 171
160 - private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectors) { 172 + private FunctionScoreQueryBuilder buildFunctionScoreQueryBuilder(String vectorsFieldName,String vectorsValue) {
161 MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery().boost(1.0f); 173 MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery().boost(1.0f);
162 //query 174 //query
163 FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(matchAllQueryBuilder); 175 FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(matchAllQueryBuilder);
164 //filter 176 //filter
165 QueryBuilder filter = QueryBuilders.matchAllQuery().boost(1.0f); 177 QueryBuilder filter = QueryBuilders.matchAllQuery().boost(1.0f);
166 //script_score 178 //script_score
167 - String[] feaArray = vectors.split(","); 179 + String[] feaArray = vectorsValue.split(",");
168 Long[] fea = new Long[feaArray.length]; 180 Long[] fea = new Long[feaArray.length];
169 for (int i = 0; i < feaArray.length; i++) { 181 for (int i = 0; i < feaArray.length; i++) {
170 String string = feaArray[i]; 182 String string = feaArray[i];
171 fea[i] = Long.valueOf(string); 183 fea[i] = Long.valueOf(string);
172 } 184 }
173 Map<String, Object> params = new HashMap<>(); 185 Map<String, Object> params = new HashMap<>();
174 - params.put("f", VECTORS_KEY); 186 + params.put("f", vectorsFieldName);
175 params.put("fea", fea); 187 params.put("fea", fea);
176 params.put("verbose", true); 188 params.put("verbose", true);
177 - String inlineScript = "yoho_hamming_score"; 189 + String inlineScript = SCRIPT_NAME;
178 Script script = new Script(inlineScript, ScriptService.ScriptType.INLINE, "native", params); 190 Script script = new Script(inlineScript, ScriptService.ScriptType.INLINE, "native", params);
179 ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(script); 191 ScriptScoreFunctionBuilder scriptBuilder = ScoreFunctionBuilders.scriptFunction(script);
180 //function_score 192 //function_score