{"id":6725,"date":"2025-09-29T13:46:57","date_gmt":"2025-09-29T05:46:57","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=6725"},"modified":"2025-09-29T13:47:06","modified_gmt":"2025-09-29T05:47:06","slug":"springboot%e4%b8%8evector-search%e6%95%b4%e5%90%88%ef%bc%8c%e5%ae%9e%e7%8e%b0%e7%9f%ad%e8%a7%86%e9%a2%91%e5%86%85%e5%ae%b9%e5%ae%9e%e6%97%b6%e6%8e%a8%e8%8d%90%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/spring-boot\/springboot%e4%b8%8evector-search%e6%95%b4%e5%90%88%ef%bc%8c%e5%ae%9e%e7%8e%b0%e7%9f%ad%e8%a7%86%e9%a2%91%e5%86%85%e5%ae%b9%e5%ae%9e%e6%97%b6%e6%8e%a8%e8%8d%90%e7%b3%bb%e7%bb%9f\/","title":{"rendered":"SpringBoot\u4e0eVector Search\u6574\u5408\uff0c\u5b9e\u73b0\u77ed\u89c6\u9891\u5185\u5bb9\u5b9e\u65f6\u63a8\u8350\u7cfb\u7edf"},"content":{"rendered":"\n<p>\u76f8\u6bd4\u4f20\u7edf\u7684\u57fa\u4e8e\u5173\u952e\u8bcd\u5339\u914d\u7684\u641c\u7d22\uff0c\u5411\u91cf\u641c\u7d22\u80fd\u6355\u6349\u5230\u66f4\u6df1\u5c42\u6b21\u7684\u8bed\u4e49\u76f8\u4f3c\u6027\u3002\u5373\u4f7f\u4e24\u4e2a\u89c6\u9891\u6807\u9898\u6ca1\u6709\u5171\u540c\u8bcd\u6c47\uff0c\u4f46\u5982\u679c\u5185\u5bb9\u76f8\u4f3c\uff0c\u5b83\u4eec\u7684\u5411\u91cf\u4e5f\u4f1a\u63a5\u8fd1\uff0c\u5c31\u80fd\u88ab\u63a8\u8350\u51fa\u6765\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u5411\u91cf (Vector)\uff1f<\/h1>\n\n\n\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u4f60\u6709\u4e00\u6bb5\u6587\u5b57\uff0c\u6bd4\u5982\u4e00\u53e5\u8bdd\uff1a\u201c\u6211\u559c\u6b22\u5728\u9633\u5149\u660e\u5a9a\u7684\u65e5\u5b50\u91cc\u53bb\u516c\u56ed\u6563\u6b65\u201d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f20\u7edf\u65b9\u5f0f: \u8ba1\u7b97\u673a\u53ef\u80fd\u628a\u5b83\u770b\u4f5c\u4e00\u4e2a\u7531\u5b57\u7b26\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u5411\u91cf\u5316: \u901a\u8fc7\u67d0\u79cd\u7b97\u6cd5\uff08\u6bd4\u5982 Word2Vec, BERT \u7b49\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e2a\u53e5\u5b50\u8f6c\u6362\u6210\u4e00\u7ec4\u6570\u5b57\uff0c\u6bd4\u5982\u00a0<code>[0.2, -0.5, 0.8, 0.1, ...]<\/code>\u3002\u8fd9\u7ec4\u6570\u5b57\u7ec4\u6210\u7684\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u5411\u91cf\u3002\u8fd9\u4e2a\u5411\u91cf\u7684\u6bcf\u4e2a\u7ef4\u5ea6\uff080.2, -0.5...\uff09\u90fd\u7f16\u7801\u4e86\u8fd9\u53e5\u8bdd\u7684\u67d0\u4e9b\u8bed\u4e49\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">\u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u5411\u91cf\u5462\uff1f<\/h1>\n\n\n\n<p>\u56e0\u4e3a\u8ba1\u7b97\u673a\u5f88\u64c5\u957f\u5904\u7406\u6570\u5b57\uff0c\u4f46\u4e0d\u592a\u5bb9\u6613\u76f4\u63a5\u7406\u89e3\u6587\u5b57\u3001\u56fe\u7247\u3001\u58f0\u97f3\u7684\u201c\u542b\u4e49\u201d\u3002\u5411\u91cf\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u5f0f\uff0c\u628a\u590d\u6742\u7684\u975e\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u6587\u672c\u3001\u56fe\u50cf\uff09\u6620\u5c04\u5230\u4e00\u4e2a\u9ad8\u7ef4\u7684\u6570\u5b57\u7a7a\u95f4\uff08\u5411\u91cf\u7a7a\u95f4\uff09\u91cc\u3002\u5728\u8fd9\u4e2a\u7a7a\u95f4\u91cc\uff0c\u76f8\u4f3c\u7684\u4e1c\u897f\uff08\u6bd4\u5982\u610f\u601d\u76f8\u8fd1\u7684\u53e5\u5b50\uff0c\u6216\u8005\u89c6\u89c9\u4e0a\u76f8\u4f3c\u7684\u56fe\u7247\uff09\u5b83\u4eec\u7684\u5411\u91cf\u5c31\u4f1a\u6bd4\u8f83\u63a5\u8fd1\uff08\u5728\u7a7a\u95f4\u91cc\u7684\u8ddd\u79bb\u6bd4\u8f83\u8fd1\uff09\uff0c\u4e0d\u76f8\u4f3c\u7684\u4e1c\u897f\u5411\u91cf\u5c31\u4f1a\u6bd4\u8f83\u8fdc\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5b9e\u73b0\u6b65\u9aa4<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6570\u636e\u51c6\u5907:\n<ul class=\"wp-block-list\">\n<li>\u7528\u6237\u753b\u50cf\u5411\u91cf\u5316: \u7cfb\u7edf\u6536\u96c6\u7528\u6237\u7684\u884c\u4e3a\u6570\u636e\uff08\u770b\u4e86\u54ea\u4e9b\u89c6\u9891\u3001\u70b9\u8d5e\u4e86\u54ea\u4e9b\u3001\u505c\u7559\u65f6\u95f4\u7b49\uff09\uff0c\u7136\u540e\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u628a\u8fd9\u4e9b\u884c\u4e3a\u62bd\u8c61\u6210\u4e00\u4e2a\u6570\u5b57\u5217\u8868\uff0c\u6bd4\u5982\u00a0<code>[0.7, -0.2, 0.5, ...]<\/code>\uff08128\u7ef4\uff09\uff0c\u8fd9\u5c31\u662f\u7528\u6237\u5174\u8da3\u5411\u91cf\u3002<\/li>\n\n\n\n<li>\u89c6\u9891\u5185\u5bb9\u5411\u91cf\u5316: \u7cfb\u7edf\u5206\u6790\u89c6\u9891\u7684\u5143\u6570\u636e\u3001\u89c6\u89c9\u7279\u5f81\u3001\u97f3\u9891\u7279\u5f81\u7b49\uff0c\u4e5f\u7528\u6a21\u578b\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u6570\u5b57\u5217\u8868\uff0c\u6bd4\u5982\u00a0<code>[0.6, -0.1, 0.4, ...]<\/code>\uff08128\u7ef4\uff09\uff0c\u8fd9\u5c31\u662f\u89c6\u9891\u5185\u5bb9\u5411\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5b58\u50a8\u5230 Elasticsearch: \u00a0 (\u7559\u610f\u4e0b\u5b57\u6bb5\u7c7b\u578b\u662fdense_vector)<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u7528\u6237\u7684\u6587\u6863\u91cc\uff0c\u9664\u4e86\u5b58\u00a0<code>userId<\/code>,\u00a0<code>username<\/code>\uff0c\u8fd8\u4f1a\u5b58\u4e00\u4e2a\u00a0<code>interestVector<\/code>\u00a0\u5b57\u6bb5\uff0c\u7c7b\u578b\u662f\u00a0<code>dense_vector<\/code>\uff0c\u503c\u5c31\u662f\u4e0a\u9762\u7b97\u51fa\u6765\u7684\u5411\u91cf\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u89c6\u9891\u7684\u6587\u6863\u91cc\uff0c\u9664\u4e86\u5b58\u00a0<code>videoId<\/code>,\u00a0<code>title<\/code>,\u00a0<code>category<\/code>\uff0c\u8fd8\u4f1a\u5b58\u4e00\u4e2a\u00a0<code>contentVector<\/code>\u00a0\u5b57\u6bb5\uff0c\u7c7b\u578b\u4e5f\u662f\u00a0<code>dense_vector<\/code>\uff0c\u503c\u662f\u89c6\u9891\u7684\u5411\u91cf\u3002<\/li>\n\n\n\n<li>\u6267\u884c\u63a8\u8350 (\u5411\u91cf\u641c\u7d22):\u00a0 \u00a0 3.1 \u5f53\u7528\u6237 A \u8bf7\u6c42\u63a8\u8350\u65f6\uff0c\u7cfb\u7edf\u83b7\u53d6\u5230 A \u7684\u00a0<code>interestVector<\/code>\uff08\u67e5\u8be2\u5411\u91cf\uff09\u3002\u00a0\u00a0 \u00a0 3.2 \u7cfb\u7edf\u5411 Elasticsearch \u53d1\u8d77\u4e00\u4e2a\u00a0<code>knn<\/code>\u00a0\u641c\u7d22\u8bf7\u6c42\uff1a\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a03.2.1 \u5728\u00a0<code>videos<\/code>\u00a0\u7d22\u5f15\u91cc\u641c\u7d22\u3002\u00a0 \u00a0 \u00a0 \u00a0 \u00a03.2.2 \u4f7f\u7528\u5b57\u6bb5\u00a0<code>contentVector<\/code>\u00a0\u8fdb\u884c kNN \u641c\u7d22\u3002\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a03.2.3 \u67e5\u8be2\u5411\u91cf\u662f\u7528\u6237 A \u7684\u00a0<code>interestVector<\/code>\u3002\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a03.2.4 \u8981\u6c42\u8fd4\u56de\u6700\u76f8\u4f3c\u7684\u00a0<code>k<\/code>\u00a0\u4e2a\u89c6\u9891 (<code>k=10<\/code>)\u3002\u00a0\u00a0 \u00a0 3.3 \u00a0 Elasticsearch \u5185\u90e8\u5229\u7528\u4f18\u5316\u7b97\u6cd5\uff0c\u5feb\u901f\u8ba1\u7b97\u5e76\u627e\u51fa\u90a3\u4e9b\u00a0<code>contentVector<\/code>\u00a0\u548c\u7528\u6237 A \u7684\u00a0<code>interestVector<\/code>\u00a0\u6700\u63a5\u8fd1\u7684\u89c6\u9891\u6587\u6863\u3002\u00a0https:\/\/wxa.wxs.qq.com\/tmpl\/oc\/base_tmpl.html\u00a0 \u00a0 \u00a03.4 \u00a0 Elasticsearch \u8fd4\u56de\u8fd9\u4e9b\u6700\u76f8\u4f3c\u7684\u89c6\u9891\u6587\u6863\u7ed9\u4f60\u7684\u7528\u6237\uff0c\u5b8c\u6210\u63a8\u8350\u3002<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">\u4ee3\u7801\u5b9e\u64cd<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- Spring Boot Data Elasticsearch \u6a21\u5757\uff0c\u63d0\u4f9b\u4e0e ES \u7684\u96c6\u6210 --&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;dependency&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;\/artifactId&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/dependency&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- Elasticsearch Java API Client\uff0c\u7528\u4e8e\u6267\u884c\u66f4\u590d\u6742\u7684\u539f\u751f ES \u67e5\u8be2 --&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;dependency&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;groupId&gt;co.elastic.clients&lt;\/groupId&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;artifactId&gt;elasticsearch-java&lt;\/artifactId&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;version&gt;8.13.2&lt;\/version&gt; &lt;!-- \u7248\u672c\u9700\u4e0e\u4f60\u7684 Elasticsearch \u670d\u52a1\u7248\u672c\u5339\u914d --&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/dependency&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;dependency&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">application.properties<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code># \u914d\u7f6e Elasticsearch \u8fde\u63a5\u4fe1\u606f<br>elasticsearch.host=localhost<br>elasticsearch.port=9200<br><br># \u5f00\u542f Elasticsearch \u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u5c42\u65e5\u5fd7\uff0c\u7528\u4e8e\u8c03\u8bd5<br># logging.level.org.springframework.data.elasticsearch.client.WIRE=trace<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Elasticsearch\u5ba2\u6237\u7aef\u914d\u7f6e\u7c7b<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.config;<br><br>import&nbsp;co.elastic.clients.elasticsearch.ElasticsearchClient;<br>import&nbsp;co.elastic.clients.json.jackson.JacksonJsonpMapper;<br>import&nbsp;co.elastic.clients.transport.ElasticsearchTransport;<br>import&nbsp;co.elastic.clients.transport.rest_client.RestClientTransport;<br>import&nbsp;org.apache.http.HttpHost;<br>import&nbsp;org.elasticsearch.client.RestClient;<br>import&nbsp;org.springframework.beans.factory.annotation.Value;<br>import&nbsp;org.springframework.context.annotation.Bean;<br>import&nbsp;org.springframework.context.annotation.Configuration;<br>import&nbsp;org.springframework.data.elasticsearch.client.ClientConfiguration;<br>import&nbsp;org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;<br>import&nbsp;org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;<br><br>@Configuration<br>@EnableElasticsearchRepositories(basePackages =&nbsp;\"com.example.repository\")<br>publicclass&nbsp;ElasticsearchConfig&nbsp;extends&nbsp;ElasticsearchConfiguration&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;@Value(\"${elasticsearch.host}\")<br>&nbsp; &nbsp;&nbsp;private&nbsp;String host;<br><br>&nbsp; &nbsp;&nbsp;@Value(\"${elasticsearch.port}\")<br>&nbsp; &nbsp;&nbsp;privateint&nbsp;port;<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u914d\u7f6e Spring Data Elasticsearch \u4f7f\u7528\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<br>&nbsp; &nbsp; &nbsp;* \u8fd9\u662f Spring Data Elasticsearch \u63a8\u8350\u7684\u65b9\u5f0f\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;ClientConfiguration \u5bf9\u8c61<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@Override<br>&nbsp; &nbsp;&nbsp;public&nbsp;ClientConfiguration&nbsp;clientConfiguration()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;ClientConfiguration.builder()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .connectedTo(host +&nbsp;\":\"&nbsp;+ port)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u914d\u7f6e Elasticsearch Java API Client (\u9ad8\u7ea7\u5ba2\u6237\u7aef)\u3002<br>&nbsp; &nbsp; &nbsp;* \u5f53\u9700\u8981\u6267\u884c Spring Data Elasticsearch \u4e0d\u76f4\u63a5\u652f\u6301\u7684\u590d\u6742\u67e5\u8be2\uff08\u5982 kNN\uff09\u65f6\u4f7f\u7528\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;ElasticsearchClient \u5b9e\u4f8b<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@Bean<br>&nbsp; &nbsp;&nbsp;public&nbsp;ElasticsearchClient&nbsp;elasticsearchClient()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 1. \u521b\u5efa\u4f4e\u7ea7 REST \u5ba2\u6237\u7aef<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; RestClient restClient = RestClient.builder(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;new&nbsp;HttpHost(host, port)).build();<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 2. \u4f7f\u7528 Jackson \u4f5c\u4e3a JSON \u6620\u5c04\u5668\u521b\u5efa\u4f20\u8f93\u5c42<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; ElasticsearchTransport transport =&nbsp;new&nbsp;RestClientTransport(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restClient,&nbsp;new&nbsp;JacksonJsonpMapper());<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 3. \u521b\u5efa\u5e76\u8fd4\u56de\u9ad8\u7ea7\u5ba2\u6237\u7aef<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;returnnew&nbsp;ElasticsearchClient(transport);<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u7528\u6237\u5b9e\u4f53\u7c7b<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.model;<br><br>import&nbsp;org.springframework.data.annotation.Id;<br>import&nbsp;org.springframework.data.elasticsearch.annotations.Document;&nbsp;<br>import&nbsp;org.springframework.data.elasticsearch.annotations.Field;&nbsp;<br>import&nbsp;org.springframework.data.elasticsearch.annotations.FieldType;&nbsp;<br><br>import&nbsp;java.util.List;<br><br>@Document(indexName =&nbsp;\"users\")<br>publicclass&nbsp;User&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;@Id<br>&nbsp; &nbsp;&nbsp;private&nbsp;String id;<br><br>&nbsp; &nbsp;&nbsp;@Field(type = FieldType.Text)&nbsp;<em>\/\/ \u6620\u5c04\u4e3a ES Text \u7c7b\u578b\uff0c\u9002\u5408\u5168\u6587\u641c\u7d22<\/em><br>&nbsp; &nbsp;&nbsp;private&nbsp;String username;<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u7528\u6237\u5174\u8da3\u5411\u91cf\u5b57\u6bb5\u3002<br>&nbsp; &nbsp; &nbsp;* \u5173\u952e\u6ce8\u89e3\uff1a<br>&nbsp; &nbsp; &nbsp;* - type = FieldType.Dense_Vector: \u6307\u5b9a\u4e3a\u5bc6\u96c6\u5411\u91cf\u7c7b\u578b\uff0cES \u7528\u4e8e\u5411\u91cf\u641c\u7d22\u3002<br>&nbsp; &nbsp; &nbsp;* - dims = 128: \u6307\u5b9a\u5411\u91cf\u7684\u7ef4\u5ea6\u3002\u5fc5\u987b\u4e0e\u5b9e\u9645\u751f\u6210\u7684\u5411\u91cf\u7ef4\u5ea6\u4e00\u81f4\u3002<br>&nbsp; &nbsp; &nbsp;* \u8fd9\u4e2a\u5b57\u6bb5\u5c06\u7528\u4e8e\u5b58\u50a8\u7528\u6237\u5174\u8da3\u7684\u6570\u503c\u5316\u8868\u793a\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@Field(type = FieldType.Dense_Vector, dims =&nbsp;128)<br>&nbsp; &nbsp;&nbsp;private&nbsp;List&lt;Float&gt; interestVector;<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;getId()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;id;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setId(String id)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.id = id;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;getUsername()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;username;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setUsername(String username)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.username = username;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Float&gt;&nbsp;getInterestVector()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;interestVector;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setInterestVector(List&lt;Float&gt; interestVector)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.interestVector = interestVector;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;@Override<br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;toString()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return\"User{\"&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\"id='\"&nbsp;+ id +&nbsp;'\\''&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\", username='\"&nbsp;+ username +&nbsp;'\\''&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\", interestVector=\"&nbsp;+ interestVector +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;'}';<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u89c6\u9891\u5b9e\u4f53\u7c7b<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.model;<br><br>import&nbsp;org.springframework.data.annotation.Id;<br>import&nbsp;org.springframework.data.elasticsearch.annotations.Document;<br>import&nbsp;org.springframework.data.elasticsearch.annotations.Field;<br>import&nbsp;org.springframework.data.elasticsearch.annotations.FieldType;<br><br>import&nbsp;java.util.List;<br><br>@Document(indexName =&nbsp;\"videos\")<br>publicclass&nbsp;Video&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;@Id<br>&nbsp; &nbsp;&nbsp;private&nbsp;String id;<br><br>&nbsp; &nbsp;&nbsp;@Field(type = FieldType.Text)&nbsp;<em>\/\/ \u89c6\u9891\u6807\u9898<\/em><br>&nbsp; &nbsp;&nbsp;private&nbsp;String title;<br><br>&nbsp; &nbsp;&nbsp;@Field(type = FieldType.Keyword)&nbsp;<em>\/\/ \u89c6\u9891\u5206\u7c7b\uff0cKeyword \u7c7b\u578b\u9002\u5408\u7cbe\u786e\u5339\u914d\u548c\u805a\u5408<\/em><br>&nbsp; &nbsp;&nbsp;private&nbsp;String category;<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u89c6\u9891\u5185\u5bb9\u7279\u5f81\u5411\u91cf\u5b57\u6bb5\u3002<br>&nbsp; &nbsp; &nbsp;* \u540c\u6837\u4f7f\u7528 Dense_Vector \u7c7b\u578b\u3002<br>&nbsp; &nbsp; &nbsp;* \u91cd\u8981\uff1a\u7ef4\u5ea6\u5fc5\u987b\u4e0e User \u7684 interestVector \u7ef4\u5ea6\u76f8\u540c\uff0c\u624d\u80fd\u8fdb\u884c\u5411\u91cf\u76f8\u4f3c\u5ea6\u8ba1\u7b97\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@Field(type = FieldType.Dense_Vector, dims =&nbsp;128)<br>&nbsp; &nbsp;&nbsp;private&nbsp;List&lt;Float&gt; contentVector;<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;getId()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;id;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setId(String id)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.id = id;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;getTitle()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;title;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setTitle(String title)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.title = title;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;getCategory()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;category;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setCategory(String category)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.category = category;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Float&gt;&nbsp;getContentVector()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;contentVector;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;void&nbsp;setContentVector(List&lt;Float&gt; contentVector)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.contentVector = contentVector;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;@Override<br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;toString()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return\"Video{\"&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\"id='\"&nbsp;+ id +&nbsp;'\\''&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\", title='\"&nbsp;+ title +&nbsp;'\\''&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\", category='\"&nbsp;+ category +&nbsp;'\\''&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\", contentVector=\"&nbsp;+ contentVector +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;'}';<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u5411\u91cf\u670d\u52a1\u7c7b<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.service;<br><br>import&nbsp;org.springframework.stereotype.Service;<br><br>import&nbsp;java.util.List;<br>import&nbsp;java.util.Random;<br>import&nbsp;java.util.stream.Collectors;<br>import&nbsp;java.util.stream.DoubleStream;<br><br>@Service<br>publicclass&nbsp;VectorService&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;<em>\/\/ \u5b9a\u4e49\u5411\u91cf\u7ef4\u5ea6\uff0c\u5fc5\u987b\u4e0e\u6a21\u578b\u8f93\u51fa\u548c ES \u5b57\u6bb5\u5b9a\u4e49\u4e00\u81f4<\/em><br>&nbsp; &nbsp;&nbsp;privatestaticfinalint&nbsp;VECTOR_DIMENSIONS =&nbsp;128;<br>&nbsp; &nbsp;&nbsp;<em>\/\/ \u7528\u4e8e\u751f\u6210\u6a21\u62df\u5411\u91cf\u7684\u968f\u673a\u6570\u751f\u6210\u5668<\/em><br>&nbsp; &nbsp;&nbsp;privatefinal&nbsp;Random random =&nbsp;new&nbsp;Random();<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u751f\u6210\u7528\u6237\u5174\u8da3\u5411\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;* \u5728\u771f\u5b9e\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u4e2a\u5411\u91cf\u5e94\u8be5\u7531\u673a\u5668\u5b66\u4e60\u6a21\u578b\u6839\u636e\u7528\u6237\u7684\u5386\u53f2\u884c\u4e3a\uff08\u89c2\u770b\u3001\u70b9\u8d5e\u3001\u8bc4\u8bba\u7b49\uff09<br>&nbsp; &nbsp; &nbsp;* \u52a8\u6001\u8ba1\u7b97\u5f97\u51fa\u3002\u8fd9\u91cc\u4f7f\u7528\u968f\u673a\u6570\u6a21\u62df\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;\u4ee3\u8868\u7528\u6237\u5174\u8da3\u7684\u5411\u91cf List&lt;Float&gt;<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Float&gt;&nbsp;generateUserInterestVector()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ \u751f\u6210 128 \u4e2a -1.0 \u5230 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u53cc\u7cbe\u5ea6\u6570\uff0c\u5e76\u8f6c\u6362\u4e3a Float List<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;random.doubles(VECTOR_DIMENSIONS, -1.0,&nbsp;1.0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.boxed()&nbsp;<em>\/\/ \u5c06 double \u8f6c\u4e3a Double<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(Double::floatValue)&nbsp;<em>\/\/ \u5c06 Double \u8f6c\u4e3a Float<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());&nbsp;<em>\/\/ \u6536\u96c6\u5230 List<\/em><br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u751f\u6210\u89c6\u9891\u5185\u5bb9\u7279\u5f81\u5411\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;* \u5728\u771f\u5b9e\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u4e2a\u5411\u91cf\u5e94\u7531\u5185\u5bb9\u7406\u89e3\u6a21\u578b\u6839\u636e\u89c6\u9891\u7684\u5143\u6570\u636e\u3001\u89c6\u89c9\u3001\u97f3\u9891\u7279\u5f81\u7b49<br>&nbsp; &nbsp; &nbsp;* \u8ba1\u7b97\u5f97\u51fa\u3002\u8fd9\u91cc\u540c\u6837\u4f7f\u7528\u968f\u673a\u6570\u6a21\u62df\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;\u4ee3\u8868\u89c6\u9891\u5185\u5bb9\u7684\u5411\u91cf List&lt;Float&gt;<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Float&gt;&nbsp;generateVideoContentVector()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;random.doubles(VECTOR_DIMENSIONS, -1.0,&nbsp;1.0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.boxed()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(Double::floatValue)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u63a8\u8350\u670d\u52a1\u7c7b<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.service;<br><br>import&nbsp;co.elastic.clients.elasticsearch.ElasticsearchClient;<br>import&nbsp;co.elastic.clients.elasticsearch.core.SearchRequest;<br>import&nbsp;co.elastic.clients.elasticsearch.core.SearchResponse;<br>import&nbsp;co.elastic.clients.elasticsearch.core.search.Hit;<br>import&nbsp;com.example.model.Video;<br>import&nbsp;org.springframework.beans.factory.annotation.Autowired;<br>import&nbsp;org.springframework.data.elasticsearch.client.elc.NativeQuery;<br>import&nbsp;org.springframework.data.elasticsearch.core.ElasticsearchOperations;<br>import&nbsp;org.springframework.data.elasticsearch.core.SearchHit;<br>import&nbsp;org.springframework.data.elasticsearch.core.SearchHits;<br>import&nbsp;org.springframework.stereotype.Service;<br><br>import&nbsp;java.io.IOException;<br>import&nbsp;java.util.ArrayList;<br>import&nbsp;java.util.HashMap;<br>import&nbsp;java.util.List;<br>import&nbsp;java.util.Map;<br>import&nbsp;java.util.stream.Collectors;<br><br>@Service<br>publicclass&nbsp;RecommendationService&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;<em>\/\/ \u6ce8\u5165 Elasticsearch Java API Client\uff0c\u7528\u4e8e\u6267\u884c kNN \u7b49\u9ad8\u7ea7\u67e5\u8be2<\/em><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;ElasticsearchClient elasticsearchClient;<br><br>&nbsp; &nbsp;&nbsp;<em>\/\/ \u6ce8\u5165 Spring Data Elasticsearch \u64cd\u4f5c\u6a21\u677f\uff0c\u7528\u4e8e\u66f4\u4fbf\u6377\u7684\u6570\u636e\u64cd\u4f5c\u548c\u90e8\u5206\u67e5\u8be2<\/em><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;ElasticsearchOperations elasticsearchOperations;<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \u4f7f\u7528 Elasticsearch \u7684 k-Nearest Neighbor (kNN) \u641c\u7d22\u529f\u80fd<br>&nbsp; &nbsp; &nbsp;* \u6765\u67e5\u627e\u4e0e\u7528\u6237\u5174\u8da3\u5411\u91cf\u6700\u76f8\u4f3c\u7684\u89c6\u9891\u3002<br>&nbsp; &nbsp; &nbsp;* \u8fd9\u662f Elasticsearch 8.x+ \u63a8\u8350\u7684\u9ad8\u6548\u5411\u91cf\u76f8\u4f3c\u5ea6\u641c\u7d22\u65b9\u5f0f\u3002<br>&nbsp; &nbsp; &nbsp;*<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;userInterestVector \u7528\u6237\u5f53\u524d\u7684\u5174\u8da3\u5411\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;k &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u9700\u8981\u8fd4\u56de\u7684\u63a8\u8350\u89c6\u9891\u6570\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;\u63a8\u8350\u7684\u89c6\u9891\u5217\u8868\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Video&gt;&nbsp;recommendVideosKnn(List&lt;Float&gt; userInterestVector,&nbsp;int&nbsp;k)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 1. \u6784\u5efa kNN \u641c\u7d22\u8bf7\u6c42<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SearchRequest searchRequest = SearchRequest.of(s -&gt; s<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .index(\"videos\")&nbsp;<em>\/\/ \u6307\u5b9a\u8981\u5728\u54ea\u4e2a\u7d22\u5f15\u4e2d\u641c\u7d22<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .size(k)&nbsp;<em>\/\/ \u8bbe\u7f6e\u8fd4\u56de\u7ed3\u679c\u7684\u6570\u91cf<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .knn(knn -&gt; knn&nbsp;<em>\/\/ \u914d\u7f6e kNN \u67e5\u8be2\u53c2\u6570<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .field(\"contentVector\")&nbsp;<em>\/\/ \u6307\u5b9a\u8fdb\u884c kNN \u641c\u7d22\u7684\u5411\u91cf\u5b57\u6bb5\uff08\u89c6\u9891\u7684\u7279\u5f81\u5411\u91cf\uff09<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .queryVector(userInterestVector)&nbsp;<em>\/\/ \u63d0\u4f9b\u67e5\u8be2\u5411\u91cf\uff08\u7528\u6237\u7684\u5174\u8da3\u5411\u91cf\uff09<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .k(k)&nbsp;<em>\/\/ \u6307\u5b9a\u8981\u8fd4\u56de\u7684\u6700\u90bb\u8fd1\u5411\u91cf\u6570\u91cf<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .numCandidates(k *&nbsp;10)&nbsp;<em>\/\/ \u641c\u7d22\u7684\u5019\u9009\u96c6\u5927\u5c0f\uff0c\u901a\u5e38\u5927\u4e8e k \u4ee5\u63d0\u9ad8\u7cbe\u5ea6\uff0c\u4f46\u4f1a\u5f71\u54cd\u6027\u80fd<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 2. \u6267\u884c\u641c\u7d22\u8bf7\u6c42\uff0c\u5e76\u6307\u5b9a\u8fd4\u56de\u7684\u6587\u6863\u7c7b\u578b\u4e3a Video.class<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SearchResponse&lt;Video&gt; response = elasticsearchClient.search(searchRequest, Video.class);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 3. \u5904\u7406\u54cd\u5e94\u7ed3\u679c\uff0c\u63d0\u53d6 Video \u5bf9\u8c61<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;response.hits().hits().stream()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Hit::source)&nbsp;<em>\/\/ \u83b7\u53d6\u6bcf\u4e2a\u547d\u4e2d\u6587\u6863\u7684 source (\u5373 Video \u5bf9\u8c61)<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(video -&gt; video !=&nbsp;null)&nbsp;<em>\/\/ \u8fc7\u6ee4\u6389 null \u503c<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp;<em>\/\/ \u6536\u96c6\u5230 List<\/em><br><br>&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;catch&nbsp;(IOException e) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u4f7f\u7528\u65e5\u5fd7\u6846\u67b6\uff08\u5982 SLF4J\uff09\u8bb0\u5f55\u9519\u8bef<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.err.println(\"\u6267\u884c kNN \u641c\u7d22\u65f6\u53d1\u751f\u9519\u8bef: \"&nbsp;+ e.getMessage());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;returnnew&nbsp;ArrayList&lt;&gt;();&nbsp;<em>\/\/ \u53d1\u751f\u9519\u8bef\u65f6\u8fd4\u56de\u7a7a\u5217\u8868<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* \uff08\u5907\u9009\u65b9\u6848\uff09\u4f7f\u7528 script_score \u67e5\u8be2\u7ed3\u5408\u4f59\u5f26\u76f8\u4f3c\u5ea6\u811a\u672c<br>&nbsp; &nbsp; &nbsp;* \u6765\u8ba1\u7b97\u5411\u91cf\u76f8\u4f3c\u5ea6\u5e76\u8fdb\u884c\u6392\u5e8f\u3002<br>&nbsp; &nbsp; &nbsp;* \u6b64\u65b9\u6cd5\u66f4\u7075\u6d3b\uff0c\u4f46\u901a\u5e38\u6bd4 kNN \u641c\u7d22\u6162\uff0c\u5c24\u5176\u5728\u5927\u6570\u636e\u96c6\u4e0a\u3002<br>&nbsp; &nbsp; &nbsp;*<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;userInterestVector \u7528\u6237\u5f53\u524d\u7684\u5174\u8da3\u5411\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;k &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u9700\u8981\u8fd4\u56de\u7684\u63a8\u8350\u89c6\u9891\u6570\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;\u63a8\u8350\u7684\u89c6\u9891\u5217\u8868\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Video&gt;&nbsp;recommendVideosScriptScore(List&lt;Float&gt; userInterestVector,&nbsp;int&nbsp;k)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;try&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 1. \u6784\u5efa\u7528\u4e8e\u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\u7684 Painless \u811a\u672c<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 'cosineSimilarity' \u662f Elasticsearch \u63d0\u4f9b\u7684\u5185\u7f6e\u51fd\u6570<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 'params.query_vector' \u662f\u6211\u4eec\u4f20\u5165\u7684\u67e5\u8be2\u5411\u91cf<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 'contentVector' \u662f\u6587\u6863\u4e2d\u7684\u5411\u91cf\u5b57\u6bb5<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String scriptSource =&nbsp;\"cosineSimilarity(params.query_vector, 'contentVector') + 1.0\";<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ +1.0 \u662f\u4e3a\u4e86\u5c06\u8303\u56f4\u4ece &#91;-1, 1] \u8f6c\u6362\u4e3a &#91;0, 2]\uff0c\u786e\u4fdd\u5206\u6570\u4e3a\u6b63<\/em><br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 2. \u51c6\u5907\u811a\u672c\u53c2\u6570<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String, Object&gt; scriptParams =&nbsp;new&nbsp;HashMap&lt;&gt;();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scriptParams.put(\"query_vector\", userInterestVector);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 3. \u6784\u5efa script_score \u67e5\u8be2<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;co.elastic.clients.elasticsearch._types.query_dsl.Query query = co.elastic.clients.elasticsearch._types.query_dsl.Query.of(q -&gt; q<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.scriptScore(ss -&gt; ss<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.query(q2 -&gt; q2.matchAll(m -&gt; m))&nbsp;<em>\/\/ \u5bf9\u6240\u6709\u6587\u6863\u8fdb\u884c\u8bc4\u5206<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.script(s -&gt; s<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.inline(i -&gt; i<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.source(scriptSource)&nbsp;<em>\/\/ \u5185\u8054\u811a\u672c\u6e90\u7801<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.params(scriptParams)&nbsp;<em>\/\/ \u811a\u672c\u53c2\u6570<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 4. \u4f7f\u7528 Spring Data Elasticsearch \u7684 NativeQuery \u5305\u88c5\u67e5\u8be2<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeQuery nativeQuery = NativeQuery.builder()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.withQuery(query)&nbsp;<em>\/\/ \u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.withPageable(org.springframework.data.domain.PageRequest.of(0, k))&nbsp;<em>\/\/ \u8bbe\u7f6e\u5206\u9875<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.build();<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 5. \u6267\u884c\u641c\u7d22<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SearchHits&lt;Video&gt; searchHits = elasticsearchOperations.search(nativeQuery, Video.class);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em>\/\/ 6. \u5904\u7406\u7ed3\u679c<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return&nbsp;searchHits.getSearchHits().stream()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(SearchHit::getContent)&nbsp;<em>\/\/ \u83b7\u53d6\u5185\u5bb9 (Video \u5bf9\u8c61)<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());<br><br>&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;catch&nbsp;(Exception e) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.err.println(\"\u6267\u884c script_score \u641c\u7d22\u65f6\u53d1\u751f\u9519\u8bef: \"&nbsp;+ e.getMessage());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;returnnew&nbsp;ArrayList&lt;&gt;();<br>&nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Recommendation Controller<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.controller;<br><br>import&nbsp;com.example.model.Video;<br>import&nbsp;com.example.service.RecommendationService;<br>import&nbsp;com.example.service.VectorService;<br>import&nbsp;org.springframework.beans.factory.annotation.Autowired;<br>import&nbsp;org.springframework.web.bind.annotation.*;<br><br>import&nbsp;java.util.List;<br><br>@RestController<br>@RequestMapping(\"\/api\/recommendations\")<br>publicclass&nbsp;RecommendationController&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;RecommendationService recommendationService;&nbsp;<em>\/\/ \u6ce8\u5165\u63a8\u8350\u670d\u52a1<\/em><br><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;VectorService vectorService;&nbsp;<em>\/\/ \u6ce8\u5165\u5411\u91cf\u670d\u52a1<\/em><br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* GET \/api\/recommendations\/user\/{userId}<br>&nbsp; &nbsp; &nbsp;* \u4e3a\u6307\u5b9a\u7528\u6237\u83b7\u53d6\u89c6\u9891\u63a8\u8350\u3002<br>&nbsp; &nbsp; &nbsp;*<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;userId \u7528\u6237ID\uff0c\u4ece\u8def\u5f84\u53d8\u91cf\u83b7\u53d6\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;k &nbsp; &nbsp; &nbsp;\u63a8\u8350\u6570\u91cf\uff0c\u4ece\u67e5\u8be2\u53c2\u6570\u83b7\u53d6\uff0c\u9ed8\u8ba4\u4e3a 5\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;\u63a8\u8350\u7684\u89c6\u9891\u5217\u8868\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@GetMapping(\"\/user\/{userId}\")<br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Video&gt;&nbsp;getRecommendationsForUser(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @PathVariable String userId, \/\/ \u8def\u5f84\u53d8\u91cf {userId}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(defaultValue =&nbsp;\"5\")&nbsp;int&nbsp;k)&nbsp;{&nbsp;<em>\/\/ \u67e5\u8be2\u53c2\u6570 ?k=...<\/em><br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ --- \u6a21\u62df\u83b7\u53d6\u7528\u6237\u5411\u91cf ---<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ \u5728\u771f\u5b9e\u5e94\u7528\u4e2d\uff0c\u8fd9\u91cc\u5e94\u8be5\u662f\uff1a<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ Optional&lt;User&gt; userOpt = userRepository.findById(userId);<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ if (userOpt.isPresent()) {<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ &nbsp; &nbsp; List&lt;Float&gt; userVector = userOpt.get().getInterestVector();<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ } else { ... \u5904\u7406\u7528\u6237\u672a\u627e\u5230 ... }<\/em><br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ --- \u6f14\u793a\uff1a\u4e3a\u7528\u6237\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684\u5174\u8da3\u5411\u91cf ---<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; List&lt;Float&gt; userInterestVector = vectorService.generateUserInterestVector();<br>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"\u4e3a\u7528\u6237 \"&nbsp;+ userId +&nbsp;\" \u751f\u6210\u7684\u5174\u8da3\u5411\u91cf (\u524d5\u7ef4): \"&nbsp;+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userInterestVector.subList(0, Math.min(5, userInterestVector.size())) +&nbsp;\"...\");<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ \u8c03\u7528\u63a8\u8350\u670d\u52a1\u7684 kNN \u65b9\u6cd5\u83b7\u53d6\u63a8\u8350<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;recommendationService.recommendVideosKnn(userInterestVector, k);<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* POST \/api\/recommendations\/vector<br>&nbsp; &nbsp; &nbsp;* \u76f4\u63a5\u4f7f\u7528\u63d0\u4f9b\u7684\u5411\u91cf\u83b7\u53d6\u63a8\u8350\u3002\u9002\u7528\u4e8e\u6d4b\u8bd5\u6216\u5ba2\u6237\u7aef\u5df2\u751f\u6210\u5411\u91cf\u7684\u573a\u666f\u3002<br>&nbsp; &nbsp; &nbsp;*<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;vector \u8bf7\u6c42\u4f53\u4e2d\u7684\u5411\u91cf\u6570\u636e\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@param&nbsp;k &nbsp; &nbsp; &nbsp;\u63a8\u8350\u6570\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;*&nbsp;@return&nbsp;\u63a8\u8350\u7684\u89c6\u9891\u5217\u8868\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@PostMapping(\"\/vector\")<br>&nbsp; &nbsp;&nbsp;public&nbsp;List&lt;Video&gt;&nbsp;getRecommendationsByVector(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestBody List&lt;Float&gt; vector, \/\/ \u4ece\u8bf7\u6c42\u4f53\u8bfb\u53d6\u5411\u91cf<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(defaultValue =&nbsp;\"5\")&nbsp;int&nbsp;k)&nbsp;{<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ \u76f4\u63a5\u4f7f\u7528\u4f20\u5165\u7684\u5411\u91cf\u8fdb\u884c\u63a8\u8350<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;recommendationService.recommendVideosKnn(vector, k);<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u63d0\u4f9b\u7528\u4e8e\u586b\u5145\u6d4b\u8bd5\u6570\u636e\u7684 API \u63a5\u53e3<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.controller;<br><br>import&nbsp;com.example.model.User;<br>import&nbsp;com.example.model.Video;<br>import&nbsp;com.example.repository.UserRepository;<br>import&nbsp;com.example.repository.VideoRepository;<br>import&nbsp;com.example.service.VectorService;<br>import&nbsp;org.springframework.beans.factory.annotation.Autowired;<br>import&nbsp;org.springframework.web.bind.annotation.*;<br><br>import&nbsp;java.util.stream.IntStream;<br><br>@RestController<br>@RequestMapping(\"\/api\/data\")<br>publicclass&nbsp;DataManagementController&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;UserRepository userRepository;&nbsp;<em>\/\/ \u7528\u6237\u6570\u636e\u8bbf\u95ee<\/em><br><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;VideoRepository videoRepository;&nbsp;<em>\/\/ \u89c6\u9891\u6570\u636e\u8bbf\u95ee<\/em><br><br>&nbsp; &nbsp;&nbsp;@Autowired<br>&nbsp; &nbsp;&nbsp;private&nbsp;VectorService vectorService;&nbsp;<em>\/\/ \u5411\u91cf\u670d\u52a1<\/em><br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* POST \/api\/data\/populate<br>&nbsp; &nbsp; &nbsp;* \u586b\u5145 Elasticsearch \u7d22\u5f15\u7684\u7aef\u70b9\u3002<br>&nbsp; &nbsp; &nbsp;* \u521b\u5efa 100 \u4e2a\u7528\u6237\u548c 1000 \u4e2a\u89c6\u9891\uff0c\u5e76\u4e3a\u5b83\u4eec\u751f\u6210\u968f\u673a\u5411\u91cf\u3002<br>&nbsp; &nbsp; &nbsp;* \u4e3b\u8981\u7528\u4e8e\u6d4b\u8bd5\u548c\u6f14\u793a\u76ee\u7684\u3002<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@PostMapping(\"\/populate\")<br>&nbsp; &nbsp;&nbsp;public&nbsp;String&nbsp;populateData()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 1. \u6e05\u7a7a\u73b0\u6709\u6570\u636e\uff08\u53ef\u9009\uff0c\u4e3a\u4e86\u6f14\u793a\u5e72\u51c0\uff09<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; userRepository.deleteAll();<br>&nbsp; &nbsp; &nbsp; &nbsp; videoRepository.deleteAll();<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 2. \u521b\u5efa\u5e76\u4fdd\u5b58 100 \u4e2a\u7528\u6237<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; IntStream.range(1,&nbsp;101).forEach(i -&gt; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User user =&nbsp;new&nbsp;User();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.setId(\"user_\"&nbsp;+ i);&nbsp;<em>\/\/ \u8bbe\u7f6e\u7528\u6237 ID<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.setUsername(\"User\"&nbsp;+ i);&nbsp;<em>\/\/ \u8bbe\u7f6e\u7528\u6237\u540d<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.setInterestVector(vectorService.generateUserInterestVector());&nbsp;<em>\/\/ \u8bbe\u7f6e\u5174\u8da3\u5411\u91cf<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userRepository.save(user);&nbsp;<em>\/\/ \u4fdd\u5b58\u5230 ES<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; });<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ 3. \u521b\u5efa\u5e76\u4fdd\u5b58 1000 \u4e2a\u89c6\u9891<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; IntStream.range(1,&nbsp;1001).forEach(i -&gt; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Video video =&nbsp;new&nbsp;Video();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; video.setId(\"video_\"&nbsp;+ i);&nbsp;<em>\/\/ \u8bbe\u7f6e\u89c6\u9891 ID<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; video.setTitle(\"Video Title \"&nbsp;+ i);&nbsp;<em>\/\/ \u8bbe\u7f6e\u6807\u9898<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; video.setCategory(\"Category \"&nbsp;+ (i %&nbsp;10));&nbsp;<em>\/\/ \u8bbe\u7f6e\u5206\u7c7b (10\u4e2a\u7c7b\u522b)<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; video.setContentVector(vectorService.generateVideoContentVector());&nbsp;<em>\/\/ \u8bbe\u7f6e\u5185\u5bb9\u5411\u91cf<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; videoRepository.save(video);&nbsp;<em>\/\/ \u4fdd\u5b58\u5230 ES<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; });<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return\"\u6210\u529f\u586b\u5145\u6570\u636e\uff1a\u521b\u5efa\u4e86 100 \u4e2a\u7528\u6237\u548c 1000 \u4e2a\u89c6\u9891\u3002\";<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* GET \/api\/data\/users<br>&nbsp; &nbsp; &nbsp;* \u83b7\u53d6\u6240\u6709\u7528\u6237\u6570\u636e<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@GetMapping(\"\/users\")<br>&nbsp; &nbsp;&nbsp;public&nbsp;Iterable&lt;User&gt;&nbsp;getAllUsers()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;userRepository.findAll();<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;<em>\/**<br>&nbsp; &nbsp; &nbsp;* GET \/api\/data\/videos<br>&nbsp; &nbsp; &nbsp;* \u83b7\u53d6\u6240\u6709\u89c6\u9891\u6570\u636e<br>&nbsp; &nbsp; &nbsp;*\/<\/em><br>&nbsp; &nbsp;&nbsp;@GetMapping(\"\/videos\")<br>&nbsp; &nbsp;&nbsp;public&nbsp;Iterable&lt;Video&gt;&nbsp;getAllVideos()&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;videoRepository.findAll();<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">User Repository<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.repository;<br><br>import&nbsp;com.example.model.User;<br>import&nbsp;org.springframework.data.elasticsearch.repository.ElasticsearchRepository;<br>public&nbsp;interface&nbsp;UserRepository&nbsp;extends&nbsp;ElasticsearchRepository&lt;User,&nbsp;String&gt;&nbsp;{<br><br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Video Repository<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example.repository;<br><br>import&nbsp;com.example.model.Video;<br>import&nbsp;org.springframework.data.elasticsearch.repository.ElasticsearchRepository;<br><br>public&nbsp;interface&nbsp;VideoRepository&nbsp;extends&nbsp;ElasticsearchRepository&lt;Video,&nbsp;String&gt;&nbsp;{<br><br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Application<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>package&nbsp;com.example;<br><br>import&nbsp;org.springframework.boot.SpringApplication;<br>import&nbsp;org.springframework.boot.autoconfigure.SpringBootApplication;<br><br>@SpringBootApplication<br>public&nbsp;class&nbsp;Application&nbsp;{<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String&#91;] args)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(Application.class,&nbsp;args);<br>&nbsp; &nbsp; }<br>}<\/code><\/pre>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u76f8\u6bd4\u4f20\u7edf\u7684\u57fa\u4e8e\u5173\u952e\u8bcd\u5339\u914d\u7684\u641c\u7d22\uff0c\u5411\u91cf\u641c\u7d22\u80fd\u6355\u6349\u5230\u66f4\u6df1\u5c42\u6b21\u7684\u8bed\u4e49\u76f8\u4f3c\u6027\u3002\u5373\u4f7f\u4e24\u4e2a\u89c6\u9891\u6807\u9898\u6ca1\u6709\u5171\u540c\u8bcd\u6c47\uff0c\u4f46\u5982\u679c\u5185\u5bb9\u76f8\u4f3c\uff0c\u5b83\u4eec\u7684\u5411\u91cf\u4e5f\u4f1a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-6725","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"post_thumbnail_image":"","content_first_image":null,"post_medium_image_300":"","post_thumbnail_image_624":"","post_frist_image":null,"post_medium_image":"","post_large_image":"","post_full_image":"","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Spring Boot","post_date":"2025-09-29","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":188,"next_post_id":6736,"next_post_title":"\u57fa\u4e8e Java \u4e0e YOLOv8 \u5b9e\u73b0\u8868\u683c\u56fe\u50cf\u884c\u5217\u68c0\u6d4b\u7684\u5b8c\u6574\u5b9e\u8df5","previous_post_id":6711,"previous_post_title":"Spring Boot \u5b9e\u73b0 SNMP \u5e94\u7528\u3001\u53d1\u9001\u4e0e\u89e3\u6790","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6725"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6725\/revisions"}],"predecessor-version":[{"id":6726,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6725\/revisions\/6726"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}