Skip to content

Commit eb702c7

Browse files
committed
elastic
1 parent 2a5849a commit eb702c7

File tree

13 files changed

+234
-380
lines changed

13 files changed

+234
-380
lines changed

datasets/random-100/neighbours.jsonl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[0]
2+
[1]
3+
[2]
4+
[3]
5+
[4]
6+
[5]
7+
[6]
8+
[7]
9+
[8]
10+
[9]

engine/clients/client_factory.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
BaseUploader,
99
)
1010
from engine.clients.milvus import MilvusConfigurator, MilvusSearcher, MilvusUploader
11-
from engine.clients.qdrant import QdrantConfigurator, QdrantSearcher, QdrantUploader
11+
from engine.clients.elasticsearch.configure import ElasticConfigurator
12+
from engine.clients.elasticsearch.search import ElasticSearcher
13+
from engine.clients.elasticsearch.upload import ElasticUploader
14+
from engine.clients.qdrant import QdrantConfigurator, QdrantUploader, QdrantSearcher
1215
from engine.clients.weaviate import (
1316
WeaviateConfigurator,
1417
WeaviateSearcher,
@@ -19,18 +22,21 @@
1922
"qdrant": QdrantConfigurator,
2023
"weaviate": WeaviateConfigurator,
2124
"milvus": MilvusConfigurator,
25+
"elastic": ElasticConfigurator,
2226
}
2327

2428
ENGINE_UPLOADERS = {
2529
"qdrant": QdrantUploader,
2630
"weaviate": WeaviateUploader,
2731
"milvus": MilvusUploader,
32+
"elastic": ElasticUploader,
2833
}
2934

3035
ENGINE_SEARCHERS = {
3136
"qdrant": QdrantSearcher,
3237
"weaviate": WeaviateSearcher,
3338
"milvus": MilvusSearcher,
39+
"elastic": ElasticSearcher,
3440
}
3541

3642

