Skip to content
Merged
Show file tree
Hide file tree
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
67 changes: 33 additions & 34 deletions playground/python/app/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,47 @@
def main():
print("SearchClient version", __version__)

# client = SearchClientSync(
client = SearchClientSync(
environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
)
client.add_user_agent("playground")
client.add_user_agent("bar", "baz")

print("user_agent", client._config._user_agent.get())
print("client initialized", client)

try:
resp = client.search_single_index("poussing-RECORDS")
print(resp.to_dict())
finally:
client.close()

print("client closed")

# print("with transformations")
#
# config = SearchConfig(
# environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
# )
# client.add_user_agent("playground")
# client.add_user_agent("bar", "baz")
#
# config.set_transformation_region("eu")
#
# print("region in playground")
# print(config.region)
#
# client = SearchClientSync.create_with_config(config)
# client.add_user_agent("playground search with ingestion")
#
# print("user_agent", client._config._user_agent.get())
# print("client initialized", client)
#
# try:
# resp = client.search_synonyms("foo")
# resp = client.replace_all_objects_with_transformation(
# "boyd", [{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"}], 2
# )
# print(resp)
# client.browse_synonyms("foo", lambda _resp: print(_resp))
# except Exception as e:
# print(e)
# finally:
# client.close()
#
# print("client closed")
#
# print("with transformations")
#
config = SearchConfig(
environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
)

config.set_transformation_region("eu")

print("region in playground")
print(config.region)

client = SearchClientSync.create_with_config(config)
client.add_user_agent("playground search with ingestion")

print("user_agent", client._config._user_agent.get())

try:
resp = client.replace_all_objects_with_transformation(
"boyd", [{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"}], 2
)
print(resp)
except Exception as e:
print(e)
finally:
client.close()

print("client closed")
2 changes: 1 addition & 1 deletion playground/python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions specs/search/common/schemas/Hit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ hit:

A hit is a record from your index, augmented with special attributes for highlighting, snippeting, and ranking.
x-is-generic: true
x-is-hit-object: true
additionalProperties: true
required:
- objectID
Expand Down
1 change: 1 addition & 0 deletions templates/python/imports.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from pydantic import (
StrictStr,
ValidationError,
field_validator,
field_serializer,
model_serializer,
)
from typing import (
Expand Down
35 changes: 35 additions & 0 deletions templates/python/model_generic.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
extra='allow',
)

{{#vendorExtensions.x-is-hit-object}}
@staticmethod
def __dump_item(item):
return item.model_dump(
by_alias=True,
exclude_none=True,
exclude_unset=True,
warnings="none",
) if isinstance(item, BaseModel) else item

@field_serializer("highlight_result")
def serialize_highlight_result(self, v: HighlightResult | None):
if v is None:
return None

if isinstance(v, dict):
return {k: self.__dump_item(val) for k, val in v.items()}
elif isinstance(v, list):
return [self.__dump_item(val) for val in v]
else:
return self.__dump_item(v)

@field_serializer("snippet_result")
def serialize_snippet_result(self, v: SnippetResult | None):
if v is None:
return None

if isinstance(v, dict):
return {k: self.__dump_item(val) for k, val in v.items()}
elif isinstance(v, list):
return [self.__dump_item(val) for val in v]
else:
return self.__dump_item(v)
{{/vendorExtensions.x-is-hit-object}}

def to_json(self) -> str:
return self.model_dump_json(by_alias=True, exclude_unset=True)

Expand Down