forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add elasticsearch online store - update method #63
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6a8546a
Adding elasticsearch online store creator
54dda0a
implement Elasticsearch online store update with vector embeddings
piket a9aa2d2
fix format
piket 9383ea9
add requirements
piket 50fd048
fix data type default
piket 7746f99
fix data type default
piket 2d38b7e
fix format
piket 02597e3
fix format
piket 0a230c4
minor refactoring
piket 53748e5
fix linting
piket 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
170 changes: 170 additions & 0 deletions
170
sdk/python/feast/expediagroup/vectordb/elasticsearch_online_store.py
This file contains 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,170 @@ | ||
import json | ||
import logging | ||
from datetime import datetime | ||
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple | ||
|
||
from bidict import bidict | ||
from elasticsearch import Elasticsearch | ||
from pydantic.typing import Literal | ||
|
||
from feast import Entity, FeatureView, RepoConfig | ||
from feast.infra.online_stores.online_store import OnlineStore | ||
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto | ||
from feast.protos.feast.types.Value_pb2 import Value as ValueProto | ||
from feast.repo_config import FeastConfigBaseModel | ||
from feast.types import ( | ||
Bool, | ||
Bytes, | ||
ComplexFeastType, | ||
FeastType, | ||
Float32, | ||
Float64, | ||
Int32, | ||
Int64, | ||
String, | ||
UnixTimestamp, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
TYPE_MAPPING = bidict( | ||
{ | ||
Bytes: "binary", | ||
Int32: "integer", | ||
Int64: "long", | ||
Float32: "float", | ||
Float64: "double", | ||
Bool: "boolean", | ||
String: "text", | ||
UnixTimestamp: "date_nanos", | ||
} | ||
) | ||
|
||
|
||
class ElasticsearchOnlineStoreConfig(FeastConfigBaseModel): | ||
"""Online store config for the Elasticsearch online store""" | ||
|
||
type: Literal["elasticsearch"] = "elasticsearch" | ||
"""Online store type selector""" | ||
|
||
endpoint: str | ||
""" the http endpoint URL """ | ||
|
||
username: str | ||
""" username to connect to Elasticsearch """ | ||
|
||
password: str | ||
""" password to connect to Elasticsearch """ | ||
|
||
token: str | ||
""" bearer token for authentication """ | ||
|
||
|
||
class ElasticsearchConnectionManager: | ||
def __init__(self, online_config: RepoConfig): | ||
self.online_config = online_config | ||
|
||
def __enter__(self): | ||
# Connecting to Elasticsearch | ||
logger.info( | ||
f"Connecting to Elasticsearch with endpoint {self.online_config.endpoint}" | ||
) | ||
if len(self.online_config.token) > 0: | ||
self.client = Elasticsearch( | ||
self.online_config.endpoint, bearer_auth=self.online_config.token | ||
) | ||
else: | ||
self.client = Elasticsearch( | ||
self.online_config.endpoint, | ||
basic_auth=(self.online_config.username, self.online_config.password), | ||
) | ||
return self.client | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
# Disconnecting from Elasticsearch | ||
logger.info("Closing the connection to Elasticsearch") | ||
self.client.transport.close() | ||
|
||
|
||
class ElasticsearchOnlineStore(OnlineStore): | ||
def online_write_batch( | ||
self, | ||
config: RepoConfig, | ||
table: FeatureView, | ||
data: List[ | ||
Tuple[EntityKeyProto, Dict[str, ValueProto], datetime, Optional[datetime]] | ||
], | ||
progress: Optional[Callable[[int], Any]], | ||
) -> None: | ||
with ElasticsearchConnectionManager(config): | ||
pass | ||
|
||
def online_read( | ||
self, | ||
config: RepoConfig, | ||
table: FeatureView, | ||
entity_keys: List[EntityKeyProto], | ||
requested_features: Optional[List[str]] = None, | ||
) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]: | ||
pass | ||
|
||
def update( | ||
self, | ||
config: RepoConfig, | ||
tables_to_delete: Sequence[FeatureView], | ||
tables_to_keep: Sequence[FeatureView], | ||
entities_to_delete: Sequence[Entity], | ||
entities_to_keep: Sequence[Entity], | ||
partial: bool, | ||
): | ||
with ElasticsearchConnectionManager(config) as es: | ||
for fv in tables_to_delete: | ||
resp = es.indices.exists(index=fv.name) | ||
if resp.body: | ||
es.indices.delete(index=fv.name) | ||
for fv in tables_to_keep: | ||
resp = es.indices.exists(index=fv.name) | ||
if not resp.body: | ||
self._create_index(es, fv) | ||
|
||
def teardown( | ||
self, | ||
config: RepoConfig, | ||
tables: Sequence[FeatureView], | ||
entities: Sequence[Entity], | ||
): | ||
pass | ||
|
||
def _create_index(self, es, fv): | ||
index_mapping = {"properties": {}} | ||
for feature in fv.schema: | ||
is_primary = True if feature.name in fv.join_keys else False | ||
if "index_type" in feature.tags: | ||
dimensions = int(feature.tags.get("dimensions", "0")) | ||
metric_type = feature.tags.get("metric_type", "l2_norm") | ||
index_mapping["properties"][feature.name] = { | ||
"type": "dense_vector", | ||
"dims": dimensions, | ||
"index": True, | ||
"similarity": metric_type, | ||
} | ||
index_params = json.loads(feature.tags.get("index_params", "{}")) | ||
if len(index_params) > 0: | ||
index_params["type"] = feature.tags.get( | ||
"index_type", "hnsw" | ||
).lower() | ||
index_mapping["properties"][feature.name][ | ||
"index_options" | ||
] = index_params | ||
else: | ||
t = self._get_data_type(feature.dtype) | ||
t = "keyword" if is_primary and t == "text" else t | ||
index_mapping["properties"][feature.name] = {"type": t} | ||
if is_primary: | ||
index_mapping["properties"][feature.name]["index"] = True | ||
es.indices.create(index=fv.name, mappings=index_mapping) | ||
|
||
def _get_data_type(self, t: FeastType) -> str: | ||
if isinstance(t, ComplexFeastType): | ||
return "text" | ||
return TYPE_MAPPING.get(t, "text") |
This file contains 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 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 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
36 changes: 36 additions & 0 deletions
36
sdk/python/tests/expediagroup/elasticsearch_online_store_creator.py
This file contains 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,36 @@ | ||
import logging | ||
|
||
from testcontainers.elasticsearch import ElasticSearchContainer | ||
|
||
from tests.integration.feature_repos.universal.online_store_creator import ( | ||
OnlineStoreCreator, | ||
) | ||
|
||
# Configure logging | ||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ElasticsearchOnlineCreator(OnlineStoreCreator): | ||
def __init__(self, project_name: str, es_port: int): | ||
super().__init__(project_name) | ||
self.elasticsearch_container = ElasticSearchContainer( | ||
image="docker.elastic.co/elasticsearch/elasticsearch:8.8.2", | ||
port_to_expose=es_port, | ||
) | ||
|
||
def create_online_store(self): | ||
# Start the container | ||
self.elasticsearch_container.start() | ||
elasticsearch_host = self.elasticsearch_container.get_container_host_ip() | ||
elasticsearch_http_port = self.elasticsearch_container.get_exposed_port(9200) | ||
return { | ||
"host": elasticsearch_host, | ||
"port": elasticsearch_http_port, | ||
"username": "", | ||
"password": "", | ||
"token": "", | ||
} | ||
|
||
def teardown(self): | ||
self.elasticsearch_container.stop() |
Oops, something went wrong.
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.
move this to the bottom of the file