Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElasticVectorSearch constructor to support Elastic Cloud #1865

Closed
brandco opened this issue Mar 21, 2023 · 6 comments
Closed

ElasticVectorSearch constructor to support Elastic Cloud #1865

brandco opened this issue Mar 21, 2023 · 6 comments

Comments

@brandco
Copy link

brandco commented Mar 21, 2023

The current constructor for ElasticVectorSearch does not support connecting to Elastic Cloud.

>>> db = ElasticVectorSearch.from_documents(docs, embeddings,
...                                         cloud_id = CLOUD_ID,
...                                         basic_auth=(USER_NAME, PASSWORD))Traceback (most recent call last):  File "<stdin>", line 1, in <module>
  File "c:\Users\bcollingsworth\code\langchain_testing\.venv\lib\site-packages\langchain\vectorstores\base.py", line 113, in from_documents
    return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)
  File "c:\Users\bcollingsworth\code\langchain_testing\.venv\lib\site-packages\langchain\vectorstores\elastic_vector_search.py", line 161, in from_texts
    elasticsearch_url = get_from_dict_or_env(
  File "c:\Users\bcollingsworth\code\langchain_testing\.venv\lib\site-packages\langchain\utils.py", line 17, in get_from_dict_or_env
    raise ValueError(
ValueError: Did not find elasticsearch_url, please add an environment variable `ELASTICSEARCH_URL` which contains it, or pass  `elasticsearch_url` as a named parameter.

It looks like the current implementation would only support an instance of Elastic Search on localhost without security enabled, which is not recommended.

I can connect with the elasticsearch python client like this:

from elasticsearch import Elasticsearch

es = Elasticsearch(cloud_id=CLOUD_ID, http_auth=(USER_NAME, PASSWORD))
es.info()

See the documentation at Connecting to Elastic Cloud

An alternative constructor to the ElasticVectorSearch that takes a elasticsearch client instance, index name and embedding object would allow the user to deal with all the possible authentication set ups before using langchain.

@sergerdn
Copy link
Contributor

sergerdn commented Mar 29, 2023

@brandco

The login and password are working as expected, and you can set them up in the elasticsearch_url .
Here's a well-known trick:

elasticsearch_url = "https://elastic:YOUR_PASSWORD@es_host:es_port"
elastic_search = ElasticVectorSearch(
        elasticsearch_url= elasticsearch_url,
        index_name= index_name,
        embedding=embedding
)

This approach also applies to ElasticSearch installations on the cloud or any other platform:

elasticsearch_url = "https://elastic:YOUR_PASSWORD@es_host:es_port"
es = Elasticsearch(elasticsearch_url)

es.delete_by_query(index=es_index_name, body={"query": {"match_all": {}}})

@sergerdn
Copy link
Contributor

@brandco, #2141

Does it look good to you, or do you have any further questions? I have documented it in as much detail as possible.

agola11 pushed a commit that referenced this issue Mar 29, 2023
This merge includes updated comments in the ElasticVectorSearch class to
provide information on how to connect to `Elasticsearch` instances that
require login credentials, including Elastic Cloud, without any
functional changes.

The `ElasticVectorSearch` class now inherits from the `ABC` abstract
base class, which does not break or change any functionality. This
allows for easy subclassing and creation of custom implementations in
the future or for any users, especially for me 😄

I confirm that before pushing these changes, I ran:
```bash
make format && make lint
```

To ensure that the new documentation is rendered correctly I ran
```bash
make docs_build
```

To ensure that the new documentation has no broken links, I ran a check
```bash
make docs_linkcheck
```


![Capture](https://user-images.githubusercontent.com/64213648/228541688-38f17c7b-b012-4678-86b9-4dd607469062.JPG)

Also take a look at #1865

P.S. Sorry for spamming you with force-pushes. In the future, I will be
smarter.
@vibha0411
Copy link

I still have an issue.
I am trying to use ElasticVectorSearch.from_documents and it is creating a random index name and not using the index_name provided.
Is there any reason for this?

@dosubot
Copy link

dosubot bot commented Sep 18, 2023

Hi, @brandco! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, the issue you reported was about the current constructor for ElasticVectorSearch not supporting connecting to Elastic Cloud, which limits usage to only instances of Elastic Search on localhost without security enabled. One user suggested a workaround by setting up login and password in the elasticsearch_url, and another user provided a pull request with detailed documentation. It seems that the issue has been resolved.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository!

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 18, 2023
@brandco
Copy link
Author

brandco commented Sep 19, 2023

Thank you all. I'm sorry I was not able to test this fix because I had to spin down my cloud instance.

@brandco brandco closed this as completed Sep 19, 2023
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 19, 2023
@dosubot
Copy link

dosubot bot commented Sep 19, 2023

Thank you, @brandco, for closing the issue! We appreciate your help in keeping LangChain organized and efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants