How to Add Semantic Search to OpenSearch with Haystack
Haystack adds semantic search to OpenSearch by turning documents into vectors and searching them by meaning. Here is how the pieces fit and how to build it.
On this page
Short answer
Semantic search in OpenSearch with Haystack means searching documents by meaning: Haystack turns your documents and your question into vectors, and OpenSearch stores those vectors and returns the closest matches. You keep OpenSearch keyword search and add a second, meaning-based path that finds documents even when they use different words.
What is semantic search in OpenSearch with Haystack?
Semantic search in OpenSearch with Haystack is a way to search documents by meaning instead of matching exact words, using Haystack to run the search pipeline and OpenSearch to store and search the vectors.
Haystack is an open-source Python framework from deepset for building search and retrieval pipelines. OpenSearch is an open-source search engine derived from Elasticsearch, and its k-NN plugin adds vector search. Together they give you a search system where a question about refunds for a late delivery can find a help article about returns and compensation, because the two texts mean similar things even though they share few words.
The basic idea is simple. Each document is split into chunks and turned into a vector, a list of numbers that captures what the text means. The same embedding model turns the user's question into a vector, and OpenSearch returns the chunks whose vectors sit closest to it. Keyword search still works alongside this, so you get two ways to find the same documents.
This combination suits teams that already run OpenSearch or want an open-source stack they control. You do not need a separate vector database, and you keep the filters, access control and analytics that OpenSearch already provides. For background on the idea itself, see what semantic search is before you start building.
How do the pieces fit together?
A Haystack and OpenSearch search system has five parts, and each one does a single job. If you can name the part that is failing, you can usually fix it without rebuilding the rest.
| Part | Job |
|---|---|
| Embedding model | Turns text into vectors |
| Document store | Stores text and vectors in OpenSearch |
| Retriever | Finds the nearest vectors for a question |
| Pipeline | Connects the steps in order |
| Ranker (optional) | Re-orders the best matches |
At index time, documents flow through a pipeline: a converter reads the files, a splitter cuts them into chunks, an embedder turns each chunk into a vector, and a writer stores the chunk and its vector in OpenSearch. At query time, a second pipeline embeds the question, asks the retriever for the closest chunks, and hands them to your app, or to a language model that writes the answer.
The embedding model is the part that decides what similar means. Use the same model for documents and for questions, because vectors from different models cannot be compared. Changing the model later means re-embedding everything, so pick one early and test it on your own content before you index a large collection.
How does OpenSearch store and search vectors?
OpenSearch stores vectors in a k-NN index, using its k-NN plugin. Each document chunk becomes a record with the text, its metadata and a vector field, and the index builds a graph that lets it find near neighbours without comparing the question against every record.
When a query arrives, OpenSearch walks that graph to find the vectors closest to the question vector, usually by cosine similarity, which measures the angle between two vectors rather than their length. This is approximate nearest neighbour search: it trades a small amount of accuracy for speed, which is what makes searching millions of chunks fast enough for a live product.
You control the trade-off with a few settings. The index mapping sets the vector size, which must match your embedding model, and the engine, where the common choices are Lucene, Faiss and nmslib. The query sets how many candidates to check and how many results to return. More candidates means better results and slower queries, so the right numbers come from testing with your own data, not from defaults.
How do you build the indexing pipeline?
You build the indexing pipeline first, because nothing can be searched until the documents are stored. The pipeline has six steps, and each one is a Haystack component you can swap later.
- Start OpenSearch, for example with the official Docker image, and check it answers on its HTTP port.
- Install the integration with pip install opensearch-haystack.
- Create an OpenSearchDocumentStore pointing at your OpenSearch host, with the index name and the vector size your embedding model produces.
- Add a converter for your file type, a splitter to cut documents into chunks, and an embedder that turns chunks into vectors.
- Connect them in an indexing pipeline that ends with a DocumentWriter.
- Run the pipeline over your documents and check the index holds the expected number of chunks.
Chunk size matters more than most teams expect. Chunks that are too long mix several topics, so a good match can hide inside a chunk about something else. Chunks that are too short lose the context that makes them useful. A starting point is 300 to 500 words with some overlap, then test with real questions and adjust.
Store the source text with each vector, plus metadata such as the document title, date and section. The retriever needs the text to show the user, and the metadata lets you filter results by product, language or date before the vector search runs.
How do you build the query pipeline?
The query pipeline is shorter than the indexing pipeline. It embeds the user's question with the same model used at index time, asks OpenSearch for the closest chunks, and returns them in order of similarity.
The main choices are how many results to fetch and whether to combine vector search with keyword search. Fetching ten results and showing the best five is a common pattern, because the top few are usually the most useful and the rest leave room for the ranker. Hybrid search, which blends a keyword retriever with the vector retriever, helps when users type product names, error codes or other exact terms that meaning alone can miss.
Filters belong in the query, not in the application code after retrieval. If a user should only see documents for their product or region, pass that filter to the retriever so OpenSearch searches only the allowed set. Filtering after retrieval wastes results and can return fewer answers than the user should see. The step-by-step approach is covered in how to implement semantic search.
Example (hypothetical): a support docs search
Example (hypothetical): a software company has 12,000 help articles and wants search that understands meaning. The team splits each article into 3 chunks on average, which gives 36,000 chunks to embed and store.
Their embedder handles 100 chunks a minute, so the one-off indexing job takes 36,000 divided by 100, which is 360 minutes, or 6 hours. They run it overnight, and new articles are indexed as they are published, a few at a time.
At query time they fetch the 10 closest chunks and show the best 5. On a test set of 50 questions, 8 of the 10 results are relevant on average, which means the support team can link customers to the right article without writing new content. The gain came from the vectors, not from rewriting the docs.
What are the common mistakes?
The most common mistakes in a Haystack and OpenSearch setup are easy to make and easy to avoid once you know them.
- Using a different embedding model for questions than for documents, which makes the vectors impossible to compare.
- Indexing whole documents as single chunks, so a good answer hides inside a long page about something else.
- Skipping filters, so users see documents they should not see.
- Storing vectors without the source text, so results cannot be shown or checked.
- Judging quality by eye on five questions instead of testing on a real set.
A related mistake is treating the first result as the answer. Semantic search returns the closest matches, not a verified fact. If the pipeline feeds a language model, the model still needs the source text and a clear instruction to say when the documents do not contain the answer.
How do you measure and improve search quality?
You measure search quality with a test set: a list of real questions paired with the documents that should answer them. Run each question through the pipeline and count how often the right document appears in the top five results.
When the numbers are low, change one thing at a time. Try a different chunk size, raise the number of candidates, add a keyword retriever for exact terms, or test another embedding model. Each change gets a fresh run against the same test set, so you can tell whether it helped.
Keep a small set of questions that you check by hand as well, because numbers can hide problems such as outdated documents ranking first. If you are choosing between frameworks before you build, Haystack vs LangChain compares the two most common options.
The wider pattern this search feeds into is covered in RAG development.
Key takeaways
- Haystack runs the pipeline; OpenSearch stores and searches the vectors
- Use the same embedding model for documents and questions
- Chunk size and filters matter as much as the model choice
- Measure quality on a real test set before changing anything
- Keyword search still has a job: exact names, codes and terms
Frequently asked questions
What is Haystack, in simple terms?
Haystack is an open-source Python framework from deepset for building search and retrieval pipelines. It connects converters, splitters, embedders, document stores and retrievers into pipelines, so you can build semantic search or retrieval-augmented generation without writing the plumbing yourself.
Do I need a separate vector database with OpenSearch?
No. OpenSearch has a k-NN plugin that stores vectors and searches them by similarity, so it can act as both your search engine and your vector store. A separate vector database only makes sense if you need features OpenSearch does not offer.
Can I combine keyword search with semantic search?
Yes, and most teams should. Hybrid search runs a keyword retriever and a vector retriever together, then blends the results. Keyword search catches exact product names and error codes, while vector search catches questions phrased in different words.
How many chunks should a document be split into?
It depends on the document, but 300 to 500 words per chunk with some overlap is a common starting point. Test with real questions: if good answers hide inside long chunks, split smaller, and if results lose context, split larger.
What happens if I change the embedding model?
You must re-embed and re-index everything, because vectors from different models cannot be compared. That is why the model choice matters early: test it on your own content before you index a large collection.
Is Haystack only for chatbots?
No. Haystack is used for semantic search, document question answering, and retrieval-augmented generation, and chatbots are only one use. Any product that needs to find the right document for a question can use the same pipelines.