Elasticsearch
Elasticsearch is a search database, that supports full text and vector searches.
Basic Example
Section titled “Basic Example”In this basic example, we take the a Paul Graham essay, split it into chunks, embed it using an open-source embedding model, load it into Elasticsearch, and then query it. For an example using different retrieval strategies see Elasticsearch Vector Store.
If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
%pip install -qU llama-index-vector-stores-elasticsearch llama-index-embeddings-huggingface llama-index# importfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.vector_stores.elasticsearch import ElasticsearchStorefrom llama_index.core import StorageContext# set up OpenAIimport osimport getpass
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")Download Data
!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.txt'2024-05-13 15:10:43 URL:https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt [75042/75042] -> "data/paul_graham/paul_graham_essay.txt" [1]from llama_index.embeddings.huggingface import HuggingFaceEmbeddingfrom llama_index.core import Settings
# define embedding functionSettings.embed_model = HuggingFaceEmbedding( model_name="BAAI/bge-small-en-v1.5")# load documentsdocuments = SimpleDirectoryReader("./data/paul_graham/").load_data()
# define indexvector_store = ElasticsearchStore( es_url="http://localhost:9200", # see Elasticsearch Vector Store for more authentication options index_name="paul_graham_essay",)storage_context = StorageContext.from_defaults(vector_store=vector_store)index = VectorStoreIndex.from_documents( documents, storage_context=storage_context)# Query Dataquery_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.Note for AI agents: this documentation is built for programmatic access.
- Overview of all docs: https://developers.llamaindex.ai/llms.txt
- Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md
- Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters.
- A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/python/shared/mcp/