【RAG】【vector_stores034】Elasticsearch基础示例分析
1. 案例目标本案例展示了如何使用Elasticsearch作为向量存储与LlamaIndex集成的基础用法。通过将Paul Graham的文章分割成块使用开源嵌入模型进行向量化加载到Elasticsearch中然后进行查询演示了基本的向量检索功能。2. 技术栈与核心依赖Elasticsearch: 支持全文搜索和向量搜索的搜索数据库llama-index-vector-stores-elasticsearch: LlamaIndex与Elasticsearch集成的向量存储包llama-index-embeddings-huggingface: LlamaIndex与Hugging Face嵌入模型集成包llama-index: LlamaIndex核心框架BAAI/bge-small-en-v1.5: 用于文本嵌入的开源模型3. 环境配置3.1 依赖安装%pip install -qU llama-index-vector-stores-elasticsearch llama-index-embeddings-huggingface llama-index3.2 导入必要的库# import from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.vector_stores.elasticsearch import ElasticsearchStore from llama_index.core import StorageContext3.3 设置OpenAI API密钥# set up OpenAI import os import getpass os.environ[OPENAI_API_KEY] getpass.getpass(OpenAI API Key:)注意虽然示例中设置了OpenAI API密钥但在本示例中实际使用的是Hugging Face的嵌入模型而不是OpenAI的嵌入模型。API密钥可能是用于查询生成部分。4. 案例实现4.1 数据准备下载Paul Graham的文章数据!mkdir -p data/paul_graham/ !wget -nv https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt -O data/paul_graham/paul_graham_essay.txt4.2 配置嵌入模型使用Hugging Face的BAAI/bge-small-en-v1.5模型作为嵌入模型from llama_index.embeddings.huggingface import HuggingFaceEmbedding from llama_index.core import Settings # define embedding function Settings.embed_model HuggingFaceEmbedding( model_nameBAAI/bge-small-en-v1.5 )4.3 加载文档并创建索引# load documents documents SimpleDirectoryReader(./data/paul_graham/).load_data() # define index vector_store ElasticsearchStore( es_urlhttp://localhost:9200, # 更多认证选项请参见Elasticsearch Vector Store文档 index_namepaul_graham_essay, ) storage_context StorageContext.from_defaults(vector_storevector_store) index VectorStoreIndex.from_documents( documents, storage_contextstorage_context )提示确保Elasticsearch服务在http://localhost:9200上运行如果使用远程Elasticsearch或需要认证请参考Elasticsearch Vector Store文档中的认证选项index_name参数指定了在Elasticsearch中创建的索引名称4.4 查询数据# Query Data query_engine index.as_query_engine() response query_engine.query(What did the author do growing up?) print(response)示例输出The author worked on writing and programming outside of school. They wrote short stories and tried writing programs on an IBM 1401 computer. They also built a microcomputer kit and started programming on it, writing simple games and a word processor.5. 案例效果通过本示例用户可以成功将Paul Graham的文章加载到Elasticsearch向量存储中使用开源嵌入模型(BAAI/bge-small-en-v1.5)对文本进行向量化执行自然语言查询获取与问题相关的文本段落基于向量相似性检索相关内容而不仅仅是关键词匹配6. 案例实现思路环境准备安装必要的依赖包包括Elasticsearch向量存储和Hugging Face嵌入模型数据获取下载Paul Graham的文章作为示例数据嵌入模型配置设置Hugging Face的BAAI/bge-small-en-v1.5模型作为文本嵌入模型向量存储初始化创建ElasticsearchStore实例连接到本地Elasticsearch服务索引构建使用StorageContext和VectorStoreIndex从文档构建向量索引查询执行创建查询引擎并执行自然语言查询结果展示输出查询结果展示向量检索的效果7. 扩展建议高级检索策略参考[Elasticsearch Vector Store](https://docs.llamaindex.ai/en/stable/examples/vector_stores/ElasticsearchIndexDemo/)示例探索不同的检索策略如密集向量检索、稀疏向量检索、关键词搜索和混合搜索元数据过滤添加文档元数据并实现基于元数据的过滤查询自定义嵌入模型尝试其他开源嵌入模型或训练自定义嵌入模型批量处理优化大批量文档的处理流程提高索引构建效率查询优化调整查询参数如top_k、相似度阈值等以获得更精确的搜索结果安全配置为Elasticsearch添加认证和授权机制保护数据安全性能监控监控Elasticsearch的性能指标优化查询响应时间多语言支持配置适合不同语言的分析器和嵌入模型8. 总结Elasticsearch基础示例展示了如何将Elasticsearch作为向量存储与LlamaIndex集成实现基本的向量检索功能。通过使用开源的嵌入模型和Elasticsearch的强大搜索能力用户可以构建高效的语义搜索系统。这个示例为更复杂的向量检索应用奠定了基础如文档问答系统、内容推荐引擎等。Elasticsearch的可扩展性和丰富的查询功能使其成为构建大规模向量检索应用的理想选择。