|
@@ -0,0 +1,51 @@
|
|
|
|
+package com.example.xiaoshiweixinback.service.common;
|
|
|
|
+
|
|
|
|
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.InlineScript;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.Script;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
|
+import co.elastic.clients.json.JsonData;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
|
+public class EsService {
|
|
|
|
+
|
|
|
|
+ private final ElasticsearchClient client;
|
|
|
|
+
|
|
|
|
+ public List<Integer> getTenderList(List<Float> imageList) throws IOException {
|
|
|
|
+ List<Integer> list = new ArrayList<>();
|
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
|
+ //设置查询索引
|
|
|
|
+ builder.index("tender");
|
|
|
|
+
|
|
|
|
+ String source = "cosineSimilarity(params.queryVector, 'my_vector') + 1.0";
|
|
|
|
+ InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").params("queryVector", JsonData.of(imageList)).source(source));
|
|
|
|
+ Script script = Script.of(i -> i.inline(inlineScript));
|
|
|
|
+ Query query = QueryBuilders.scriptScore(i -> i.script(script).query(org.springframework.data.elasticsearch.client.elc.QueryBuilders.matchAllQueryAsQuery()));
|
|
|
|
+ builder.query(query);
|
|
|
|
+ builder.size(10);
|
|
|
|
+ SearchResponse<Integer> response = client.search(builder.build(), Integer.class);
|
|
|
|
+ List<Hit<Integer>> hits = response.hits().hits();
|
|
|
|
+ double scoreThreshold = 1.95;
|
|
|
|
+ for (Hit<Integer> hit : hits) {
|
|
|
|
+ Double score = hit.score();
|
|
|
|
+ Integer tender = hit.source();
|
|
|
|
+ if (score > scoreThreshold) {
|
|
|
|
+ list.add(tender);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|