ES8 向量搜索(knn-search)java-api 实践
简介
kNN搜索
k-nearest neighbor(kNN)搜索找到与查询向量最近的k个向量,如通过相似度计算。
kNN的常见用例包括:
基于自然语言处理(NLP)算法的相关性排序
产品推荐和推荐引擎
图像或视频的相似性搜索
要运行kNN搜索,您必须能够将数据转换为有意义的向量值。您可以在Elasticsearch之外创建这些向量,并将它们作为dense_vvector字段值添加到文档中。查询表示为具有相同维度的向量。
根据相似性度设计向量,使文档的向量与查询向量越接近匹配度越高。
kNN方法:
ES支持两种kNN搜索的方法:
近似kNN:使用kNN搜索选项
精确暴力kNN:利用向量函数script_score查询
多数情况下,我们只需要使用近似kNN。近似kNN提供低延迟是以较慢的索引速度和不完美的准确性为代价。
暴力搜索必须扫描每个匹配的文档节点来计算向量函数,这将导致搜索非常慢。我们可以通过限制传给向量函数的文档数量来降低延迟。比如我们通过其他查询条件能筛选出一个很小的文档子集,那么我们就可以通过这个方法获取较好的性能。
近似kNN搜索
运行近似kNN搜索,使用kNN选项搜索一个或多个启用索引的 dense_vector
字段。
1) 显式映射一个或多个密集向量字段。近似kNN搜索需要以下映射选项:
- index字段设置为true。
similarity
字段,该值确定用于基于查询和文档向量之间的相似性对文档进行评分的相似性度量方式,如l2_norm,欧氏距离等。
创建向量索引
在创建索引时需要选择dense_vector类型并指定index为true,并定义索引时文档间相似度量算法,此时es在索引文档时会为knn搜索生成新的索引文件,新的索引文件采用的是树加链表的存储结构,并根据similarity指定的相似算法计算文档间距离,特征相近的数据会存储在临近的分支里。
PUT image-index{ "mappings": { "properties": { "image-vector": { "type": "dense_vector", "dims": 3, "index": true, "similarity": "l2_norm" }, "title-vector": { "type": "dense_vector", "dims": 5, "index": true, "similarity": "l2_norm" }, "title": { "type": "text" }, "file-type": { "type": "keyword" } } }}
2)插入索引数据
POST image-index/_bulk?refresh=true{ "index": { "_id": "1" } }{ "image-vector": [1, 5, -20], "title-vector": [12, 50, -10, 0, 1], "title": "moose family", "file-type": "jpg" }{ "index": { "_id": "2" } }{ "image-vector": [42, 8, -15], "title-vector": [25, 1, 4, -12, 2], "title": "alpine lake", "file-type": "png" }{ "index": { "_id": "3" } }{ "image-vector": [15, 11, 23], "title-vector": [1, 5, 25, 50, 20], "title": "full moon", "file-type": "jpg" }
3)使用knn选项搜索
POST image-index/_search{ "knn": { "field": "image-vector", "query_vector": [-5, 9, -12], "k": 10, "num_candidates": 100 }, "fields": [ "title", "file-type" ]}
搜索结果中的_score为查询向量和文档向量之间的相似度,计算方式即为索引中指定的similarity字段。
num_candidates是检索分片时获得的候选集数量。类似于milvus的n_list。
为了收集结果,kNN搜索api在每个分片上查找一定数量(num_candidates )的近似最近邻候选对象。搜索计算候选向量与查询向量的相似度,从每个分片选择k个最相似的结果。搜索最终拿到每个分片返回的结果然后找到最终的k个邻近结果。
我们可以通过调大num_candidates的值来更精确的获取结果,代价是搜索的速度会变慢。如果使用一个比较大的num_candidates值会从每个分片获取更多的候选者。这就要花费更多的时间,也就有更大的可能找到真正的k个邻近结果。
类似的,我们可以降低num_candidates的值来获取更快的搜索,同时要接受潜在不太精确的结果。
kNN+filter搜索
POST image-index/_search{ "knn": { "field": "image-vector", "query_vector": [54, 10, -2], "k": 5, "num_candidates": 50, "filter": { "term": { "file-type": "png" } } }, "fields": ["title"], "_source": false}
java api
依赖:
org.elasticsearch.client elasticsearch-rest-client ${elasticsearch.version} co.elastic.clients elasticsearch-java ${elasticsearch.version} jakarta.json jakarta.json-api 2.1.1
ElasticSearchConfig
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;import co.elastic.clients.elasticsearch.ElasticsearchClient;import co.elastic.clients.json.jackson.JacksonJsonpMapper;import co.elastic.clients.transport.ElasticsearchTransport;import co.elastic.clients.transport.rest_client.RestClientTransport;import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.ssl.SSLContextBuilder;import org.apache.http.ssl.SSLContexts;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource; import javax.annotation.PostConstruct;import javax.net.ssl.SSLContext;import java.io.IOException;import java.io.InputStream;import java.security.KeyManagementException;import java.security.KeyStore;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.cert.Certificate;import java.security.cert.CertificateException;import java.security.cert.CertificateFactory; @Configuration//@Slf4jpublic class ElasticSearchConfig { @Value("${spring.elasticsearch.rest.host}") private String host; @Value("${spring.elasticsearch.rest.enable}") private boolean enable; @Value("${spring.elasticsearch.rest.port}") private int port; @Value("${spring.elasticsearch.rest.username}") private String userName; @Value("${spring.elasticsearch.rest.password}") private String passWord; @Value("${spring.elasticsearch.rest.crtName}") private String tempCrtName; private static String crtName; @PostConstruct private void init() { crtName = tempCrtName; } private HttpHost toHttpHost() { HttpHost httpHost = new HttpHost(host, port, "http"); return httpHost; } @Bean public ElasticsearchClient clientBySync() throws Exception { ElasticsearchTransport transport = getElasticsearchTransport(userName, passWord, toHttpHost()); return new ElasticsearchClient(transport); } @Bean public ElasticsearchAsyncClient clientByAsync() throws Exception { ElasticsearchTransport transport = getElasticsearchTransport(userName, passWord, toHttpHost()); return new ElasticsearchAsyncClient(transport); } @Bean public ElasticsearchTransport getTransport() throws Exception { return getElasticsearchTransport(userName, passWord, toHttpHost()); } private static SSLContext buildSSLContext() { ClassPathResource resource = new ClassPathResource(crtName); SSLContext sslContext = null; try { CertificateFactory factory = CertificateFactory.getInstance("X.509"); Certificate trustedCa; try (InputStream is = resource.getInputStream()) { trustedCa = factory.generateCertificate(is); } KeyStore trustStore = KeyStore.getInstance("pkcs12"); trustStore.load(null, null); trustStore.setCertificateEntry("ca", trustedCa); SSLContextBuilder sslContextBuilder = SSLContexts.custom() .loadTrustMaterial(trustStore, null); sslContext = sslContextBuilder.build(); } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) {// log.error("ES连接认证失败", e); } return sslContext; } private static ElasticsearchTransport getElasticsearchTransport(String username, String passwd, HttpHost... hosts) { // 账号密码的配置 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, passwd)); // 自签证书的设置,并且还包含了账号密码 RestClientBuilder.HttpClientConfigCallback callback = httpAsyncClientBuilder -> httpAsyncClientBuilder .setSSLContext(buildSSLContext()) .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setDefaultCredentialsProvider(credentialsProvider); // 用builder创建RestClient对象 RestClient client = RestClient .builder(hosts) .setHttpClientConfigCallback(callback) .build(); return new RestClientTransport(client, new JacksonJsonpMapper()); } }
批量插入/knn搜索测试
import co.elastic.clients.elasticsearch.ElasticsearchClient;import co.elastic.clients.elasticsearch._types.KnnQuery;import co.elastic.clients.elasticsearch._types.OpType;import co.elastic.clients.elasticsearch._types.aggregations.AvgAggregate;import co.elastic.clients.elasticsearch._types.aggregations.LongTermsBucket;import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;import co.elastic.clients.elasticsearch._types.query_dsl.Query;import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;import co.elastic.clients.elasticsearch.core.IndexRequest;import co.elastic.clients.elasticsearch.core.SearchResponse;import co.elastic.clients.elasticsearch.core.search.Hit;import co.elastic.clients.json.JsonData;import com.alibaba.fastjson2.JSON;import com.alibaba.fastjson2.JSONObject;import org.apache.commons.io.FileUtils;import org.apache.commons.lang3.StringUtils;import org.junit.jupiter.api.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.io.File;import java.io.IOException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;@SpringBootTestpublic class GulimallSearchApplicationTests { private final static Logger logger = LoggerFactory.getLogger(GulimallSearchApplicationTests.class); @Autowired private ElasticsearchClient elasticsearchClient; @Test public void insertTest() throws Exception { List list = FileUtils.readLines(new File("E:\\WorkData\\TEMP\\vec_output.txt"), Charset.defaultCharset()); List params = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { String s = list.get(i); if (StringUtils.isBlank(s) || !s.contains("|")) continue; JSONObject jsonObject = new JSONObject(); String[] split = s.split("\\|"); jsonObject.put("id", i); jsonObject.put("text", split[0]); List floats = JSON.parseArray(split[1], Float.class); jsonObject.put("vector", floats); params.add(jsonObject); } logger.info("list.size: {}", params.size()); for (JSONObject param : params) { insertTest(param); } } @Test public void searchTest() throws Exception { String vecStr = "[-0.13182495534420013, 0.5157117247581482, 0.1319814920425415, 1.2098767757415771, 0.3953569233417511, -0.7560071349143982, 0.6198253035545349, -0.3597263991832733, -0.7209910750389099, 0.020754046738147736, 0.3381970226764679, -0.08954983204603195, -0.20040489733219147, -0.41636574268341064, -0.9353054165840149, -1.1370081901550293, 1.0000590085983276, 0.8270509839057922, -0.08167970180511475, -0.7885590195655823, -0.5111125707626343, 0.40539664030075073, -0.9634650945663452, -0.19649815559387207, 0.04757082089781761, 0.1870611160993576, -0.8335414528846741, 0.1309504508972168, 0.9586320519447327, -0.8715129494667053, 0.06755723804235458, -0.9554585814476013, -0.08576131612062454, 0.26428577303886414, 0.2774205505847931, 0.5809488296508789, 0.4973503053188324, 0.9991136193275452, 0.6416786313056946, 0.35784733295440674, -0.5748403072357178, 1.3667298555374146, -0.2769220471382141, -1.0056191682815552, -0.0015217607142403722, 1.0278605222702026, 0.594118595123291, 0.016495073214173317, -0.17650540173053741, 0.16230060160160065, -0.2950599789619446, 4.102972507476807, -0.37414294481277466, -0.16938793659210205, -0.03754861280322075, -0.08116180449724197, -0.29197192192077637, 0.5375543832778931, -0.6057153940200806, -0.6121858954429626, -0.4848989248275757, -0.014912215061485767, 0.1414678692817688, -0.8915115594863892, 0.4597946107387543, -0.23475760221481323, -0.6506146788597107, 0.23127493262290955, 0.8497616052627563, -0.7529323101043701, 0.0990789383649826, 0.28742074966430664, -0.2789055109024048, 0.4401205778121948, 0.14829501509666443, 0.30746328830718994, 0.3696507513523102, -0.421092689037323, -0.30651789903640747, -1.6805227994918823, -1.6474555730819702, -0.5529795289039612, 0.0787125751376152, -0.5781314373016357, 0.27184024453163147, -0.5361534357070923, 0.6259598731994629, -0.5666404962539673, 0.04069717228412628, -0.23526407778263092, -0.013915907591581345, -0.5486372709274292, 0.3159649074077606, 0.31347692012786865, -0.2323826402425766, -0.5322315692901611, 1.687031626701355, 0.21880370378494263, -1.0829882621765137, 0.448579341173172, -0.31627780199050903, -0.19134844839572906, -0.4741955101490021, -0.2959926724433899, -0.08167844265699387, -0.6418899893760681, -0.21949152648448944, 1.0603184700012207, 0.2716708779335022, 0.4172762930393219, 0.6767337918281555, 0.4489956796169281, 0.31135472655296326, 1.2162410020828247, -0.29099252820014954, -0.34691035747528076, -0.31267741322517395, 0.6788668036460876, 0.18003250658512115, 0.277262806892395, -0.8890244960784912, -0.3449064791202545, 0.5557985305786133, -0.6230669021606445, 0.37174710631370544, 0.24448086321353912, 0.029452068731188774, -0.01744476705789566, 0.8030548095703125, 0.7772044539451599, 0.005610024556517601, -0.4318510890007019, 0.418354332447052, -0.9748033285140991, 1.0852757692337036, -0.15972934663295746, -0.9851102232933044, 0.21273697912693024, -0.11388485878705978, -0.7036041617393494, 0.23464618623256683, -0.22964368760585785, -0.2706468403339386, 0.5983942151069641, 0.14492236077785492, -0.22336621582508087, -0.4218609631061554, 0.5250900387763977, -0.5491968393325806, -0.0206316988915205, 1.4953666925430298, -0.2988232374191284, 0.0500728115439415, 0.3055689036846161, -0.20581506192684174, 0.47325918078422546, 0.3927423357963562, 0.21537713706493378, -0.8994218707084656, 0.5164554119110107, -0.47518566250801086, -0.5148254632949829, -0.5525524616241455, -0.428274542093277, -0.3298660218715668, 0.3335886597633362, 0.5330719947814941, -0.3562496304512024, 0.4405641555786133, -0.9655795693397522, -0.3933012783527374, 0.2653278708457947, -0.18105383217334747, 0.04351571947336197, -0.6949767470359802, -1.6579794883728027, 0.5834100246429443, 0.23504838347434998, -0.2642256021499634, -1.0315730571746826, -0.5085658431053162, -0.8408990502357483, 0.15439870953559875, 0.5801271796226501, -0.4847293198108673, -0.32450810074806213, -0.6427896618843079, 0.3529878258705139, 0.14031682908535004, 0.06927778571844101, -0.6370006203651428, 0.9013583660125732, 0.25866639614105225, -0.5697960257530212, 1.1949524879455566, 0.17150385677814484, 0.237138032913208, -0.04870197921991348, -0.5964035987854004, -0.11448823660612106, -0.4580298662185669, 0.7053911685943604, -0.10589952766895294, -0.24526232481002808, -0.5942148566246033, 0.8526578545570374, -0.6705660820007324, 0.1801866888999939, -0.36688634753227234, -1.145801067352295, -0.22329829633235931, -1.3021984100341797, -0.12443883717060089, -0.7209049463272095, 0.030842239037156105, 0.6281322836875916, -0.35514670610427856, 0.041103195399045944, -0.6183987259864807, -0.3495950400829315, -0.7058541774749756, -0.8919149041175842, -0.7663254141807556, 0.12116685509681702, -0.6132908463478088, -0.36863937973976135, 0.2288125902414322, 0.03786308318376541, 1.2080146074295044, 0.5902876257896423, 0.8224381804466248, 1.0623126029968262, -0.2048320770263672, -0.4951256811618805, -0.293998658657074, -0.13700070977210999, -0.45422008633613586, -0.496314138174057, 0.441482275724411, -0.9847365021705627, 0.2566785514354706, 0.11975473910570145, -0.47740915417671204, 0.33521854877471924, -0.9302647113800049, 0.4508187174797058, 0.28462669253349304, 0.05493105947971344, 0.19143490493297577, -0.3292771577835083, 0.3647611737251282, -0.4862430691719055, 0.4954574704170227, -0.4188776910305023, -0.237763449549675, 0.06350546330213547, -0.2960949242115021, 0.18101607263088226, 0.07952415198087692, -0.6436824202537537, 0.7179908156394958, -0.1085243746638298, 0.4656209945678711, 0.2625783681869507, -0.17761348187923431, 0.6150626540184021, 0.02531386725604534, -0.2934170067310333, 0.21907073259353638, 0.8318471312522888, -0.017382292076945305, 0.7556049823760986, -0.8854416012763977, -0.37819862365722656, -0.15984493494033813, -0.2588607966899872, 0.03155376762151718, 1.1375298500061035, 0.5484519600868225, -0.1885809451341629, 0.16556882858276367, 0.3007625341415405, -0.5498548150062561, -0.10727713257074356, -1.5526013374328613, -0.6138827800750732, -0.00927730556577444, 0.3637949228286743, -0.6362797021865845, -0.416730672121048, -0.20451897382736206, 0.42760780453681946, -0.3642737567424774, -0.05171944200992584, 2.193127155303955, 0.3752421438694, -0.4772227108478546, -0.1571044623851776, 0.048126768320798874, 0.06681513041257858, 0.14706824719905853, -1.0641165971755981, -1.150582194328308, 0.4660224914550781, -0.680321991443634, 0.26007553935050964, 0.22052587568759918, -0.5197380781173706, -0.049790024757385254, -0.2243952602148056, -0.025090938434004784, -0.33555111289024353, 0.21677498519420624, -0.4016933739185333, 0.2778061330318451, -0.18889206647872925, -0.5847033262252808, 0.9561736583709717, 0.5196832418441772, 0.39443084597587585, -0.5247765779495239, 0.2868853807449341, 0.511779248714447, 1.005184531211853, 0.47597357630729675, -0.07979296892881393, -0.47314393520355225, -1.241675853729248, -0.7985542416572571, 0.10842403769493103, -0.17005139589309692, -0.4917040169239044, 0.8768584728240967, 0.535133421421051, -0.07252436131238937, 0.6858885884284973, 0.7331241369247437, -0.2751208245754242, -0.9440416693687439, -0.41564473509788513, -0.11350453644990921, 0.3334425389766693, 0.3480837345123291, -0.23504509031772614, 0.1451176553964615, 0.3170160949230194, 0.866788923740387, -0.5282850861549377, 0.2266891896724701, -0.5902971029281616, -0.3470693528652191, 0.14048701524734497, 0.39951181411743164, -0.6173489093780518, -0.37535718083381653, 0.8005176782608032, 0.46135374903678894, 0.41313257813453674, -0.5368387699127197, 0.499979704618454, 0.4211004972457886, 0.09839384257793427, -0.6255232095718384, 0.6962053775787354, 0.882672905921936, -0.1713053584098816, 0.6673985719680786, -0.272347629070282, -0.32828524708747864, -0.20183315873146057, 0.7098619937896729, 0.3017594814300537, 0.5318636298179626, 1.7675861120224, 0.012352179735898972, 0.010712483897805214, -0.04014990106225014, 0.5191746354103088, -0.3836316764354706, -0.7280094027519226, 1.015629529953003, -1.1922037601470947, -0.08163293451070786, -0.9618234038352966, -0.610783576965332, 0.7273927330970764, -1.0529825687408447, 0.6393487453460693, -0.541419506072998, -0.5954841375350952, -0.45172253251075745, 0.1662188172340393, 0.05707554891705513, 0.14310164749622345, 0.5063771605491638, -0.30338725447654724, -0.163277730345726, 1.4947395324707031, -0.1076478660106659, 0.15448588132858276, -0.3937961757183075, -0.8191974759101868, 0.6984464526176453, -0.6114234328269958, 1.0325021743774414, -1.3374066352844238, -0.2543559968471527, -0.3035518527030945, 0.7834022641181946, -0.4380779266357422, -0.808858335018158, 0.9451856017112732, -1.2228585481643677, -1.5188595056533813, -0.7385101318359375, -0.11116594821214676, 0.843401312828064, 0.1516697108745575, 0.626079261302948, 1.0731736421585083, 0.31143417954444885, -0.4911830723285675, -0.28935763239860535, -0.2199721485376358, 1.3113754987716675, 0.2010679841041565, -0.43805864453315735, -0.13089609146118164, -0.38402947783470154, -0.66057288646698, -1.3370413780212402, 0.6173458099365234, -0.6066834926605225, 0.6050617694854736, -0.9005452990531921, 0.8839082717895508, -0.35749655961990356, 0.15900133550167084, 0.3107379376888275, -0.8992351293563843, -0.935965895652771, 0.2091383934020996, 0.4970630407333374, 0.010194439440965652, 0.08995712548494339, 0.5939480066299438, 0.3774252235889435, -1.0204596519470215, -0.1013791635632515, -0.4761205017566681, 0.30110371112823486, 0.4602327346801758, -0.8037660717964172, -0.48142579197883606, 0.15831530094146729, 0.01706138625741005, 0.007952406071126461, -0.9372773766517639, 1.1439601182937622, 0.7907620072364807, 0.45220059156417847, 0.0955483615398407, -1.1939895153045654, -0.15391187369823456, 0.2938200533390045, -0.14216966927051544, 1.453192114830017, 0.23065508902072906, 0.4366813004016876, 0.8709571957588196, -0.2993483543395996, 0.4179665744304657, 0.5071791410446167, 0.36307021975517273, -0.29186442494392395, -0.2681388556957245, -0.6282910704612732, -0.08568862080574036, -0.015933340415358543, -0.03570815920829773, 0.5331277847290039, 0.23552647233009338, -0.40676259994506836, -0.9448947906494141, 0.1362900286912918, -0.3107372224330902, 0.8102473616600037, 0.2651998698711395, -0.5928671360015869, -0.02949046529829502, 0.325480192899704, 0.09235880523920059, 0.4541666805744171, -0.47440433502197266, 0.207527294754982, -0.8426303267478943, -0.05481724441051483, 0.6139618754386902, -0.3128722906112671, -0.11944282799959183, 0.032050203531980515, 0.20112024247646332, 0.30582165718078613, 0.635436475276947, -0.7376264929771423, -0.4206443727016449, -0.37938690185546875, 0.8806634545326233, 0.5800477266311646, -0.31377217173576355, -0.07245708256959915, 1.1369770765304565, 0.7184438705444336, -0.569002091884613, -0.28139057755470276, -0.6318895816802979, -0.32201603055000305, 0.11833372712135315, 1.459011435508728, 1.4220099449157715, 0.14647451043128967, -0.162994846701622, -0.05369144305586815, 0.5978099703788757, -0.5627399682998657, 0.45026838779449463, -0.7712469696998596, -0.8628026843070984, -0.21038389205932617, 1.29502534866333, 0.15517395734786987, 0.4604344666004181, -0.5019217133522034, 0.7621732950210571, -0.1224614679813385, 0.6718882322311401, 0.896763801574707, 0.7912658452987671, 0.2909198999404907, -0.44716957211494446, -1.3621968030929565, 0.5572307705879211, -0.10829555243253708, 0.06402679532766342, -0.8576376438140869, -0.7057247161865234, 0.2881285548210144, -0.6362447738647461, 0.16544117033481598, 0.187337264418602, -0.5961824059486389, -0.5454924702644348, -0.35544416308403015, 0.19072268903255463, 0.5735036730766296, 0.07992382347583771, -0.4518664479255676, 0.0013552362797781825, 0.31833693385124207, -1.0987396240234375, -0.23536378145217896, -0.16740860044956207, -0.5614094138145447, 0.9381183385848999, -0.5282272696495056, 0.5642679929733276, -0.11480367928743362, -0.48194000124931335, -0.06250406056642532, 0.1292407512664795, 0.3450099229812622, 0.17109043896198273, 0.3283403217792511, 0.4389880299568176, 0.08660338073968887, -0.09206132590770721, 0.512690007686615, 0.7636525630950928, 0.3890857398509979, 0.7700429558753967, 0.2658924162387848, 1.1760928630828857, -1.1317532062530518, 1.0531471967697144, -0.09364710003137589, -0.3774707615375519, 0.0074880183674395084, -1.0342662334442139, 0.7783626317977905, -0.31503450870513916, 0.22905564308166504, -0.07266593724489212, -0.4132945239543915, 0.6285417079925537, 0.7547109127044678, 0.6334087252616882, 0.6159496903419495, -0.2305477261543274, 0.13069845736026764, 0.7669737339019775, 0.856577455997467, 0.006858898792415857, 0.457430362701416, 0.07513565570116043, 1.4533030986785889, 0.2834520936012268, 0.3436378240585327, -0.38849207758903503, 0.6437581181526184, 0.3927347958087921, -0.5175148248672485, 0.7453470826148987, -0.31686699390411377, -0.4638811945915222, -0.5511123538017273, 0.181724414229393, 0.2632334530353546, 0.5536782741546631, -0.717678964138031, -0.6810265779495239, -0.061758410185575485, -0.07204370945692062, 0.7549692392349243, 0.2700137197971344, 0.4005989730358124, -0.4768402874469757, 0.23312702775001526, 0.5981244444847107, -0.7396137714385986, -0.45443764328956604, -0.1116941049695015, 0.2543375790119171, -0.19807347655296326, 0.001302754390053451, 1.3985486030578613, 0.7423760890960693, -0.9008021950721741, -0.4925093352794647, -0.6138692498207092, 0.9644115567207336, 0.17153577506542206, 0.6256873607635498, 0.7303361296653748, -0.5501835942268372, 0.4467894732952118, -0.18884125351905823, 1.4195194244384766, 0.1901400238275528, -1.3081344366073608, -0.5504124164581299, -0.8520178198814392, 1.1249363422393799, -0.25002679228782654, -0.5372235774993896, 0.5109835267066956, 0.25000157952308655, 0.13999618589878082, -0.28450676798820496, -0.6249191164970398, 0.1488274186849594, 0.9032642245292664, -0.17740009725093842, -0.3793050944805145, -0.8580751419067383, 0.6907321214675903, -0.7103999257087708, -1.128669261932373, 0.3951438069343567, 0.04220225661993027, 1.047918438911438, 1.2307379245758057, 0.9939243197441101, -1.1905916929244995, -1.1036252975463867, -1.1394094228744507, -0.04481160640716553, -0.03766907751560211, -0.9090474843978882, -0.713390588760376, -0.4805450141429901, -0.09850222617387772, 0.6759057641029358, 0.8166624307632446, 0.7544222474098206, -0.4656640887260437, -1.116026759147644, -0.59232097864151, -0.24339403212070465, -0.7609081864356995, 0.05218454450368881, 0.11115903407335281, 0.8679887652397156, -0.6800999045372009, 0.0033064077142626047, 0.6069257855415344, 1.6216983795166016, -0.33234891295433044, 0.4253925085067749, 0.27553609013557434, 0.6046768426895142, -0.07929065823554993, 0.8591046929359436, 0.3208215534687042, -0.19023066759109497, -0.17903658747673035, 0.13604125380516052, -0.01308573130518198, -0.9396982192993164, 0.35946783423423767, -0.10077157616615295, -0.4185013473033905, -0.136262908577919, 0.4924154579639435, -0.18913616240024567, 0.7712398767471313, 0.011921069584786892, -0.08248690515756607, -0.1711365431547165, -0.38438987731933594, -1.2819545269012451, 0.9156429171562195, -0.09064939618110657, -0.16613249480724335, -1.158524990081787, 0.6762368083000183, 0.4363611340522766, -0.8340129256248474, 0.34485679864883423, -0.21829324960708618, -0.20657749474048615, -0.4783969223499298, 0.8621751666069031, -0.40753233432769775, -0.1194688156247139, -0.8974472284317017, 1.2560043334960938, 0.3465055823326111, -0.32614248991012573, 0.03557245433330536, -0.3421289324760437, -1.21098792552948, 0.4357704222202301, -0.1985855996608734, -0.4948621094226837, -0.20760613679885864, -0.740304708480835, -0.1639600247144699, -1.050977110862732, -0.6332544684410095, 0.6931124329566956, 0.2925596535205841, 0.550227165222168, 0.6792681217193604, 0.23355303704738617, -1.486087441444397, -0.30686768889427185, 2.6258504390716553, -0.03426945209503174, 0.7802882790565491, 0.7526844143867493, 0.5387230515480042, 0.06050233915448189, 0.635113537311554, 0.3891644775867462, -0.6100666522979736, 0.014770970679819584, -1.2967066764831543, -0.23108898103237152]"; List floats = JSON.parseArray(vecStr, Float.class); KnnQuery knnQuery = KnnQuery.of(m -> m.field("vector").queryVector(floats).k(3).numCandidates(100)); Query rangeQuery = RangeQuery.of(q -> q.field("id").lte(JsonData.of(100)))._toQuery(); SearchResponse search = elasticsearchClient.search(s -> s .index("demo_knn2") .query(rangeQuery) .knn(knnQuery) , JSONObject.class); System.out.println(search); List result = new ArrayList<>(); for (Hit hit : search.hits().hits()) { JSONObject source = hit.source(); source.put("score", hit.score()); result.add(source); } logger.info("SearchResponse: {}", JSON.toJSONString(result)); } @Test public void insertTest(JSONObject jsonObject) throws Exception { IndexRequest indexRequest = new IndexRequest.Builder() .index("demo_knn2") .opType(OpType.Index) .id(jsonObject.getString("id")) .document(jsonObject) .build(); elasticsearchClient.index(indexRequest); }}
来源地址:https://blog.csdn.net/VISION_SONG/article/details/131283785
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341