Skip to content

Commit 889edc9

Browse files
authored
Add body field parameters to 'search' API
1 parent 9834f69 commit 889edc9

File tree

16 files changed

+524
-135
lines changed

16 files changed

+524
-135
lines changed

docs/guide/examples.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The `search()` method returns results that are matching a query:
6767

6868
[source,py]
6969
----------------------------
70-
res = es.search(index="test-index", body={"query": {"match_all": {}}})
70+
res = es.search(index="test-index", query={"match_all": {}})
7171
print("Got %d Hits:" % res['hits']['total']['value'])
7272
for hit in res['hits']['hits']:
7373
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

docs/sphinx/async.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ and are used in the same way as other APIs, just with an extra ``await``:
3636
async def main():
3737
resp = await es.search(
3838
index="documents",
39-
body={"query": {"match_all": {}}},
39+
query={"match_all": {}},
4040
size=20,
4141
)
4242
print(resp)

docs/sphinx/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Example Usage
6161
6262
es.indices.refresh(index="test-index")
6363
64-
res = es.search(index="test-index", body={"query": {"match_all": {}}})
64+
res = es.search(index="test-index", query={"match_all": {}})
6565
print("Got %d Hits:" % res['hits']['total']['value'])
6666
for hit in res['hits']['hits']:
6767
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

