-
Notifications
You must be signed in to change notification settings - Fork 280
Add TencentES client #623
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
Open
morning-color
wants to merge
9
commits into
zilliztech:main
Choose a base branch
from
morning-color:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add TencentES client #623
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
686c749
add tencent es
7aede6d
support tencent es
ba5d44f
Add support for TencentElasticsearch
5a1c857
Merge branch 'zilliztech:main' into main
KANIOYH 32ab47d
Fix argument order in Elasticsearch config
morning-color 85f1432
Add Tencent ES installation command to README
morning-color 33a17be
Fix tencent_es installation command in README
morning-color 14607ad
Merge branch 'main' into main
morning-color 35d77a2
Extends TencentElasticsearch client from ElasticCloud
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ def __hash__(self) -> int: | |
| self.use_routing, | ||
| self.efConstruction, | ||
| self.M, | ||
| 2, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
96 changes: 96 additions & 0 deletions
96
vectordb_bench/backend/clients/tencent_elasticsearch/cli.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import os | ||
| from typing import Annotated, Unpack | ||
|
|
||
| import click | ||
| from pydantic import SecretStr | ||
|
|
||
| from vectordb_bench.backend.clients import DB | ||
| from vectordb_bench.cli.cli import ( | ||
| CommonTypedDict, | ||
| cli, | ||
| click_parameter_decorators_from_typed_dict, | ||
| run, | ||
| ) | ||
|
|
||
|
|
||
| class TencentElasticsearchTypedDict(CommonTypedDict): | ||
| scheme: Annotated[ | ||
| str, | ||
| click.option( | ||
| "--scheme", | ||
| type=str, | ||
| help="Protocol in use to connect to the node", | ||
| default="http", | ||
| show_default=True, | ||
| ), | ||
| ] | ||
| host: Annotated[ | ||
| str, | ||
| click.option("--host", type=str, help="shot connection string", required=True), | ||
| ] | ||
| port: Annotated[ | ||
| int, | ||
| click.option("--port", type=int, help="Port to connect to", default=9200, show_default=True), | ||
| ] | ||
| user: Annotated[ | ||
| str, | ||
| click.option("--user", type=str, help="Db username", required=True), | ||
| ] | ||
| password: Annotated[ | ||
| str, | ||
| click.option( | ||
| "--password", | ||
| type=str, | ||
| help="TencentElasticsearch password", | ||
| default=lambda: os.environ.get("TES_PASSWORD", ""), | ||
| show_default="$TES_PASSWORD", | ||
| ), | ||
| ] | ||
| m: Annotated[ | ||
| int, | ||
| click.option("--m", type=int, help="HNSW M parameter", default=16, show_default=True), | ||
| ] | ||
| ef_construction: Annotated[ | ||
| int, | ||
| click.option( | ||
| "--ef_construction", | ||
| type=int, | ||
| help="HNSW efConstruction parameter", | ||
| default=200, | ||
| show_default=True, | ||
| ), | ||
| ] | ||
| num_candidates: Annotated[ | ||
| int, | ||
| click.option( | ||
| "--num_candidates", | ||
| type=int, | ||
| help="Number of candidates to consider during searching", | ||
| default=200, | ||
| show_default=True, | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @cli.command() | ||
| @click_parameter_decorators_from_typed_dict(TencentElasticsearchTypedDict) | ||
| def TencentElasticsearch(**parameters: Unpack[TencentElasticsearchTypedDict]): | ||
| from .config import TencentElasticsearchConfig, TencentElasticsearchIndexConfig | ||
|
|
||
| run( | ||
| db=DB.TencentElasticsearch, | ||
| db_config=TencentElasticsearchConfig( | ||
| db_label=parameters["db_label"], | ||
| scheme=parameters["scheme"], | ||
| host=parameters["host"], | ||
| port=parameters["port"], | ||
| user=parameters["user"], | ||
| password=SecretStr(parameters["password"]), | ||
| ), | ||
| db_case_config=TencentElasticsearchIndexConfig( | ||
| M=parameters["m"], | ||
| efConstruction=parameters["ef_construction"], | ||
| num_candidates=parameters["num_candidates"], | ||
| ), | ||
| **parameters, | ||
| ) | ||
92 changes: 92 additions & 0 deletions
92
vectordb_bench/backend/clients/tencent_elasticsearch/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| from enum import Enum | ||
|
|
||
| from pydantic import BaseModel, SecretStr | ||
|
|
||
| from ..api import DBCaseConfig, DBConfig, IndexType, MetricType | ||
|
|
||
|
|
||
| class TencentElasticsearchConfig(DBConfig, BaseModel): | ||
| #: Protocol in use to connect to the node | ||
| scheme: str = "http" | ||
| host: str = "" | ||
| port: int = 9200 | ||
| user: str = "elastic" | ||
| password: SecretStr | ||
|
|
||
| def to_dict(self) -> dict: | ||
| return { | ||
| "hosts": [{"scheme": self.scheme, "host": self.host, "port": self.port}], | ||
| "basic_auth": (self.user, self.password.get_secret_value()), | ||
| } | ||
|
|
||
|
|
||
| class ESElementType(str, Enum): | ||
| float = "float" # 4 byte | ||
| byte = "byte" # 1 byte, -128 to 127 | ||
|
|
||
|
|
||
| class TencentElasticsearchIndexConfig(BaseModel, DBCaseConfig): | ||
| element_type: ESElementType = ESElementType.float | ||
| index: IndexType = IndexType.TES_VSEARCH | ||
| number_of_shards: int = 1 | ||
| number_of_replicas: int = 0 | ||
| refresh_interval: str = "3s" | ||
| merge_max_thread_count: int = 8 | ||
| use_rescore: bool = False | ||
| oversample_ratio: float = 2.0 | ||
| use_routing: bool = False | ||
| use_force_merge: bool = True | ||
|
|
||
| metric_type: MetricType | None = None | ||
| efConstruction: int | None = None | ||
| M: int | None = None | ||
| num_candidates: int | None = None | ||
|
|
||
| def __eq__(self, obj: any): | ||
| return ( | ||
| self.index == obj.index | ||
| and self.number_of_shards == obj.number_of_shards | ||
| and self.number_of_replicas == obj.number_of_replicas | ||
| and self.use_routing == obj.use_routing | ||
| and self.efConstruction == obj.efConstruction | ||
| and self.M == obj.M | ||
| ) | ||
|
|
||
| def __hash__(self) -> int: | ||
| return hash( | ||
| ( | ||
| self.index, | ||
| self.number_of_shards, | ||
| self.number_of_replicas, | ||
| self.use_routing, | ||
| self.efConstruction, | ||
| self.M, | ||
| 2, | ||
| ) | ||
| ) | ||
|
|
||
| def parse_metric(self) -> str: | ||
| if self.metric_type == MetricType.L2: | ||
| return "l2_norm" | ||
| if self.metric_type == MetricType.IP: | ||
| return "dot_product" | ||
| return "cosine" | ||
|
|
||
| def index_param(self) -> dict: | ||
| return { | ||
| "type": "dense_vector", | ||
| "index": True, | ||
| "element_type": self.element_type.value, | ||
| "similarity": self.parse_metric(), | ||
| "index_options": { | ||
| "type": self.index.value, | ||
| "index": "hnsw", | ||
| "m": self.M, | ||
| "ef_construction": self.efConstruction, | ||
| }, | ||
| } | ||
|
|
||
| def search_param(self) -> dict: | ||
| return { | ||
| "num_candidates": self.num_candidates, | ||
| } |
53 changes: 53 additions & 0 deletions
53
vectordb_bench/backend/clients/tencent_elasticsearch/tencent_elasticsearch.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import logging | ||
| import time | ||
| from contextlib import contextmanager | ||
|
|
||
| from vectordb_bench.backend.filter import Filter, FilterOp | ||
|
|
||
| from ..elastic_cloud.elastic_cloud import ElasticCloud | ||
| from .config import TencentElasticsearchIndexConfig | ||
|
|
||
| for logger in ("elasticsearch", "elastic_transport"): | ||
| logging.getLogger(logger).setLevel(logging.WARNING) | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| SECONDS_WAITING_FOR_FORCE_MERGE_API_CALL_SEC = 30 | ||
|
|
||
|
|
||
| class TencentElasticsearch(ElasticCloud): | ||
| supported_filter_types: list[FilterOp] = [ | ||
| FilterOp.NonFilter, | ||
| FilterOp.NumGE, | ||
| FilterOp.StrEqual, | ||
| ] | ||
|
|
||
| @contextmanager | ||
| def init(self) -> None: | ||
| """connect to elasticsearch""" | ||
| from elasticsearch import Elasticsearch | ||
|
|
||
| self.client = Elasticsearch(**self.db_config, request_timeout=1800) | ||
|
|
||
| yield | ||
| self.client = None | ||
| del self.client | ||
|
|
||
| def optimize(self, data_size: int | None = None): | ||
| """optimize will be called between insertion and search in performance cases.""" | ||
| assert self.client is not None, "should self.init() first" | ||
| self.client.indices.refresh(index=self.indice) | ||
| time.sleep(SECONDS_WAITING_FOR_FORCE_MERGE_API_CALL_SEC) | ||
| if self.case_config.use_force_merge: | ||
| force_merge_task_id = self.client.indices.forcemerge( | ||
| index=self.indice, | ||
| max_num_segments=1, | ||
| wait_for_completion=False, | ||
| )["task"] | ||
| log.info(f"Elasticsearch force merge task id: {force_merge_task_id}") | ||
| while True: | ||
| time.sleep(SECONDS_WAITING_FOR_FORCE_MERGE_API_CALL_SEC) | ||
| task_status = self.client.tasks.get(task_id=force_merge_task_id) | ||
| if task_status["completed"]: | ||
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if this CLI tool could support
ElasticCloud,AliyunES, andTencentES. Otherwise, no rush—I can handle it in a follow-up PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made the TencentElasticsearch client class directly inherit from the ElasticCloud class.
Sorry, I haven't made changes to the CLI tool because I don't have test resources for ElasticCloud or AliyunES, and I'm unsure if the modifications will work properly. Could you merge my PR first, and then merge the CLI tool changes later?