mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-27 08:58:48 +00:00
make the elasticsearch api support version which below 8.x (#5495)
the api which create index or search in the elasticsearch below 8.x is different with 8.x. When use the es which below 8.x , it will throw error. I fix the problem Co-authored-by: gaofeng27692 <gaofeng27692@hundsun.com>
This commit is contained in:
parent
6632188606
commit
80b3fdf2f7
@ -177,7 +177,7 @@ class ElasticVectorSearch(VectorStore, ABC):
|
|||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
# TODO would be nice to create index before embedding,
|
# TODO would be nice to create index before embedding,
|
||||||
# just to save expensive steps for last
|
# just to save expensive steps for last
|
||||||
self.client.indices.create(index=self.index_name, mappings=mapping)
|
self.create_index(self.client, self.index_name, mapping)
|
||||||
|
|
||||||
for i, text in enumerate(texts):
|
for i, text in enumerate(texts):
|
||||||
metadata = metadatas[i] if metadatas else {}
|
metadata = metadatas[i] if metadatas else {}
|
||||||
@ -226,7 +226,9 @@ class ElasticVectorSearch(VectorStore, ABC):
|
|||||||
"""
|
"""
|
||||||
embedding = self.embedding.embed_query(query)
|
embedding = self.embedding.embed_query(query)
|
||||||
script_query = _default_script_query(embedding, filter)
|
script_query = _default_script_query(embedding, filter)
|
||||||
response = self.client.search(index=self.index_name, query=script_query, size=k)
|
response = self.client_search(
|
||||||
|
self.client, self.index_name, script_query, size=k
|
||||||
|
)
|
||||||
hits = [hit for hit in response["hits"]["hits"]]
|
hits = [hit for hit in response["hits"]["hits"]]
|
||||||
docs_and_scores = [
|
docs_and_scores = [
|
||||||
(
|
(
|
||||||
@ -281,3 +283,24 @@ class ElasticVectorSearch(VectorStore, ABC):
|
|||||||
texts, metadatas=metadatas, refresh_indices=refresh_indices
|
texts, metadatas=metadatas, refresh_indices=refresh_indices
|
||||||
)
|
)
|
||||||
return vectorsearch
|
return vectorsearch
|
||||||
|
|
||||||
|
def create_index(self, client: Any, index_name: str, mapping: Dict) -> None:
|
||||||
|
version_num = client.info()["version"]["number"][0]
|
||||||
|
version_num = int(version_num)
|
||||||
|
if version_num >= 8:
|
||||||
|
client.indices.create(index=index_name, mappings=mapping)
|
||||||
|
else:
|
||||||
|
client.indices.create(index=index_name, body={"mappings": mapping})
|
||||||
|
|
||||||
|
def client_search(
|
||||||
|
self, client: Any, index_name: str, script_query: Dict, size: int
|
||||||
|
) -> Any:
|
||||||
|
version_num = client.info()["version"]["number"][0]
|
||||||
|
version_num = int(version_num)
|
||||||
|
if version_num >= 8:
|
||||||
|
response = client.search(index=index_name, query=script_query, size=size)
|
||||||
|
else:
|
||||||
|
response = client.search(
|
||||||
|
index=index_name, body={"query": script_query, "size": size}
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
Loading…
Reference in New Issue
Block a user