|
@@ -44,12 +44,12 @@ public class OpenSearchHandler implements PojoRequestHandler<TaskImageMsg, List<
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 维度一:namespace 维度
|
|
// 维度一:namespace 维度
|
|
|
- String nsFilter = String.format("sourceSendPackageDeptId = %d and taskId != %d", taskImageIndex.getSourceSendPackageDeptId(), taskImageIndex.getTaskId());
|
|
|
|
|
|
|
+ String nsFilter = String.format("sourceSendPackageDeptId = %d AND taskId != %d", taskImageIndex.getSourceSendPackageDeptId(), taskImageIndex.getTaskId());
|
|
|
List<TaskImageIndex.MultiResult> nsResult = knnSearch(namespace, taskImageIndex.getVector(), nsFilter, "namespace");
|
|
List<TaskImageIndex.MultiResult> nsResult = knnSearch(namespace, taskImageIndex.getVector(), nsFilter, "namespace");
|
|
|
List<TaskImageIndex.MultiResult> multiResultList = new ArrayList<>(nsResult);
|
|
List<TaskImageIndex.MultiResult> multiResultList = new ArrayList<>(nsResult);
|
|
|
|
|
|
|
|
// 维度二:namespace + taskId 维度
|
|
// 维度二:namespace + taskId 维度
|
|
|
- String nsTaskFilter = String.format("sourceSendPackageDeptId = %d and taskId = %d and id != '%s'", taskImageIndex.getSourceSendPackageDeptId(), taskImageIndex.getTaskId(), taskImageIndex.getId());
|
|
|
|
|
|
|
+ String nsTaskFilter = String.format("sourceSendPackageDeptId = %d AND taskId = %d AND id != \"%s\"", taskImageIndex.getSourceSendPackageDeptId(), taskImageIndex.getTaskId(), taskImageIndex.getId());
|
|
|
List<TaskImageIndex.MultiResult> nsTaskResult = knnSearch(namespace, taskImageIndex.getVector(), nsTaskFilter, "task");
|
|
List<TaskImageIndex.MultiResult> nsTaskResult = knnSearch(namespace, taskImageIndex.getVector(), nsTaskFilter, "task");
|
|
|
multiResultList.addAll(nsTaskResult);
|
|
multiResultList.addAll(nsTaskResult);
|
|
|
taskImageIndex.setMultiResult(JacksonUtils.toJson(multiResultList));
|
|
taskImageIndex.setMultiResult(JacksonUtils.toJson(multiResultList));
|
|
@@ -208,23 +208,29 @@ public class OpenSearchHandler implements PojoRequestHandler<TaskImageMsg, List<
|
|
|
knn.setTopK(TOP_K);
|
|
knn.setTopK(TOP_K);
|
|
|
knn.setFilter(filter);
|
|
knn.setFilter(filter);
|
|
|
knn.setScoreThreshold(SCORE_THRESHOLD);
|
|
knn.setScoreThreshold(SCORE_THRESHOLD);
|
|
|
- knn.setOutputFields(List.of("id", "namespace", "fields"));
|
|
|
|
|
|
|
|
|
|
SearchRequest request = new SearchRequest();
|
|
SearchRequest request = new SearchRequest();
|
|
|
request.setTableName(TABLE_NAME);
|
|
request.setTableName(TABLE_NAME);
|
|
|
request.setKnn(knn);
|
|
request.setKnn(knn);
|
|
|
|
|
+ request.setOutputFields(List.of("taskId", "image"));
|
|
|
|
|
|
|
|
SearchResponse response = OpenSearchUtils.getClient().search(request);
|
|
SearchResponse response = OpenSearchUtils.getClient().search(request);
|
|
|
- return parseToMultiResultList(response.getBody(), type);
|
|
|
|
|
|
|
+ String responseBody = response.getBody();
|
|
|
|
|
+ log.info("knn search filter:{}, namespace:{}, response:{}", filter, namespace, responseBody);
|
|
|
|
|
+
|
|
|
|
|
+ JsonNode bodyNode = JacksonUtils.toJsonNode(responseBody);
|
|
|
|
|
+ if (bodyNode.has("errorCode")) {
|
|
|
|
|
+ log.error("knn search fail. filter:{}, namespace:{}, response:{}", filter, namespace, responseBody);
|
|
|
|
|
+ throw new RuntimeException("knn search failed");
|
|
|
|
|
+ }
|
|
|
|
|
+ return parseToMultiResultList(bodyNode, type);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("knnSearch exception, filter:{}", filter, e);
|
|
log.error("knnSearch exception, filter:{}", filter, e);
|
|
|
throw new RuntimeException("对比异常,filter:" + filter, e);
|
|
throw new RuntimeException("对比异常,filter:" + filter, e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private List<TaskImageIndex.MultiResult> parseToMultiResultList(String body, String type) {
|
|
|
|
|
- JsonNode bodyNode = JacksonUtils.toJsonNode(body);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ private List<TaskImageIndex.MultiResult> parseToMultiResultList(JsonNode bodyNode, String type) {
|
|
|
JsonNode resultArray;
|
|
JsonNode resultArray;
|
|
|
if (bodyNode == null || !bodyNode.has("result")
|
|
if (bodyNode == null || !bodyNode.has("result")
|
|
|
|| !(resultArray = bodyNode.get("result")).isArray() || resultArray.isEmpty()) {
|
|
|| !(resultArray = bodyNode.get("result")).isArray() || resultArray.isEmpty()) {
|
|
@@ -235,6 +241,7 @@ public class OpenSearchHandler implements PojoRequestHandler<TaskImageMsg, List<
|
|
|
for (JsonNode jsonNode : resultArray) {
|
|
for (JsonNode jsonNode : resultArray) {
|
|
|
TaskImageIndex.MultiResult multiResult = new TaskImageIndex.MultiResult();
|
|
TaskImageIndex.MultiResult multiResult = new TaskImageIndex.MultiResult();
|
|
|
JsonNode fields = jsonNode.get("fields");
|
|
JsonNode fields = jsonNode.get("fields");
|
|
|
|
|
+
|
|
|
multiResult.setTaskId(fields.get("taskId").asLong());
|
|
multiResult.setTaskId(fields.get("taskId").asLong());
|
|
|
multiResult.setImage(fields.get("image").asText());
|
|
multiResult.setImage(fields.get("image").asText());
|
|
|
|
|
|
|
@@ -244,6 +251,7 @@ public class OpenSearchHandler implements PojoRequestHandler<TaskImageMsg, List<
|
|
|
multiResult.setScore(jsonNode.get("similarityScore").asDouble());
|
|
multiResult.setScore(jsonNode.get("similarityScore").asDouble());
|
|
|
}
|
|
}
|
|
|
multiResult.setType(type);
|
|
multiResult.setType(type);
|
|
|
|
|
+ list.add(multiResult);
|
|
|
}
|
|
}
|
|
|
return list;
|
|
return list;
|
|
|
}
|
|
}
|