Files
qdrant-client/qdrant_client/local/payload_value_extractor.py
George 9e61a69652 Drop python3.9 (#1110)
* new: remove vectors_count, update http and grpc models

* fix: update inspection cache

* new: add conversions and update interface

* fix: fix some conversions

* fix: fix typo

* fix: fix isinstance

* fix: regen async

* fix: fix update_filter usage, fix isinstance

* tests: collection metadata test

* fix: address backward compatibility in test

* new: update models, add max payload index count and copy vectors

* fix; update _inspection_cache

* new: add read consistency to count points

* Allow uuids in interface (#1085)

* new: direct uuid support

* tests: add uuid tests

* fix: update inspection cache

* new: add collection metadata and tests to local mode (#1089)

* new: add collection metadata and tests to local mode

* fix: regen async client

* new: implement parametrized rrf in local mode (#1087)

* new: implement parametrized rrf in local mode

* refactoring: use a variable for a magic value

* fix: adjust conversion according to AI

* Update filter (#1090)

* new: add missing update_filter, implement it in local mode

* fix: fix type hint, fix update operation, fix rest uploader, add tests

* fix: fix update filter is None case

* fix: mypy was not a good boy

* Text any filter (#1091)

* new: add match text any local mode

* tests: add match text any tests

* new: update models, remove init_from and locks (#1100)

* new: update models, remove init_from and locks

* deprecate: remove init from tests

* deprecate: remove lock tests

* new: convert ascii_folding

* fix: fix type stub

* new: convert acorn

* new: convert shard key with fallback

* new: update grpcio and grpcio tools in generator (#1106)

* new: update grpcio and grpcio tools in generator

* fix: bind grpcio and tools versions to 1.62.0 in generator

* Remove deprecated methods (#1103)

* deprecate: remove old api methods

* deprecate: remove type stub for removed methods

* deprecate: remove old api methods from test_qdrant_client

* deprecate: replace search with query points in test_in_memory

* deprecate: replace search methods in fastembed mixin with query points

* deprecate: replace old api methods in test async qdrant client

* deprecate: replace search with query points in test delete points

* deprecate: replace discover and context with query points in test_discovery

* deprecate: replace recommend_groups with query_points_groups in test_group_recommend

* deprecate: replace search_groups in test_group_search

* deprecate: replace recommend with query points in test_recommendation

* deprecate: replace search with query points in test search

* deprecate: replace context and discover with query points in test sparse discovery

* deprecate: replace search with query points in test sparse idf search

* deprecate: replace recommend with query points in test sparse recommend

* deprecate: replace search with query points in test sparse search

* deprecate: replace missing search request with query request in qdrant_fastembed

* deprecate: replace search with query points in test multivector search queries

* deprecate: replace upload records with upload points in test_updates

* deprecate: remove redundant structs (#1104)

* deprecate: remove redundant structs

* fix: do not use removed conversions in local mode

* fix: remove redundant conversions, simplify types.QueryRequest

* deprecate: replace old style grpc vector conversion to a new one (#1105)

* deprecate: replace old style grpc vector conversion to a new one

* fix: ignore union attr in conversion

* review fixes

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

* new: deprecate add, query, query_batch in fastembed mixin (#1102)

* new: deprecate add, query, query_batch in fastembed mixin

* 1.16 -> 1.17

---------

Co-authored-by: generall <andrey@vasnetsov.com>

---------

Co-authored-by: generall <andrey@vasnetsov.com>

* new: yet another update

* new: add initial_state to create shard key (#1109)

* new: drop python3.9, replace union and optional with | where possible

* fix: fix missing type hints, regen async

* fix: remove redundant optional

* fix: fix ai comments

* fix: update type hints from merge

* new: update pyproject and lock

* new: replace optional and union with |

* new: remove optional and union from qdrant local

* new: replace union with | in client classes

* fix: replace remaining union, optional, etc, address review comments

* new: adjust numpy versioning

---------

Co-authored-by: generall <andrey@vasnetsov.com>
2025-12-12 17:21:08 +07:00

93 lines
2.8 KiB
Python

import uuid
from typing import Any
from qdrant_client.local.json_path_parser import (
JsonPathItem,
JsonPathItemType,
parse_json_path,
)
def value_by_key(payload: dict[str, Any], key: str, flat: bool = True) -> list[Any] | None:
"""
Get value from payload by key.
Args:
payload: arbitrary json-like object
flat: If True, extend list of values. If False, append. By default, we use True and flatten the arrays,
we need it for filters, however for `count` method we need to keep the arrays as is.
key:
Key or path to value in payload.
Examples:
- "name"
- "address.city"
- "location[].name"
- "location[0].name"
Returns:
List of values or None if key not found.
"""
keys = parse_json_path(key)
result = []
def _get_value(data: Any, k_list: list[JsonPathItem]) -> None:
if not k_list:
return
current_key = k_list.pop(0)
if len(k_list) == 0:
if isinstance(data, dict) and current_key.item_type == JsonPathItemType.KEY:
if current_key.key in data:
value = data[current_key.key]
if isinstance(value, list) and flat:
result.extend(value)
else:
result.append(value)
elif isinstance(data, list):
if current_key.item_type == JsonPathItemType.WILDCARD_INDEX:
result.extend(data)
elif current_key.item_type == JsonPathItemType.INDEX:
assert current_key.index is not None
if current_key.index < len(data):
result.append(data[current_key.index])
elif current_key.item_type == JsonPathItemType.KEY:
if not isinstance(data, dict):
return
if current_key.key in data:
_get_value(data[current_key.key], k_list.copy())
elif current_key.item_type == JsonPathItemType.INDEX:
assert current_key.index is not None
if not isinstance(data, list):
return
if current_key.index < len(data):
_get_value(data[current_key.index], k_list.copy())
elif current_key.item_type == JsonPathItemType.WILDCARD_INDEX:
if not isinstance(data, list):
return
for item in data:
_get_value(item, k_list.copy())
_get_value(payload, keys)
return result if result else None
def parse_uuid(value: Any) -> uuid.UUID | None:
"""
Parse UUID from value.
Args:
value: arbitrary value
"""
try:
return uuid.UUID(str(value))
except ValueError:
return None