search
- class opensearchpy.helpers.search.Search(**kwargs)[source]
Bases:
Request
Search request to opensearch.
- Parameters:
using – OpenSearch instance to use
index – limit the search to index
doc_type – only query this type.
kwargs (Any) –
All the parameters supplied (or omitted) at creation type can be later overridden by methods (using, index and doc_type respectively).
- __getitem__(n)[source]
Support slicing the Search instance for pagination.
Slicing equates to the from/size parameters. E.g.:
s = Search().query(...)[0:25]
is equivalent to:
s = Search().query(...).extra(from_=0, size=25)
- _clone()[source]
Return a clone of the current search request. Performs a shallow copy of all the underlying objects. Used internally by most state modifying APIs.
- Return type:
- collapse(field=None, inner_hits=None, max_concurrent_group_searches=None)[source]
Add collapsing information to the search request.
If called without providing
field
, it will remove all collapse requirements, otherwise it will replace them with the provided arguments.The API returns a copy of the Search object and can thus be chained.
- count()[source]
Return the number of hits matching the query and filters. Note that only the actual number is returned.
- Return type:
- execute(ignore_cache=False)[source]
Execute the search and return an instance of
Response
wrapping all the data.
- classmethod from_dict(d)[source]
Construct a new Search instance from a raw dict containing the search body. Useful when migrating from raw dictionaries.
Example:
s = Search.from_dict({ "query": { "bool": { "must": [...] } }, "aggs": {...} }) s = s.filter('term', published=True)
- highlight(*fields, **kwargs)[source]
Request highlighting of some fields. All keyword arguments passed in will be used as parameters for all the fields in the
fields
parameter. Example:Search().highlight('title', 'body', fragment_size=50)
will produce the equivalent of:
{ "highlight": { "fields": { "body": {"fragment_size": 50}, "title": {"fragment_size": 50} } } }
If you want to have different options for different fields you can call
highlight
twice:Search().highlight('title', fragment_size=50).highlight('body', fragment_size=100)
which will produce:
{ "highlight": { "fields": { "body": {"fragment_size": 100}, "title": {"fragment_size": 50} } } }
- highlight_options(**kwargs)[source]
Update the global highlighting options used for this request. For example:
s = Search() s = s.highlight_options(order='score')
- scan()[source]
Turn the search into a scan search and return a generator that will iterate over all the documents matching the query.
Use
params
method to specify any additional arguments you with to pass to the underlyingscan
helper fromopensearchpy
- Return type:
- script_fields(**kwargs)[source]
Define script fields to be calculated on hits.
Example:
s = Search() s = s.script_fields(times_two="doc['field'].value * 2") s = s.script_fields( times_three={ 'script': { 'lang': 'painless', 'source': "doc['field'].value * params.n", 'params': {'n': 3} } } )
- sort(*keys)[source]
Add sorting information to the search request. If called without arguments it will remove all sort requirements. Otherwise it will replace them. Acceptable arguments are:
'some.field' '-some.other.field' {'different.field': {'any': 'dict'}}
so for example:
s = Search().sort( 'category', '-title', {"price" : {"order" : "asc", "mode" : "avg"}} )
will sort by
category
,title
(in descending order) andprice
in ascending order using theavg
mode.The API returns a copy of the Search object and can thus be chained.
- source(fields=None, **kwargs)[source]
Selectively control how the _source field is returned.
- Parameters:
- Return type:
If
fields
is None, the entire document will be returned for each hit. If fields is a dictionary with keys of ‘includes’ and/or ‘excludes’ the fields will be either included or excluded appropriately.Calling this multiple times with the same named parameter will override the previous values with the new ones.
Example:
s = Search() s = s.source(includes=['obj1.*'], excludes=["*.description"]) s = Search() s = s.source(includes=['obj1.*']).source(excludes=["*.description"])
- suggest(name, text, **kwargs)[source]
Add a suggestions request to the search.
- Parameters:
- Return type:
All keyword arguments will be added to the suggestions body. For example:
s = Search() s = s.suggest('suggestion-1', 'OpenSearch', term={'field': 'body'})