ElasticSearch的向量存储和搜索

ElasticSearch的向量存储和搜索

引入依赖

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>langchain4j-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>


        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j</artifactId>
            <version>0.29.1</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-qianfan</artifactId>
            <version>0.30.0</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-open-ai</artifactId>
            <version>0.29.1</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-zhipu-ai</artifactId>
            <version>0.29.1</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-dashscope</artifactId>
            <version>0.29.1</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-ollama</artifactId>
            <version>0.29.1</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId>
            <version>0.29.1</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-pgvector</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-elasticsearch</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-redis</artifactId>
            <version>0.29.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

示例代码

java 复制代码
package org.example;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.elasticsearch.ElasticsearchEmbeddingStore;

public class EsMain {

    public static void main(String[] args) {

        TextSegment textSegment = TextSegment.from("我爱吃苹果");

        EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();
        Response<Embedding> embed = embeddingModel.embed(textSegment);

        ElasticsearchEmbeddingStore embeddingStore = ElasticsearchEmbeddingStore
        .builder()
        .serverUrl("http://192.11.2.1:29201")
        .indexName("vector_index")
        .dimension(embed.content().dimension())
        .userName("elastic")
        .password("1231230")
        .build();

        embeddingStore.add(embed.content(), textSegment);


        TextSegment queryTextSegment = TextSegment.from("我喜欢吃苹果");
        Embedding queryEmbedding = embeddingModel.embed(queryTextSegment).content();
        EmbeddingSearchRequest request = EmbeddingSearchRequest.builder().queryEmbedding(queryEmbedding).maxResults(3).minScore(0.1).build();
        EmbeddingSearchResult<TextSegment> result = embeddingStore.search(request);

        for (EmbeddingMatch<TextSegment> match : result.matches()) {
            System.out.println(match.embedded().text());
            System.out.println(match.score());
        }
    }
}
相关推荐
PingCAP9 分钟前
TiDB 亮相宜昌“医院‘云数智’技术实践研讨及成果展示交流会”,探讨国产化 + AI 背景下的数据库新趋势
数据库·人工智能·tidb
文弱_书生9 分钟前
再谈图像处理中的傅里叶变换
图像处理·人工智能·傅里叶变换
钡铼技术物联网关12 分钟前
ARM边缘计算时代:BLIoTLink如何打通设备互联任督二脉
arm开发·人工智能·边缘计算
小李独爱秋19 分钟前
机器学习开发全流程详解:从数据到部署的完整指南
人工智能·机器学习
Dovis(誓平步青云)22 分钟前
深挖 DeepSeek 隐藏玩法·智能炼金术2.0版本
人工智能·深度学习·机器学习·数据挖掘·服务发现·智慧城市
陈明勇24 分钟前
一文掌握 MCP 上下文协议:从理论到实践
人工智能·后端·mcp
zskj_zhyl26 分钟前
智绅科技全场景智慧养老系统:助力老年人畅享幸福晚年
人工智能·科技
Lx35229 分钟前
区块链+AI:智能合约的自动化审计革命
人工智能
weixin_3875456433 分钟前
探索 GitHub Copilot:当 AI 成为你的贴身编码助手
人工智能·github·copilot
zidea33 分钟前
我和我的 AI Agent(1) 异步优先、结构化输出以及如何处理依赖
人工智能·python·trae