# Using elastic search with langchain

**URL:** <https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748>\
**Category:** Elasticsearch\
**Created:** [April 18, 2024, 9:45pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748 "2024-04-18T21:45:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Alzate](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_alzate/32/122038_2.png) [@David\_Alzate](https://discuss.elastic.co/u/David_Alzate)\
**Post date:** [April 18, 2024, 9:45pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/1 "2024-04-18T21:45:45Z")

</div>

This category relates to the [Enterprise Search](https://www.elastic.co/enterprise-search) set of products - App Search, Site Search and Workplace Search.  
If your question relates to core Elasticsearch functionality, please head over to the #Elasticsearch category for assistance.

I am using in python the libraries langchain\_elasticsearch to implement a MultiQueryRetriever. from the code seen below.

```python
 from langchain_elasticsearch import ElasticsearchStore
 from langchain.retrievers.multi_query import MultiQueryRetriever
 vectorstore = ElasticsearchStore(
    embedding=embeddings,
    vector_query_field = "query_embeddings",
    query_field = "query_field",
    index_name="index_with_embeddings",
    es_connection=es_client
 )
 retriever = MultiQueryRetriever( retriever=vectorstore.as_retriever(), llm_chain=llm_chain, parser_key="lines")
unique_docs = retriever.get_relevant_documents(query='here is the question?')

```

The mapping of the index is the following:

```json
 "index_with_embeddings": {
        "mappings": {
            "dynamic": "strict",
            "properties": {
                "CreateTimeStamp": {
                    "type": "date"
                },
                "query_embeddings": {
                    "type": "dense_vector",
                    "dims": 1536,
                    "index": true,
                    "similarity": "cosine"
                },
                "query_field": {
                    "type": "text"
                },
                "Sequence": {
                    "type": "integer"
                },
                "Field_add1": {
                    "type": "text"
                },
                "Field_add2": {
                    "type": "text"
                }
            }
        }
    }

```

The error I got in python is the following:

```python
[Lib\site-packages\langchain\retrievers\multi_query.py:175](file://.venv/Lib/site-packages/langchain/retrievers/multi_query.py:175),
in MultiQueryRetriever._get_relevant_documents(self, query, run_manager) 

[173](file://.venv/Lib/site-packages/langchain/retrievers/multi_query.py:173) 
if self.include_original:

[807](file://.venv/Lib/site-packages/langchain_elasticsearch/vectorstores.py:807) 
page_content=hit["_source"].get(self.query_field, ""), 
[808](file://.venv/Lib/site-packages/langchain_elasticsearch/vectorstores.py:808)
metadata=hit["_source"]["metadata"],

```

Does anybody know how this problem can be fixed to use an existing index with the definition mentioned above?

The current notebooks in github are precarious and they don't give details about existing indexes. (here is the link ) [elasticsearch-labs/notebooks/langchain at main · elastic/elasticsearch-labs · GitHub](https://github.com/elastic/elasticsearch-labs/tree/main/notebooks/langchain)

Do the indexes must follow the schema of the "langchain schema document"? Is there any way to use an existing index that does have a different structure?

---

<div class="post-metadata">

**Author:** ![Rodney\_Norris](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rodney_norris/32/117187_2.png) [@Rodney\_Norris](https://discuss.elastic.co/u/Rodney_Norris)\
**Post date:** [April 19, 2024, 2:45pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/2 "2024-04-19T14:45:38Z")

</div>

> [@David\_Alzate](#):
>
> ```auto
> [808](file://.venv/Lib/site-packages/langchain_elasticsearch/vectorstores.py:808)
> metadata=hit["_source"]["metadata"],
> 
> ```

This line in the error makes me think this might be the same error reported here: [langchain-elastic #7](https://github.com/langchain-ai/langchain-elastic/issues/7) where the [default\_doc\_builder](https://github.com/langchain-ai/langchain-elastic/blob/main/libs/elasticsearch/langchain_elasticsearch/vectorstores.py#L889) is expecting there to be a `metadata` key on the document. If it's missing there is a KeyError.

I'm looking into resolving this, but for now you could try to add an empty `metadata: {}` field to your documents and see if it resolves the error.

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [April 22, 2024, 9:24am UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/3 "2024-04-22T09:24:30Z")

</div>

Also to mention, the vector\_store setup the indices for you, as you ingest through LangChain's own loaders [Document loaders | 🦜️🔗 LangChain](https://python.langchain.com/docs/modules/data_connection/document_loaders/)

For those who have ingested data externally and simply want to query, we advise using the [ElasticsearchRetriever](https://python.langchain.com/docs/integrations/retrievers/elasticsearch_retriever/) which might fit your needs better.

Joe

---

<div class="post-metadata">

**Author:** ![David\_Alzate](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_alzate/32/122038_2.png) [@David\_Alzate](https://discuss.elastic.co/u/David_Alzate)\
**Post date:** [April 22, 2024, 9:53pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/4 "2024-04-22T21:53:18Z")

</div>

Hi Joseph,

Thanks for your suggestion of using Elasticsearchretriever, it seems a better solution for existing indexes, and I tested with several queries and it worked.

However, I tried to run a llm\_chain to use the MultiQueryRetriever, but when I passed the Elasticsearchretriever to instantiate the MultiQueryRetriever I am getting the following error.

```python
def vector_query(search_query: str) -> dict:
    vector = embeddings.embed_query(search_query) # same embeddings as for indexing
    return {
        "knn": {
            "field": "Embeddings",
            "query_vector": vector,
            "k": 5,
            "num_candidates": 10,
        }
    }

index_name = "index_wiht_embeddings"
vector_retriever = ElasticsearchRetriever.from_es_params(
    index_name=index_name,
    body_func=vector_query,
    content_field="Sentence",
    url=f"https://{ELASTIC_URL}:443",
    username = ELASTIC_USER,
    password = ELASTIC_PASSWORD     
)
question = "Who are the people who have knowledge in Elasticsearch"

output_parser = LineListOutputParser()
QUERY_PROMPT = PromptTemplate(
    input_variables=["question"],
    template="""You are an AI language model assistant. Your task is to generate five 
    different versions of the given user question to retrieve relevant documents from a vector 
    database. By generating multiple perspectives on the user question, your goal is to help
    the user overcome some of the limitations of the distance-based similarity search. 
    Provide these alternative questions separated by newlines, and do not number the response, or add any character at the beggining.
    Original question: {question}""",
)

llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)

```

and once I created the Multiquery here is the error.

```python
retriever = MultiQueryRetriever(retriever=vector_retriever, llm_chain=llm_chain, parser_key="lines")  
unique_docs = retriever.invoke(query=question)
len(unique_docs)

```

Error:

```python
TypeError Traceback (most recent call last)
Cell In[51], line 2
      1 retriever = MultiQueryRetriever(retriever=vector_retriever, llm_chain=llm_chain, parser_key="lines")  
----> 2 unique_docs = retriever.invoke(query=question)
      3 len(unique_docs)

TypeError: BaseRetriever.invoke() missing 1 required positional argument: 'input'

```

My question is if there any way o pass Elasticsearchretriever as a retriever to the Multiqueryretriever ?

Thanks beforehand,  
David

---

<div class="post-metadata">

**Author:** ![David\_Alzate](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_alzate/32/122038_2.png) [@David\_Alzate](https://discuss.elastic.co/u/David_Alzate)\
**Post date:** [April 22, 2024, 10:12pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/5 "2024-04-22T22:12:52Z")

</div>

Hi Rodney,

I will try the solution you proposed later, because it seems more appropriated to test the solution suggested by Joseph, that retriever allows me to implement a hybrid search over several fields, so it'd be better to use Elasticsearchretriever.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [April 24, 2024, 11:08am UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/6 "2024-04-24T11:08:13Z")

</div>

Hey David,

So here is an example of the multi-query using the Vector store in a notebook. [elasticsearch-labs/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb at main · elastic/elasticsearch-labs · GitHub](https://github.com/elastic/elasticsearch-labs/blob/main/notebooks/langchain/multi-query-retriever-examples/langchain-multi-query-retriever.ipynb)

My next task is to try this out with the retriever, to verify if theres any issue here. So will do that and report back!

Joe

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [April 26, 2024, 8:01pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/7 "2024-04-26T20:01:32Z")

</div>

so it should work, tested it by updating the [chatbot notebook](https://github.com/elastic/elasticsearch-labs/blob/main/notebooks/langchain/multi-query-retriever-examples/chatbot-with-multi-query-retriever.ipynb)

and using the following code for retriever

```python
from langchain_elasticsearch import ElasticsearchRetriever

def vector_query(search_query: str):
    vector = embeddings.embed_query(search_query) # same embeddings as for indexing
    return {
        "knn": {
            "field": "vector",
            "query_vector": vector,
            "k": 3,
            "num_candidates": 10,
        }
    }

vector_retriever = ElasticsearchRetriever.from_es_params(
    index_name="chatbot-multi-query-demo",
    body_func=vector_query,
    content_field="text",
    # url="http://localhost:9200",
)

retriever = MultiQueryRetriever.from_llm(vector_retriever, llm)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 24, 2024, 8:01pm UTC](https://discuss.elastic.co/t/using-elastic-search-with-langchain/357748/8 "2024-05-24T20:01:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