engine/elasticsearch-8.3.1/client/config.py renamed to engine/clients/elasticsearch/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import os
2-
3-
ELASTIC_HOST = os.environ.get("SERVER_HOST", "elastic_server")
41
ELASTIC_PORT = 9200
52
ELASTIC_INDEX = "bench"
63
ELASTIC_USER = "elastic"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from elasticsearch import Elasticsearch, NotFoundError
2+
3+
from engine.base_client.configure import BaseConfigurator
4+
from engine.base_client.distances import Distance
5+
from engine.clients.elasticsearch import ELASTIC_USER, ELASTIC_PASSWORD, ELASTIC_PORT, ELASTIC_INDEX
6+
7+
8+
class ElasticConfigurator(BaseConfigurator):
9+
DISTANCE_MAPPING = {
10+
Distance.L2_SQUARED: "l2_norm",
11+
Distance.COSINE: "cosine",
12+
Distance.DOT: "dot_product",
13+
}
14+
15+
def __init__(self, host, collection_params: dict, connection_params: dict):
16+
super().__init__(host, collection_params, connection_params)
17+
18+
self.client = Elasticsearch(
19+
f"http://{host}:{ELASTIC_PORT}",
20+
basic_auth=(ELASTIC_USER, ELASTIC_PASSWORD),
21+
**{
22+
**{
23+
"verify_certs": False,
24+
"request_timeout": 90,
25+
"retry_on_timeout": True,
26+
},
27+
**connection_params
28+
}
29+
)
30+
31+
def clean(self):
32+
try:
33+
self.client.indices.delete(index=ELASTIC_INDEX)
34+
except NotFoundError:
35+
pass
36+
37+
def recreate(
38+
self, distance, vector_size, collection_params,
39+
):
40+
self.client.indices.create(
41+
index=ELASTIC_INDEX,
42+
mappings={
43+
"properties": {
44+
"vector": {
45+
"type": "dense_vector",
46+
"dims": vector_size,
47+
"index": True,
48+
"similarity": self.DISTANCE_MAPPING[distance],
49+
"index_options": {
50+
**{
51+
"type": "hnsw",
52+
"m": 16,
53+
"ef_construction": 100,
54+
},
55+
**collection_params.get("index_options"),
56+
},
57+
}
58+
}
59+
}
60+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import uuid
2+
from typing import Tuple, List
3+
4+
from elasticsearch import Elasticsearch
5+
6+
from engine.base_client.search import BaseSearcher
7+
from engine.clients.elasticsearch import ELASTIC_PORT, ELASTIC_USER, ELASTIC_PASSWORD, ELASTIC_INDEX
8+
9+
10+
class ElasticSearcher(BaseSearcher):
11+
search_params = {}
12+
client: Elasticsearch = None
13+
14+
@classmethod
15+
def init_client(cls, host, connection_params: dict, search_params: dict):
16+
cls.client: Elasticsearch = Elasticsearch(
17+
f"http://{host}:{ELASTIC_PORT}",
18+
basic_auth=(ELASTIC_USER, ELASTIC_PASSWORD),
19+
**{
20+
**{
21+
"verify_certs": False,
22+
"request_timeout": 90,
23+
"retry_on_timeout": True,
24+
},
25+
**connection_params
26+
}
27+
)
28+
cls.search_params = search_params
29+
30+
@classmethod
31+
def search_one(cls, vector, meta_conditions, top) -> List[Tuple[int, float]]:
32+
res = cls.client.knn_search(
33+
index=ELASTIC_INDEX,
34+
knn={
35+
"field": "vector",
36+
"query_vector": vector,
37+
"k": 10,
38+
**{
39+
"num_candidates": 100,
40+
**cls.search_params
41+
}
42+
},
43+
)
44+
return [(uuid.UUID(hex=hit["_id"]).int, hit["_score"]) for hit in res["hits"]["hits"]]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import uuid
2+
from typing import Optional, List
3+
4+
from elasticsearch import Elasticsearch
5+
6+
from engine.base_client.upload import BaseUploader
7+
from engine.clients.elasticsearch import ELASTIC_PORT, ELASTIC_USER, ELASTIC_PASSWORD, ELASTIC_INDEX
8+
9+
10+
class ElasticUploader(BaseUploader):
11+
client = None
12+
upload_params = {}
13+
14+
@classmethod
15+
def init_client(cls, host, connection_params, upload_params):
16+
cls.client: Elasticsearch = Elasticsearch(
17+
f"http://{host}:{ELASTIC_PORT}",
18+
basic_auth=(ELASTIC_USER, ELASTIC_PASSWORD),
19+
**{
20+
**{
21+
"verify_certs": False,
22+
"request_timeout": 90,
23+
"retry_on_timeout": True,
24+
},
25+
**connection_params
26+
}
27+
)
28+
cls.upload_params = upload_params
29+
30+
@classmethod
31+
def upload_batch(
32+
cls, ids: List[int], vectors: List[list], metadata: Optional[List[dict]]
33+
):
34+
operations = []
35+
for idx, vector in zip(ids, vectors):
36+
vector_id = uuid.UUID(int=idx).hex
37+
38+
operations.append({"index": {"_id": vector_id}})
39+
operations.append({"vector": vector})
40+
41+
cls.client.bulk(
42+
index=ELASTIC_INDEX,
43+
operations=operations,
44+
)
45+
46+
@classmethod
47+
def post_upload(cls):
48+
cls.client.indices.forcemerge(
49+
index=ELASTIC_INDEX,
50+
wait_for_completion=True
51+
)
52+
return {}

engine/elasticsearch-8.3.1/client.Dockerfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

engine/elasticsearch-8.3.1/client/cmd.py

Lines changed: 0 additions & 126 deletions
This file was deleted.

engine/elasticsearch-8.3.1/config.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

engine/servers/elasticsearch-single-node/docker-compose.yaml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ version: '3.5'
22

33
services:
44
es:
5-
image: docker.elastic.co/elasticsearch/elasticsearch:8.3.1
6-
volumes:
7-
- es_data:/usr/share/elasticsearch/data
5+
image: docker.elastic.co/elasticsearch/elasticsearch:8.3.3
86
environment:
97
ELASTIC_PASSWORD: "passwd"
108
KIBANA_PASSWORD: "passwd"
119
SERVER_SSL_ENABLED: "false"
1210
discovery.type: "single-node"
11+
xpack.security.enabled: "false"
1312
ports:
1413
- "9200:9200"
1514
- "9300:9300"
16-
networks:
17-
- elastic
18-
19-
networks:
20-
elastic:
21-
driver: bridge
22-
23-
volumes:
24-
es_data:

0 commit comments

Comments
 (0)