Added a query method for OpenSearch vector db.

- This PR aims to address the error 400: "**'OpenSearchClient' object has no attribute 'query'**".
- With the implemented query() method, this issue should be resolved and allow uploaded documents to be vectorized and retrieved based on the given query.
This commit is contained in:
Vineeth B V 2025-02-06 12:04:14 +05:30 committed by GitHub
parent e9d6ada25c
commit fd6b039859
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -113,6 +113,40 @@ class OpenSearchClient:
return self._result_to_search_result(result)
def query(
self, index_name: str, filter: dict, limit: Optional[int] = None
) -> Optional[GetResult]:
if not self.has_collection(index_name):
return None
query_body = {
"query": {
"bool": {
"filter": []
}
},
"_source": ["text", "metadata"],
}
for field, value in filter.items():
query_body["query"]["bool"]["filter"].append({
"term": {field: value}
})
size = limit if limit else 10
try:
result = self.client.search(
index=f"{self.index_prefix}_{index_name}",
body=query_body,
size=size
)
return self._result_to_get_result(result)
except Exception as e:
return None
def get_or_create_index(self, index_name: str, dimension: int):
if not self.has_index(index_name):
self._create_index(index_name, dimension)