elasticsearch/_async/client/__init__.py

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,39 @@ async def scroll(self, body=None, scroll_id=None, params=None, headers=None):
15611561
"track_total_hits",
15621562
"typed_keys",
15631563
"version",
1564+
body_params=[
1565+
"_source",
1566+
"aggregations",
1567+
"aggs",
1568+
"collapse",
1569+
"docvalue_fields",
1570+
"explain",
1571+
"fields",
1572+
"from_",
1573+
"highlight",
1574+
"indices_boost",
1575+
"min_score",
1576+
"pit",
1577+
"post_filter",
1578+
"profile",
1579+
"query",
1580+
"rescore",
1581+
"runtime_mappings",
1582+
"script_fields",
1583+
"search_after",
1584+
"seq_no_primary_term",
1585+
"size",
1586+
"slice",
1587+
"sort",
1588+
"stats",
1589+
"stored_fields",
1590+
"suggest",
1591+
"terminate_after",
1592+
"timeout",
1593+
"track_scores",
1594+
"track_total_hits",
1595+
"version",
1596+
],
15641597
)
15651598
async def search(
15661599
self, body=None, index=None, doc_type=None, params=None, headers=None
@@ -1575,12 +1608,13 @@ async def search(
15751608
`_all` or empty string to perform the operation on all indices
15761609
:arg doc_type: A comma-separated list of document types to
15771610
search; leave empty to perform the operation on all types
1578-
:arg _source: True or false to return the _source field or not,
1579-
or a list of fields to return
1611+
:arg _source:
15801612
:arg _source_excludes: A list of fields to exclude from the
15811613
returned _source field
15821614
:arg _source_includes: A list of fields to extract and return
15831615
from the _source field
1616+
:arg aggregations:
1617+
:arg aggs:
15841618
:arg allow_no_indices: Whether to ignore if a wildcard indices
15851619
expression resolves into no concrete indices. (This includes `_all`
15861620
string or when no indices have been specified)
@@ -1598,22 +1632,24 @@ async def search(
15981632
:arg ccs_minimize_roundtrips: Indicates whether network round-
15991633
trips should be minimized as part of cross-cluster search requests
16001634
execution Default: true
1635+
:arg collapse:
16011636
:arg default_operator: The default operator for query string
16021637
query (AND or OR) Valid choices: AND, OR Default: OR
16031638
:arg df: The field to use as default where no field prefix is
16041639
given in the query string
1605-
:arg docvalue_fields: A comma-separated list of fields to return
1606-
as the docvalue representation of a field for each hit
1640+
:arg docvalue_fields:
16071641
:arg expand_wildcards: Whether to expand wildcard expression to
16081642
concrete indices that are open, closed or both. Valid choices: open,
16091643
closed, hidden, none, all Default: open
1610-
:arg explain: Specify whether to return detailed information
1611-
about score computation as part of a hit
1612-
:arg from_: Starting offset (default: 0)
1644+
:arg explain:
1645+
:arg fields:
1646+
:arg from_:
1647+
:arg highlight:
16131648
:arg ignore_throttled: Whether specified concrete, expanded or
16141649
aliased indices should be ignored when throttled
16151650
:arg ignore_unavailable: Whether specified concrete indices
16161651
should be ignored when unavailable (missing or closed)
1652+
:arg indices_boost:
16171653
:arg lenient: Specify whether format-based query failures (such
16181654
as providing text to a numeric field) should be ignored
16191655
:arg max_concurrent_shard_requests: The number of concurrent
@@ -1623,6 +1659,9 @@ async def search(
16231659
:arg min_compatible_shard_node: The minimum compatible version
16241660
that all shards involved in search should have for this request to be
16251661
successful
1662+
:arg min_score:
1663+
:arg pit:
1664+
:arg post_filter:
16261665
:arg pre_filter_shard_size: A threshold that enforces a pre-
16271666
filter roundtrip to prefilter search shards based on query rewriting if
16281667
the number of shards the search request expands to exceeds the
@@ -1632,42 +1671,42 @@ async def search(
16321671
shard bounds and the query are disjoint.
16331672
:arg preference: Specify the node or shard the operation should
16341673
be performed on (default: random)
1674+
:arg profile:
16351675
:arg q: Query in the Lucene query string syntax
1676+
:arg query:
16361677
:arg request_cache: Specify if request cache should be used for
16371678
this request or not, defaults to index level setting
1679+
:arg rescore:
16381680
:arg rest_total_hits_as_int: Indicates whether hits.total should
16391681
be rendered as an integer or an object in the rest search response
16401682
:arg routing: A comma-separated list of specific routing values
1683+
:arg runtime_mappings:
1684+
:arg script_fields:
16411685
:arg scroll: Specify how long a consistent view of the index
16421686
should be maintained for scrolled search
1687+
:arg search_after:
16431688
:arg search_type: Search operation type Valid choices:
16441689
query_then_fetch, dfs_query_then_fetch
1645-
:arg seq_no_primary_term: Specify whether to return sequence
1646-
number and primary term of the last modification of each hit
1647-
:arg size: Number of hits to return (default: 10)
1648-
:arg sort: A comma-separated list of <field>:<direction> pairs
1649-
:arg stats: Specific 'tag' of the request for logging and
1650-
statistical purposes
1651-
:arg stored_fields: A comma-separated list of stored fields to
1652-
return as part of a hit
1690+
:arg seq_no_primary_term:
1691+
:arg size:
1692+
:arg slice:
1693+
:arg sort:
1694+
:arg stats:
1695+
:arg stored_fields:
1696+
:arg suggest:
16531697
:arg suggest_field: Specify which field to use for suggestions
16541698
:arg suggest_mode: Specify suggest mode Valid choices: missing,
16551699
popular, always Default: missing
16561700
:arg suggest_size: How many suggestions to return in response
16571701
:arg suggest_text: The source text for which the suggestions
16581702
should be returned
1659-
:arg terminate_after: The maximum number of documents to collect
1660-
for each shard, upon reaching which the query execution will terminate
1661-
early.
1662-
:arg timeout: Explicit operation timeout
1663-
:arg track_scores: Whether to calculate and return scores even
1664-
if they are not used for sorting
1665-
:arg track_total_hits: Indicate if the number of documents that
1666-
match the query should be tracked
1703+
:arg terminate_after:
1704+
:arg timeout:
1705+
:arg track_scores:
1706+
:arg track_total_hits:
16671707
:arg typed_keys: Specify whether aggregation and suggester names
16681708
should be prefixed by their respective types in the response
1669-
:arg version: Specify whether to return document version as part
1670-
of a hit
1709+
:arg version:
16711710
"""
16721711
if "from_" in params:
16731712
params["from"] = params.pop("from_")

elasticsearch/_async/client/__init__.pyi

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ from typing import (
2323
Any,
2424
Collection,
2525
Dict,
26+
List,
2627
Mapping,
2728
MutableMapping,
2829
Optional,
@@ -32,6 +33,8 @@ from typing import (
3233
Union,
3334
)
3435

36+
from typing_extensions import Literal
37+
3538
from ..transport import AsyncTransport
3639
from .async_search import AsyncSearchClient
3740
from .autoscaling import AutoscalingClient
@@ -841,49 +844,78 @@ class AsyncElasticsearch(object):
841844
self,
842845
*,
843846
body: Optional[Mapping[str, Any]] = ...,
844-
index: Optional[Any] = ...,
845-
doc_type: Optional[Any] = ...,
846-
_source: Optional[Any] = ...,
847-
_source_excludes: Optional[Any] = ...,
848-
_source_includes: Optional[Any] = ...,
847+
index: Optional[Union[List[str], str]] = ...,
848+
doc_type: Optional[Union[List[str], str]] = ...,
849+
_source: Optional[Union[Union[List[str], str], bool]] = ...,
850+
_source_excludes: Optional[Union[List[str], str]] = ...,
851+
_source_includes: Optional[Union[List[str], str]] = ...,
852+
aggregations: Optional[Mapping[str, Mapping[str, Any]]] = ...,
853+
aggs: Optional[Mapping[str, Mapping[str, Any]]] = ...,
849854
allow_no_indices: Optional[bool] = ...,
850855
allow_partial_search_results: Optional[bool] = ...,
851856
analyze_wildcard: Optional[bool] = ...,
852-
analyzer: Optional[Any] = ...,
853-
batched_reduce_size: Optional[Any] = ...,
857+
analyzer: Optional[str] = ...,
858+
batched_reduce_size: Optional[int] = ...,
854859
ccs_minimize_roundtrips: Optional[bool] = ...,
855-
default_operator: Optional[Any] = ...,
856-
df: Optional[Any] = ...,
857-
docvalue_fields: Optional[Any] = ...,
858-
expand_wildcards: Optional[Any] = ...,
860+
collapse: Optional[Mapping[str, Any]] = ...,
861+
default_operator: Optional[Union[Literal["AND", "OR"], str]] = ...,
862+
df: Optional[str] = ...,
863+
docvalue_fields: Optional[Union[List[str], str]] = ...,
864+
expand_wildcards: Optional[
865+
Union[
866+
List[Union[Literal["all", "open", "closed", "hidden", "none"], str]],
867+
Union[Literal["all", "open", "closed", "hidden", "none"], str],
868+
str,
869+
]
870+
] = ...,
859871
explain: Optional[bool] = ...,
860-
from_: Optional[Any] = ...,
872+
fields: Optional[List[Union[Mapping[str, Any], str]]] = ...,
873+
from_: Optional[int] = ...,
874+
highlight: Optional[Mapping[str, Any]] = ...,
861875
ignore_throttled: Optional[bool] = ...,
862876
ignore_unavailable: Optional[bool] = ...,
877+
indices_boost: Optional[List[Mapping[str, float]]] = ...,
863878
lenient: Optional[bool] = ...,
864-
max_concurrent_shard_requests: Optional[Any] = ...,
865-
min_compatible_shard_node: Optional[Any] = ...,
866-
pre_filter_shard_size: Optional[Any] = ...,
867-
preference: Optional[Any] = ...,
868-
q: Optional[Any] = ...,
879+
max_concurrent_shard_requests: Optional[int] = ...,
880+
min_compatible_shard_node: Optional[str] = ...,
881+
min_score: Optional[float] = ...,
882+
pit: Optional[Mapping[str, Any]] = ...,
883+
post_filter: Optional[Mapping[str, Any]] = ...,
884+
pre_filter_shard_size: Optional[int] = ...,
885+
preference: Optional[str] = ...,
886+
profile: Optional[bool] = ...,
887+
q: Optional[str] = ...,
888+
query: Optional[Mapping[str, Any]] = ...,
869889
request_cache: Optional[bool] = ...,
890+
rescore: Optional[Union[List[Mapping[str, Any]], Mapping[str, Any]]] = ...,
870891
rest_total_hits_as_int: Optional[bool] = ...,
871-
routing: Optional[Any] = ...,
872-
scroll: Optional[Any] = ...,
873-
search_type: Optional[Any] = ...,
892+
routing: Optional[str] = ...,
893+
runtime_mappings: Optional[Mapping[str, Mapping[str, Any]]] = ...,
894+
script_fields: Optional[Mapping[str, Mapping[str, Any]]] = ...,
895+
scroll: Optional[Union[int, str]] = ...,
896+
search_after: Optional[List[Union[None, float, int, str]]] = ...,
897+
search_type: Optional[
898+
Union[Literal["query_then_fetch", "dfs_query_then_fetch"], str]
899+
] = ...,
874900
seq_no_primary_term: Optional[bool] = ...,
875-
size: Optional[Any] = ...,
876-
sort: Optional[Any] = ...,
877-
stats: Optional[Any] = ...,
878-
stored_fields: Optional[Any] = ...,
879-
suggest_field: Optional[Any] = ...,
880-
suggest_mode: Optional[Any] = ...,
881-
suggest_size: Optional[Any] = ...,
882-
suggest_text: Optional[Any] = ...,
883-
terminate_after: Optional[Any] = ...,
884-
timeout: Optional[Any] = ...,
901+
size: Optional[int] = ...,
902+
slice: Optional[Mapping[str, Any]] = ...,
903+
sort: Optional[Union[List[str], str]] = ...,
904+
stats: Optional[List[str]] = ...,
905+
stored_fields: Optional[Union[List[str], str]] = ...,
906+
suggest: Optional[
907+
Union[Mapping[str, Any], Mapping[str, Mapping[str, Any]]]
908+
] = ...,
909+
suggest_field: Optional[str] = ...,
910+
suggest_mode: Optional[
911+
Union[Literal["missing", "popular", "always"], str]
912+
] = ...,
913+
suggest_size: Optional[int] = ...,
914+
suggest_text: Optional[str] = ...,
915+
terminate_after: Optional[int] = ...,
916+
timeout: Optional[Union[int, str]] = ...,
885917
track_scores: Optional[bool] = ...,
886-
track_total_hits: Optional[bool] = ...,
918+
track_total_hits: Optional[Union[bool, int]] = ...,
887919
typed_keys: Optional[bool] = ...,
888920
version: Optional[bool] = ...,
889921
pretty: Optional[bool] = ...,

elasticsearch/_async/helpers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,11 @@ async def async_scan(
353353
scroll_kwargs.setdefault(key, val)
354354

355355
# initial search
356+
search_kwargs = kwargs.copy()
357+
if query:
358+
search_kwargs.update(query)
356359
resp = await client.search(
357-
body=query, scroll=scroll, size=size, request_timeout=request_timeout, **kwargs
360+
scroll=scroll, size=size, request_timeout=request_timeout, **search_kwargs
358361
)
359362
scroll_id = resp.get("_scroll_id")
360363

0 commit comments

Comments
 (0)