From 161e0558ef35eed4c9124fc451642789716b8dca Mon Sep 17 00:00:00 2001 From: Chunlei Wu Date: Fri, 20 Oct 2023 10:19:46 -0700 Subject: [PATCH] feat: :sparkles: implement new filter query param RE: #296 filter no longer an alias for fields param --- biothings/web/query/builder.py | 8 +++++++- biothings/web/settings/default.py | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/biothings/web/query/builder.py b/biothings/web/query/builder.py index 436c3c6da..f3865d350 100644 --- a/biothings/web/query/builder.py +++ b/biothings/web/query/builder.py @@ -356,9 +356,15 @@ def apply_extras(self, search, options): # https://www.elastic.co/guide/en/elasticsearch/ # reference/current/index-modules.html + # Feature: filter + # apply extra filter (as query_string query) to filter results + # Ref: https://www.elastic.co/guide/en/elasticsearch/reference/8.10/query-dsl-bool-query.html + if options.filter: + search = search.filter("query_string", query=options.filter) + # Feature: post_filter # -- implementation using query string matching - # Ref: https://www.elastic.co/guide/en/elasticsearch/reference/8.1/filter-search-results.html#post-filter + # Ref: https://www.elastic.co/guide/en/elasticsearch/reference/8.10/filter-search-results.html#post-filter if options.post_filter: search = search.post_filter("query_string", query=options["post_filter"]) diff --git a/biothings/web/settings/default.py b/biothings/web/settings/default.py index 398836080..6bc73335c 100644 --- a/biothings/web/settings/default.py +++ b/biothings/web/settings/default.py @@ -98,7 +98,7 @@ "raw": {"type": bool, "default": False}, "rawquery": {"type": bool, "default": False}, # query builder stage - "_source": {"type": list, "max": 1000, "alias": ("fields", "field", "filter")}, + "_source": {"type": list, "max": 1000, "alias": ("fields", "field")}, "size": {"type": int, "max": 1000, "alias": "limit"}, # formatter stage "dotfield": {"type": bool, "default": False}, @@ -124,6 +124,9 @@ **{ "from": {"type": int, "max": 10000, "alias": "skip"}, "sort": {"type": list, "max": 10}, + # use to set extra filter, as a filter clause in a boolean query + "filter": {"type": str, "default": None}, + # use to set post_filter query, this one does not impact facets "post_filter": {"type": str, "default": None}, }, }, # for py3.9+, we can just use `|` operator like `COMMON_KWARGS.copy() | {...}`