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

feat: add elastic search wrapper #509

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions gptcache/manager/vector_data/elastic_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from typing import List

import numpy as np

from gptcache.manager.vector_data.base import VectorBase, VectorData
from gptcache.utils.log import gptcache_log
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import NotFoundError


class ElasticSearchStore(VectorBase):
def __init__(
self,
host: str = "localhost",
port: str = "9200",
username: str = "",
password: str = "",
dimension: int = 0,
collection_name: str = "gptcache",
top_k: int = 1,
namespace: str = "",
):
self._client = Elasticsearch("http://localhost:9200")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimFG @cxie @xiaofan-luan
I am currently running a local docker image of elastic search. What should be the hostname for the production use cases here?

Also, a point to note, I have used a http URL since, TLS was required to use a https with Elastic Search

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps through a variable, let the user pass the url parameter by himself. The default value of the parameter is: "http://localhost:9200"

self.top_k = top_k
self.dimension = dimension
self.collection_name = collection_name
self.namespace = namespace
self.doc_prefix = f"{self.namespace}doc:"
self.create_collection(collection_name)

def _create_collection(self, collection_name):
if self._check_index_exists(collection_name):
gptcache_log.info(
"The %s already exists, and it will be used directly", collection_name
)
else:
gptcache_log.info("Index does not exist")
mappings = {
"properties": {
"text": {"type": "text"},
"vector": {"type": "dense_vector", "dims": self.dimension},
}
}
self._client.indices.create(index=collection_name, mappings=mappings)

def _check_index_exists(self, index_name):
try:
return self._client.exists(index=index_name)
except NotFoundError:
return False

def mul_add(self, datas: List[VectorData]):
for data in datas:
id: int = data.id
doc = {
"_index": self.collection_name,
"_id": id,
"_source": {"vector": data.data.tolist()},
}

def search(self, data: np.ndarray, top_k: int = -1):
search_body: dict = {
"query": {
"script_score": {
"query": {"match_all": {}},
"script": {
"source": "cosineSimilarity(params.queryVector, 'vector') + 1.0",
"params": {"queryVector": data},
},
}
},
"size": top_k,
"sort": [{"_score": {"order": "desc"}}],
}

results = self._client.search(index=self.collection_name, body=search_body)
return [
(float(result["_score"]), int(result["_id"]))
for result in results["hits"]["hits"]
]

def rebuild(self, ids=None) -> bool:
pass

Comment on lines +82 to +84
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimFG @cxie @xiaofan-luan
do we need a rebuild method for Elastic Search?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the index can be updated as the data changes, rebuild does not need to be implemented. If not, say faiss, you need to implement

def delete(self, ids) -> None:
for id in ids:
self._client.delete(index=self.collection_name, id=id)
Loading