1"use strict";(self.webpackChunkspring_ai_alibaba_website=self.webpackChunkspring_ai_alibaba_website||[]).push([[3749],{26835:(e,r,n)=>{n.r(r),n.d(r,{assets:()=>d,contentTitle:()=>o,default:()=>h,frontMatter:()=>c,metadata:()=>t,toc:()=>a});const t=JSON.parse('{"id":"rag/vectordbs/pgvector","title":"PGvector","description":"\u672c\u8282\u5c06\u6307\u5bfc\u60a8\u8bbe\u7f6e PGvector VectorStore \u6765\u5b58\u50a8\u6587\u6863\u5d4c\u5165\u5e76\u6267\u884c\u76f8\u4f3c\u6027\u641c\u7d22\u3002","source":"@site/integration/rag/vectordbs/pgvector.md","sourceDirName":"rag/vectordbs","slug":"/rag/vectordbs/pgvector","permalink":"/integration/rag/vectordbs/pgvector","draft":false,"unlisted":false,"editUrl":"https://github.com/spring-ai-alibaba/website/tree/main/integration/rag/vectordbs/pgvector.md","tags":[],"version":"current","lastUpdatedBy":"Ken Liu","lastUpdatedAt":1768271906000,"frontMatter":{},"sidebar":"integrationSidebar","previous":{"title":"Oracle Database 23ai - AI Vector Search","permalink":"/integration/rag/vectordbs/oracle"},"next":{"title":"Redis","permalink":"/integration/rag/vectordbs/redis"}}');var s=n(74848),i=n(28453);const c={},o="PGvector",d={},a=[{value:"Prerequisites",id:"prerequisites",level:2},{value:"Auto-Configuration",id:"auto-configuration",level:2},{value:"Configuration properties",id:"pgvector-properties",level:2},{value:"Metadata filtering",id:"metadata-filtering",level:2},{value:"Manual Configuration",id:"manual-configuration",level:2},{value:"Run Postgres & PGVector DB locally",id:"run-postgres--pgvector-db-locally",level:2},{value:"Accessing the Native Client",id:"accessing-the-native-client",level:2}];function l(e){const r={a:"a",blockquote:"blockquote",code:"code",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(r.header,{children:(0,s.jsx)(r.h1,{id:"pgvector",children:"PGvector"})}),"\n",(0,s.jsxs)(r.p,{children:["\u672c\u8282\u5c06\u6307\u5bfc\u60a8\u8bbe\u7f6e PGvector ",(0,s.jsx)(r.code,{children:"VectorStore"})," \u6765\u5b58\u50a8\u6587\u6863\u5d4c\u5165\u5e76\u6267\u884c\u76f8\u4f3c\u6027\u641c\u7d22\u3002"]}),"\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.a,{href:"https://github.com/pgvector/pgvector",children:"PGvector"})," \u662f PostgreSQL \u7684\u5f00\u6e90\u6269\u5c55\uff0c\u652f\u6301\u5b58\u50a8\u548c\u641c\u7d22\u673a\u5668\u5b66\u4e60\u751f\u6210\u7684\u5d4c\u5165\u3002\u5b83\u63d0\u4f9b\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u8ba9\u7528\u6237\u8bc6\u522b\u7cbe\u786e\u548c\u8fd1\u4f3c\u7684\u6700\u8fd1\u90bb\u3002\u5b83\u8bbe\u8ba1\u4e3a\u4e0e\u5176\u4ed6 PostgreSQL \u529f\u80fd\uff08\u5305\u62ec\u7d22\u5f15\u548c\u67e5\u8be2\uff09\u65e0\u7f1d\u534f\u4f5c\u3002"]}),"\n",(0,s.jsx)(r.h2,{id:"prerequisites",children:"Prerequisites"}),"\n",(0,s.jsxs)(r.p,{children:["\u9996\u5148\uff0c\u60a8\u9700\u8981\u8bbf\u95ee\u542f\u7528\u4e86 ",(0,s.jsx)(r.code,{children:"vector"}),"\u3001",(0,s.jsx)(r.code,{children:"hstore"})," \u548c ",(0,s.jsx)(r.code,{children:"uuid-ossp"})," \u6269\u5c55\u7684 PostgreSQL \u5b9e\u4f8b\u3002"]}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"})," \u60a8\u53ef\u4ee5\u901a\u8fc7 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.docker-compose",children:"Docker Compose"})," \u6216 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.testcontainers",children:"Testcontainers"})," \u5c06 PGvector \u6570\u636e\u5e93\u4f5c\u4e3a Spring Boot dev service \u8fd0\u884c\u3002\u6216\u8005\uff0c\u3010Run Postgres & PGVector DB locally,setup local Postgres/PGVector\u3011 \u9644\u5f55\u663e\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Docker \u5bb9\u5668\u5728\u672c\u5730\u8bbe\u7f6e\u6570\u636e\u5e93\u3002"]}),"\n"]}),"\n",(0,s.jsxs)(r.p,{children:["\u5728\u542f\u52a8\u65f6\uff0c\u5982\u679c\u660e\u786e\u542f\u7528\u4e86 schema \u521d\u59cb\u5316\u529f\u80fd\uff0c",(0,s.jsx)(r.code,{children:"PgVectorStore"})," \u5c06\u5c1d\u8bd5\u5b89\u88c5\u6240\u9700\u7684\u6570\u636e\u5e93\u6269\u5c55\uff0c\u5e76\u5728\u4e0d\u5b58\u5728\u65f6\u521b\u5efa\u6240\u9700\u7684\u5e26\u7d22\u5f15\u7684 ",(0,s.jsx)(r.code,{children:"vector_store"})," \u8868\u3002"]}),"\n",(0,s.jsx)(r.p,{children:"\u6216\u8005\uff0c\u60a8\u53ef\u4ee5\u624b\u52a8\u
16267\u884c\u6b64\u64cd\u4f5c\uff1a"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-sql",children:'CREATE EXTENSION IF NOT EXISTS vector;\nCREATE EXTENSION IF NOT EXISTS hstore;\nCREATE EXTENSION IF NOT EXISTS "uuid-ossp";\n\nCREATE TABLE IF NOT EXISTS vector_store (\n\tid uuid DEFAULT uuid_generate_v4() PRIMARY KEY,\n\tcontent text,\n\tmetadata json,\n\tembedding vector(1536) // 1536 \u662f\u9ed8\u8ba4\u7684\u5d4c\u5165\u7ef4\u5ea6\n);\n\nCREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops);\n'})}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"})," \u5982\u679c\u60a8\u4f7f\u7528\u4e0d\u540c\u7684\u7ef4\u5ea6\uff0c\u8bf7\u5c06 ",(0,s.jsx)(r.code,{children:"1536"})," \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u5d4c\u5165\u7ef4\u5ea6\u3002PGvector \u5bf9 HNSW \u7d22\u5f15\u6700\u591a\u652f\u6301 2000 \u4e2a\u7ef4\u5ea6\u3002"]}),"\n"]}),"\n",(0,s.jsxs)(r.p,{children:["\u63a5\u4e0b\u6765\uff0c\u5982\u679c\u9700\u8981\uff0c\u4e3a ",(0,s.jsx)(r.a,{href:"../embeddings#available-implementations",children:"EmbeddingModel"})," \u63d0\u4f9b\u4e00\u4e2a API key\uff0c\u7528\u4e8e\u751f\u6210 ",(0,s.jsx)(r.code,{children:"PgVectorStore"})," \u5b58\u50a8\u7684\u5d4c\u5165\u3002"]}),"\n",(0,s.jsx)(r.h2,{id:"auto-configuration",children:"Auto-Configuration"}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u6ce8\u610f\uff1a"}),"\nSpring AI auto-configuration\u3001starter modules \u7684 artifact \u540d\u79f0\u53d1\u751f\u4e86\u91cd\u5927\u53d8\u5316\u3002\n\u8bf7\u53c2\u9605 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-ai/reference/upgrade-notes.html",children:"upgrade notes"})," \u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u3002"]}),"\n"]}),"\n",(0,s.jsx)(r.p,{children:"\u7136\u540e\u5c06 PgVectorStore boot starter \u4f9d\u8d56\u9879\u6dfb\u52a0\u5230\u60a8\u7684\u9879\u76ee\uff1a"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-xml",children:"<dependency>\n\t<groupId>org.springframework.ai</groupId>\n\t<artifactId>spring-ai-starter-vector-store-pgvector</artifactId>\n</dependency>\n"})}),"\n",(0,s.jsxs)(r.p,{children:["\u6216\u6dfb\u52a0\u5230\u60a8\u7684 Gradle ",(0,s.jsx)(r.code,{children:"build.gradle"})," \u6784\u5efa\u6587\u4ef6\u3002"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-groovy",children:"dependencies {\n implementation 'org.springframework.ai:spring-ai-starter-vector-store-pgvector'\n}\n"})}),"\n",(0,s.jsxs)(r.p,{children:["\u5411\u91cf\u5b58\u50a8\u5b9e\u73b0\u53ef\u4ee5\u4e3a\u60a8\u521d\u59cb\u5316\u6240\u9700\u7684 schema\uff0c\u4f46\u60a8\u5fc5\u987b\u901a\u8fc7\u5728\u9002\u5f53\u7684\u6784\u9020\u51fd\u6570\u4e2d\u6307\u5b9a ",(0,s.jsx)(r.code,{children:"initializeSchema"})," \u5e03\u5c14\u503c\u6216\u5728 ",(0,s.jsx)(r.code,{children:"application.properties"})," \u6587\u4ef6\u4e2d\u8bbe\u7f6e ",(0,s.jsx)(r.code,{children:"...initialize-schema=true"})," \u6765\u9009\u62e9\u52a0\u5165\u3002"]}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u6ce8\u610f\uff1a"})," \u8fd9\u662f\u4e00\u4e2a\u7834\u574f\u6027\u66f4\u6539\uff01\u5728 Spring AI \u7684\u65e9\u671f\u7248\u672c\u4e2d\uff0c\u6b64 schema \u521d\u59cb\u5316\u9ed8\u8ba4\u53d1\u751f\u3002"]}),"\n"]}),"\n",(0,s.jsxs)(r.p,{children:["Vector Store \u8fd8\u9700\u8981\u4e00\u4e2a ",(0,s.jsx)(r.code,{children:"EmbeddingModel"})," \u5b9e\u4f8b\u6765\u8ba1\u7b97\u6587\u6863\u7684\u5d4c\u5165\u3002\n\u60a8\u53ef\u4ee5\u9009\u62e9\u4e00\u4e2a\u53ef\u7528\u7684 ",(0,s.jsx)(r.a,{href:"../embeddings#available-implementations",children:"EmbeddingModel \u5b9e\u73b0"}),"\u3002"]}),"\n",(0,s.jsxs)(r.p,{children:["\u4f8b\u5982\uff0c\u8981\u4f7f\u7528 ",(0,s.jsx)(r.a,{href:"../embeddings/openai-embeddings",children:"OpenAI EmbeddingModel"}),"\uff0c\u8bf7\u5c06\u4ee5\u4e0b\u4f9d\u8d56\u9879\u6dfb\u52a0\u5230\u60a8\u7684\u9879\u76ee\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-xml",children:"<dependency>\n\t<groupId>org.springframework.ai</groupId>\n\t<artifactId>spring-ai-starter-model-openai</artifactId>\n</dependency>\n"})}),"\n",(0,s.jsxs)(r.p,{children:["\u6216\u6dfb\u52a0\u5230\u60a8\u7684 Gradle ",(0,s.jsx)(r.code,{children:"build.gradle"})," \u6784\u5efa\u6587\u4ef6\u3002"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-groovy",children:"dependencies {\n implementation 'org.springframework.ai:spring-ai-starter-model-openai'\n}\n"})}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"})," \u8bf7\u53c2\u9605 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-ai/reference/getting-started.html#dependency-management",children:"Dependency Management"})," \u90e8\u5206\uff0c\u5c06 Spring AI BOM \u6dfb\u52a0\u5230\u60a8\u7684\u6784\u5efa\u6587\u4ef6\u4e2d\u3002\n\u8bf7\u53c2\u9605 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-ai/reference/getting-started.html#artifact-repositories",children:"Artifact Repositories"})," \u90e8\u5206\uff0c\u5c06 Maven Central \u548c/\u6216 Snapshot Repositories \u6dfb\u52a0\u5230\u60a8\u7684\u6784\u5efa\u6587\u4ef6\u4e2d\u3002"]}),"\n"]}),"\n",(0,s.jsxs)(r.p,{children:["\u8981\u8fde\u63a5\u548c\u914d\u7f6e ",(0,s.jsx)(r.code,{children:"PgVectorStore"}),"\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u5b9e\u4f8b\u7684\u8bbf\u95ee\u8be6\u7ec6\u4fe1\u606f\u3002\n\u53ef\u4ee5\u901a\u8fc7 Spring Boot \u7684 ",(0,s.jsx)(r.code,{children:"application.yml"})," \u63d0\u4f9b\u7b80\u5355\u914d\u7f6e\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-yaml",children:"spring:\n datasource:\n url: jdbc:postgresql://localhost:5432/postgres\n username: postgres\n password: postgres\n ai:\n\tvectorstore:\n\t pgvector:\n\t\tindex-type: HNSW\n\t\tdistance-type: COSINE_DISTANCE\n\t\tdimensions: 1536\n\t\tmax-document-batch-size: 10000 # \u53ef\u9009\uff1a\u6bcf\u6279\u6587\u6863\u7684\u6700\u5927\u6570\u91cf\n"})}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"})," \u5982\u679c\u60a8\u901a\u8fc7 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.docker-compose",children:"Docker Compose"})," \u6216 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.testcontainers",children:"Testcontainers"})," \u5c06 PGvector \u4f5c\u4e3a Spring Boot dev service \u8fd0\u884c\uff0c\u5219\u65e0\u9700\u914d\u7f6e URL\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u56e0\u4e3a\u5b83\u4eec\u7531 Spring Boot \u81ea\u52a8\u914d\u7f6e\u3002"]}),"\n"]}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"}
1)," \u67e5\u770b ",(0,s.jsx)(r.a,{href:"#pgvector-properties",children:"configuration parameters"})," \u5217\u8868\u4ee5\u4e86\u89e3\u9ed8\u8ba4\u503c\u548c\u914d\u7f6e\u9009\u9879\u3002"]}),"\n"]}),"\n",(0,s.jsxs)(r.p,{children:["\u73b0\u5728\u60a8\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u81ea\u52a8\u88c5\u914d ",(0,s.jsx)(r.code,{children:"VectorStore"})," \u5e76\u4f7f\u7528\u5b83"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-java",children:'@Autowired VectorStore vectorStore;\n\n// ...\n\nList<Document> documents = List.of(\n new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),\n new Document("The World is Big and Salvation Lurks Around the Corner"),\n new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));\n\n// \u5c06\u6587\u6863\u6dfb\u52a0\u5230 PGVector\nvectorStore.add(documents);\n\n// \u68c0\u7d22\u4e0e\u67e5\u8be2\u76f8\u4f3c\u7684\u6587\u6863\nList<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());\n'})}),"\n",(0,s.jsx)(r.h2,{id:"pgvector-properties",children:"Configuration properties"}),"\n",(0,s.jsx)(r.p,{children:"\u60a8\u53ef\u4ee5\u5728 Spring Boot \u914d\u7f6e\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u5c5e\u6027\u6765\u81ea\u5b9a\u4e49 PGVector \u5411\u91cf\u5b58\u50a8\u3002"}),"\n",(0,s.jsxs)(r.table,{children:[(0,s.jsx)(r.thead,{children:(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.th,{children:"Property"}),(0,s.jsx)(r.th,{children:"Description"}),(0,s.jsx)(r.th,{children:"Default value"})]})}),(0,s.jsxs)(r.tbody,{children:[(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.index-type"})}),(0,s.jsxs)(r.td,{children:["\u6700\u8fd1\u90bb\u641c\u7d22\u7d22\u5f15\u7c7b\u578b\u3002\u9009\u9879\u6709 ",(0,s.jsx)(r.code,{children:"NONE"})," - \u7cbe\u786e\u6700\u8fd1\u90bb\u641c\u7d22\uff0c",(0,s.jsx)(r.code,{children:"IVFFlat"})," - \u7d22\u5f15\u5c06\u5411\u91cf\u5212\u5206\u4e3a\u5217\u8868\uff0c\u7136\u540e\u641c\u7d22\u6700\u63a5\u8fd1\u67e5\u8be2\u5411\u91cf\u7684\u90a3\u4e9b\u5217\u8868\u7684\u5b50\u96c6\u3002\u5b83\u7684\u6784\u5efa\u65f6\u95f4\u66f4\u5feb\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u6bd4 HNSW \u5c11\uff0c\u4f46\u67e5\u8be2\u6027\u80fd\u8f83\u4f4e\uff08\u5728\u901f\u5ea6-\u53ec\u56de\u6743\u8861\u65b9\u9762\uff09\u3002",(0,s.jsx)(r.code,{children:"HNSW"})," - \u521b\u5efa\u591a\u5c42\u56fe\u3002\u5b83\u7684\u6784\u5efa\u65f6\u95f4\u6bd4 IVFFlat \u6162\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u6bd4 IVFFlat \u591a\uff0c\u4f46\u67e5\u8be2\u6027\u80fd\u66f4\u597d\uff08\u5728\u901f\u5ea6-\u53ec\u56de\u6743\u8861\u65b9\u9762\uff09\u3002\u6ca1\u6709\u50cf IVFFlat \u90a3\u6837\u7684\u8bad\u7ec3\u6b65\u9aa4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u8868\u4e2d\u6ca1\u6709\u4efb\u4f55\u6570\u636e\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u7d22\u5f15\u3002"]}),(0,s.jsx)(r.td,{children:"HNSW"})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.distance-type"})}),(0,s.jsxs)(r.td,{children:["\u641c\u7d22\u8ddd\u79bb\u7c7b\u578b\u3002\u9ed8\u8ba4\u4e3a ",(0,s.jsx)(r.code,{children:"COSINE_DISTANCE"}),"\u3002\u4f46\u5982\u679c\u5411\u91cf\u5f52\u4e00\u5316\u5230\u957f\u5ea6 1\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 ",(0,s.jsx)(r.code,{children:"EUCLIDEAN_DISTANCE"})," \u6216 ",(0,s.jsx)(r.code,{children:"NEGATIVE_INNER_PRODUCT"})," \u4ee5\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u3002"]}),(0,s.jsx)(r.td,{children:"COSINE_DISTANCE"})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.dimensions"})}),(0,s.jsxs)(r.td,{children:["\u5d4c\u5165\u7ef4\u5ea6\u3002\u5982\u679c\u672a\u660e\u786e\u6307\u5b9a\uff0cPgVectorStore \u5c06\u4ece\u63d0\u4f9b\u7684 ",(0,s.jsx)(r.code,{children:"EmbeddingModel"})," \u68c0\u7d22\u7ef4\u5ea6\u3002\u7ef4\u5ea6\u5728\u8868\u521b\u5efa\u65f6\u8bbe\u7f6e\u5230\u5d4c\u5165\u5217\u3002\u5982\u679c\u60a8\u66f4\u6539\u7ef4\u5ea6\uff0c\u5219\u5fc5\u987b\u91cd\u65b0\u521b\u5efa vector_store \u8868\u3002"]}),(0,s.jsx)(r.td,{children:"-"})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.remove-existing-vector-store-table"})}
1),(0,s.jsxs)(r.td,{children:["\u5728\u542f\u52a8\u65f6\u5220\u9664\u73b0\u6709\u7684 ",(0,s.jsx)(r.code,{children:"vector_store"})," \u8868\u3002"]}),(0,s.jsx)(r.td,{children:"false"})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.initialize-schema"})}),(0,s.jsx)(r.td,{children:"\u662f\u5426\u521d\u59cb\u5316\u6240\u9700\u7684 schema"}),(0,s.jsx)(r.td,{children:"false"})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.schema-name"})}),(0,s.jsx)(r.td,{children:"\u5411\u91cf\u5b58\u50a8 schema \u540d\u79f0"}),(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"public"})})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.table-name"})}),(0,s.jsx)(r.td,{children:"\u5411\u91cf\u5b58\u50a8\u8868\u540d\u79f0"}),(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"vector_store"})})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.schema-validation"})}),(0,s.jsx)(r.td,{children:"\u542f\u7528 schema \u548c\u8868\u540d\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u662f\u6709\u6548\u4e14\u5b58\u5728\u7684\u5bf9\u8c61\u3002"}),(0,s.jsx)(r.td,{children:"false"})]}),(0,s.jsxs)(r.tr,{children:[(0,s.jsx)(r.td,{children:(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.max-document-batch-size"})}),(0,s.jsx)(r.td,{children:"\u5355\u6279\u5904\u7406\u7684\u6700\u5927\u6587\u6863\u6570\u3002"}),(0,s.jsx)(r.td,{children:"10000"})]})]})]}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"})," \u5982\u679c\u60a8\u914d\u7f6e\u81ea\u5b9a\u4e49 schema \u548c/\u6216\u8868\u540d\uff0c\u8bf7\u8003\u8651\u901a\u8fc7\u8bbe\u7f6e ",(0,s.jsx)(r.code,{children:"spring.ai.vectorstore.pgvector.schema-validation=true"})," \u6765\u542f\u7528 schema \u9a8c\u8bc1\u3002\n\u8fd9\u786e\u4fdd\u4e86\u540d\u79f0\u7684\u6b63\u786e\u6027\uff0c\u5e76\u964d\u4f4e\u4e86 SQL \u6ce8\u5165\u653b\u51fb\u7684\u98ce\u9669\u3002"]}),"\n"]}),"\n",(0,s.jsx)(r.h2,{id:"metadata-filtering",children:"Metadata filtering"}),"\n",(0,s.jsxs)(r.p,{children:["\u60a8\u53ef\u4ee5\u5c06\u901a\u7528\u7684\u3001\u53ef\u79fb\u690d\u7684 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters",children:"metadata filters"})," \u4e0e PgVector \u5b58\u50a8\u4e00\u8d77\u4f7f\u7528\u3002"]}),"\n",(0,s.jsx)(r.p,{children:"\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u8868\u8fbe\u5f0f\u8bed\u8a00\uff1a"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-java",children:"vectorStore.similaritySearch(\n SearchRequest.builder()\n .query(\"The World\")\n .topK(TOP_K)\n .similarityThreshold(SIMILARITY_THRESHOLD)\n .filterExpression(\"author in ['john', 'jill'] && article_type == 'blog'\").build());\n"})}),"\n",(0,s.jsxs)(r.p,{children:["\u6216\u4f7f\u7528 ",(0,s.jsx)(r.code,{children:"Filter.Expression"})," DSL \u4ee5\u7f16\u7a0b\u65b9\u5f0f\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-java",children:'FilterExpressionBuilder b = new FilterExpressionBuilder();\n\nvectorStore.similaritySearch(SearchRequest.builder()\n .query("The World")\n .topK(TOP_K)\n .similarityThreshold(SIMILARITY_THRESHOLD)\n .filterExpression(b.and(\n b.in("author","john", "jill"),\n b.eq("article_type", "blog")).build()).build());\n'})}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u6ce8\u610f\uff1a"})," \u8fd9\u4e9b\u8fc7\u6ee4\u5668\u8868\u8fbe\u5f0f\u88ab\u8f6c\u6362\u4e3a PostgreSQL JSON \u8def\u5f84\u8868\u8fbe\u5f0f\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u6570\u636e\u8fc7\u6ee4\u3002"]}),"\n"]}),"\n",(0,s.jsx)(r.h2,{id:"manual-configuration",children:"Manual Configuration"}),"\n",(0,s.jsxs)(r.p,{children:["\u4e0d\u4f7f\u7528 Spring Boot auto-configuration\uff0c\u60a8\u53ef\u4ee5\u624b\u52a8\u914d\u7f6e ",(0,s.jsx)(r.code,{children:"PgVectorStore"}
1),"\u3002\n\u4e3a\u6b64\uff0c\u60a8\u9700\u8981\u5c06 PostgreSQL \u8fde\u63a5\u548c ",(0,s.jsx)(r.code,{children:"JdbcTemplate"})," auto-configuration \u4f9d\u8d56\u9879\u6dfb\u52a0\u5230\u60a8\u7684\u9879\u76ee\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-xml",children:"<dependency>\n\t<groupId>org.springframework.boot</groupId>\n\t<artifactId>spring-boot-starter-jdbc</artifactId>\n</dependency>\n\n<dependency>\n\t<groupId>org.postgresql</groupId>\n\t<artifactId>postgresql</artifactId>\n\t<scope>runtime</scope>\n</dependency>\n\n<dependency>\n\t<groupId>org.springframework.ai</groupId>\n\t<artifactId>spring-ai-pgvector-store</artifactId>\n</dependency>\n"})}),"\n",(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.strong,{children:"\u63d0\u793a\uff1a"})," \u8bf7\u53c2\u9605 ",(0,s.jsx)(r.a,{href:"https://docs.spring.io/spring-ai/reference/getting-started.html#dependency-management",children:"Dependency Management"})," \u90e8\u5206\uff0c\u5c06 Spring AI BOM \u6dfb\u52a0\u5230\u60a8\u7684\u6784\u5efa\u6587\u4ef6\u4e2d\u3002"]}),"\n"]}),"\n",(0,s.jsx)(r.p,{children:"\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u914d\u7f6e PgVector\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bbe\u7f6e\uff1a"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-java",children:'@Bean\npublic VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {\n return PgVectorStore.builder(jdbcTemplate, embeddingModel)\n .dimensions(1536) // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a\u6a21\u578b\u7ef4\u5ea6\u6216 1536\n .distanceType(COSINE_DISTANCE) // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a COSINE_DISTANCE\n .indexType(HNSW) // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a HNSW\n .initializeSchema(true) // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a false\n .schemaName("public") // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a "public"\n .vectorTableName("vector_store") // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a "vector_store"\n .maxDocumentBatchSize(10000) // \u53ef\u9009\uff1a\u9ed8\u8ba4\u4e3a 10000\n .build();\n}\n'})}),"\n",(0,s.jsx)(r.h2,{id:"run-postgres--pgvector-db-locally",children:"Run Postgres & PGVector DB locally"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-bash",children:"docker run -it --rm --name postgres -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres pgvector/pgvector\n"})}),"\n",(0,s.jsx)(r.p,{children:"\u60a8\u53ef\u4ee5\u50cf\u8fd9\u6837\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff1a"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-bash",children:"psql -U postgres -h localhost -p 5432\n"})}),"\n",(0,s.jsx)(r.h2,{id:"accessing-the-native-client",children:"Accessing the Native Client"}),"\n",(0,s.jsxs)(r.p,{children:["PGVector Store \u5b9e\u73b0\u901a\u8fc7 ",(0,s.jsx)(r.code,{children:"getNativeClient()"})," \u65b9\u6cd5\u63d0\u4f9b\u5bf9\u5e95\u5c42\u539f\u751f JDBC \u5ba2\u6237\u7aef\uff08",(0,s.jsx)(r.code,{children:"JdbcTemplate"}),"\uff09\u7684\u8bbf\u95ee\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-java",children:"PgVectorStore vectorStore = context.getBean(PgVectorStore.class);\nOptional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();\n\nif (nativeClient.isPresent()) {\n JdbcTemplate jdbc = nativeClient.get();\n // \u4f7f\u7528\u539f\u751f\u5ba2\u6237\u7aef\u8fdb\u884c PostgreSQL \u7279\u5b9a\u64cd\u4f5c\n}\n"})}),"\n",(0,s.jsxs)(r.p,{children:["\u539f\u751f\u5ba2\u6237\u7aef\u4f7f\u60a8\u53ef\u4ee5\u8bbf\u95ee\u53ef\u80fd\u672a\u901a\u8fc7 ",(0,s.jsx)(r.code,{children:"VectorStore"})," \u63a5\u53e3\u516c\u5f00\u7684 PostgreSQL \u7279\u5b9a\u529f\u80fd\u548c\u64cd\u4f5c\u3002"]})]})}function h(e={}){const{wrapper:r}={...(0,i.R)(),...e.components};return r?(0,s.jsx)(r,{...e,children:(0,s.jsx)(l,{...e})}):l(e)}},28453:(e,r,n)=>{n.d(r,{R:()=>c,x:()=>o});var t=n(96540);const s={},i=t.createContext(s);function c(e){const r=t.useContext(i);return t.useMemo(function(){return"function"==typeof e?e(r):{...r,...e}},[r,e])}function o(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:c(e.components),t.createElement(i.Provider,{value:r},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.