Skip to content

Commit f65f4fe

Browse files
committed
Search index: add nested filter.
1 parent 2b219a0 commit f65f4fe

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

examples/search_index.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ def term_query(table_name, index_name):
5959
_print_rows(rows, total_count)
6060

6161
def range_query(table_name, index_name):
62-
query = RangeQuery('k', 'key100', 'key200', include_lower=False, include_upper=False)
62+
query = RangeQuery('k', 'key100', 'key500', include_lower=False, include_upper=False)
6363
rows, next_token, total_count, is_all_succeed = client.search(
64-
table_name, index_name, SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL)
64+
table_name, index_name, SearchQuery(query, offset=100, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL)
6565
)
6666

6767
_print_rows(rows, total_count)
@@ -121,8 +121,11 @@ def bool_query(table_name, index_name):
121121

122122
def geo_distance_query(table_name, index_name):
123123
query = GeoDistanceQuery('g', '32.5,116.5', 300000)
124+
sort = Sort(sorters=[
125+
GeoDistanceSort('g', ['32.5,116.5', '32.0,116.0'], sort_order=SortOrder.DESC, sort_mode=SortMode.MAX)
126+
])
124127
rows, next_token, total_count, is_all_succeed = client.search(
125-
table_name, index_name, SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL)
128+
table_name, index_name, SearchQuery(query, limit=100, get_total_count=True, sort=sort), ColumnsToGet(return_type=ColumnReturnType.ALL)
126129
)
127130

128131
_print_rows(rows, total_count)
@@ -144,10 +147,13 @@ def geo_polygon_query(table_name, index_name):
144147
_print_rows(rows, total_count)
145148

146149
def nested_query(table_name, index_name):
147-
nested_query = RangeQuery('n.nl', range_from=100, range_to=300, include_lower=True, include_upper=True)
150+
nested_query = RangeQuery('n.nl', range_from=110, range_to=200, include_lower=True, include_upper=True)
148151
query = NestedQuery('n', nested_query)
152+
sort = Sort(
153+
sorters = [FieldSort('n.nl', sort_order=SortOrder.ASC, nested_filter=NestedFilter('n', RangeQuery('n.nl', range_from=150, range_to=200)))]
154+
)
149155
rows, next_token, total_count, is_all_succeed = client.search(
150-
table_name, index_name, SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL)
156+
table_name, index_name, SearchQuery(query, limit=100, get_total_count=True, sort=sort), ColumnsToGet(return_type=ColumnReturnType.ALL)
151157
)
152158

153159
_print_rows(rows, total_count)
@@ -244,7 +250,7 @@ def delete_search_index(index_name):
244250
#describe_search_index()
245251

246252
# perform queries
247-
match_all_query(table_name, index_name)
253+
#match_all_query(table_name, index_name)
248254
#match_query(table_name, index_name)
249255
#match_phrase_query(table_name, index_name)
250256
#term_query(table_name, index_name)
@@ -256,6 +262,6 @@ def delete_search_index(index_name):
256262
#geo_distance_query(table_name, index_name)
257263
#geo_bounding_box_query(table_name, index_name)
258264
#geo_polygon_query(table_name, index_name)
259-
#nested_query(table_name, nested_index_name)
265+
nested_query(table_name, nested_index_name)
260266
#function_score_query(table_name, nested_index_name)
261267

tablestore/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
'ColumnsToGet',
7979
'ColumnReturnType',
8080
'FieldValueFactor',
81+
'GeoDistanceType',
82+
'NestedFilter'
8183
]
8284

8385

tablestore/encoder.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,10 @@ def _make_index_sorter(self, proto, sorter):
402402
proto.field_sort.mode = self._get_enum(sorter.sort_mode)
403403

404404
if sorter.nested_filter is not None:
405-
proto.field_sort.nested_filter = self._encode_nested_filter(sorter.nested_filter)
405+
self._make_nested_filter(proto.field_sort.nested_filter, sorter.nested_filter)
406406
elif isinstance(sorter, GeoDistanceSort):
407407
proto.geo_distance_sort.field_name = sorter.field_name
408-
proto.geo_distance_sort.points = sorter.points
408+
proto.geo_distance_sort.points.extend(sorter.points)
409409

410410
if sorter.sort_order is not None:
411411
proto.geo_distance_sort.order = self._get_enum(sorter.sort_order)
@@ -417,7 +417,7 @@ def _make_index_sorter(self, proto, sorter):
417417
proto.geo_distance_sort.distance_type = self._get_enum(sorter.geo_distance_type)
418418

419419
if sorter.nested_filter is not None:
420-
proto.geo_distance_sort.nested_filter = self._encode_nested_filter(sorter.nested_filter)
420+
self._make_nested_filter(proto.geo_distance_sort.nested_filter, sorter.nested_filter)
421421
elif isinstance(sorter, ScoreSort):
422422
proto.score_sort.order = self._get_enum(sorter.sort_order)
423423
else:
@@ -828,12 +828,9 @@ def _encode_create_search_index(self, table_name, index_name, index_meta):
828828

829829
return proto
830830

831-
def _encode_nested_filter(self, nested_filter):
832-
nested_filter_proto = None
833-
if nested_filter:
834-
# TODO encode nested filter
835-
pass
836-
return nested_filter_proto
831+
def _make_nested_filter(self, proto, nested_filter):
832+
proto.path = nested_filter.path
833+
self._make_query(proto.filter, nested_filter.query_filter)
837834

838835
def _encode_search(self, table_name, index_name, search_query, columns_to_get, routing_keys):
839836
proto = search_pb2.SearchRequest()

tablestore/metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
'SyncPhase',
8080
'ColumnsToGet',
8181
'ColumnReturnType',
82+
'GeoDistanceType',
83+
'NestedFilter'
8284
]
8385

8486

0 commit comments

Comments
 (0)