search #
Searches documents.
There is a limit to how many documents can be returned by a single search query. That limit is by default set at 10000 documents (see limits.documentsFetchCount
), and you can't get over it even with the from
and size
pagination options.
To handle larger result sets, you have to either create a cursor by providing a value to the scroll
option or, if you sort the results, by using the Elasticsearch search_after command.
When using a cursor with the scroll
option, Elasticsearch has to duplicate the transaction log to keep the same result during the entire scroll session. It can lead to memory leaks if a scroll duration too large is provided, or if too many scroll sessions are open simultaneously.
You can restrict the scroll session maximum duration under the services.storage.maxScrollDuration
configuration key.
This method also supports the Koncorde Filters DSL to match documents by passing the lang
argument with the value koncorde
. Koncorde filters will be translated into an Elasticsearch query.
Koncorde bool
operator and regexp
clause are not supported for search queries.
Multi Search #
This method also support searching accross multiple indexes and collections using the targets
parameter instead of index
, collection
parameters. See Target Format.
Multi Search is only supported in WebSocket and MQTT protocols.
Query Syntax #
HTTP #
URL: http://kuzzle:7512/<index>/<collection>/_search[?from=<int>][&size=<int>][&scroll=<time to live>][&lang=<query language>]
Method: POST
Body:
{
"query": {
// ...
},
"aggregations": {
// ...
},
"sort": [
// ...
]
}
You can also access this route with the GET
verb:
URL: http://kuzzle:7512/<index>/<collection>/_search[?searchBody=<string>][?from=<int>][&size=<int>][&scroll=<time to live>]
Method: GET
Other protocols #
Search using index
& collection
parameters
{
"index": "<index>",
"collection": "<collection>",
"controller": "document",
"action": "search",
"body": {
"query": {
// ...
},
"aggregations": {
// ...
},
"sort": [
// ...
]
},
// optional:
"from": <starting offset>,
"size": <page size>,
"scroll": "<scroll duration>",
"lang": "<query language>"
}
Search using targets
parameter
{
"targets": [
{
"index": "<index>"
"collections": ["<collection>", "<anotherCollection>"]
},
// ...
],
"controller": "document",
"action": "search",
"body": {
"query": {
// ...
},
"aggregations": {
// ...
},
"sort": [
// ...
]
},
// optional:
"from": <starting offset>,
"size": <page size>,
"scroll": "<scroll duration>",
"lang": "<query language>"
}
Kourou #
kourou document:search <index> <collection> <query>
kourou document:search <index> <collection> <query> --sort <sort> --size <size>
Arguments #
collection
: collection nameindex
: index name
or
targets
: list of target. See Target Format.
Optional: #
from
: paginates search results by defining the offset from the first result you want to fetch. Usually used with thesize
argumentscroll
: creates a forward-only result cursor. This option must be set with a time duration, at the end of which the cursor is destroyed. If set, a cursor identifier namedscrollId
is returned in the results. This cursor can then be moved forward using the scroll API actionsize
: set the maximum number of documents returned per result pagelang
: specify the query language to use. By default, it'selasticsearch
butkoncorde
can also be used.Available since 2.8.0
Body properties #
Optional: #
query
: the search query itself, using the ElasticSearch Query DSL or the Koncorde Filters DSL syntax.aggregations
: control how the search result should be aggregatedsort
: contains a list of fields, used to sort search results, in order of importance
An empty body matches all documents in the queried collection.
Only the following fields are available in the top level of the search body: aggregations
, aggs
, collapse
, explain
, fields
, from
, highlight
, query
, search_timeout
, size
, sort
, _name
, _source
, _source_excludes
, _source_includes
Response #
Returns a paginated search result set, with the following properties:
aggregations
: provides aggregation information. Present only if anaggregations
object has been provided in the search bodyhits
: array of found documents. Each document has the following properties:_id
: document unique identifierindex
: index nameAvailable since 2.17.0collection
: collection nameAvailable since 2.17.0_score
: relevance score_source
: new document contenthighlight
: optional result from highlight APIinner_hits
: optional result from inner_hits APIAvailable since 2.14.1
remaining
: remaining documents that can be fetched. Present only if thescroll
argument has been suppliedAvailable since 2.4.0scrollId
: identifier to the next page of result. Present only if thescroll
argument has been suppliedtotal
: total number of found documents. Can be greater than the number of documents in a result page, meaning that other matches than the one retrieved are available
{
"status": 200,
"error": null,
"index": "<index>",
"collection": "<collection>",
"action": "search",
"controller": "document",
"requestId": "<unique request identifier>",
"result": {
"scrollId": "<scroll id>",
"hits": [
{
"_id": "<document unique identifier>",
"_score": 1,
"_source": {
// document content
}
},
{
"_id": "<another document unique identifier>",
"_score": 1,
"_source": {
// document content
}
}
],
// Present only if aggregation parameters have been set
"aggregations": {
"aggs_name": {
}
},
"total": 42
}
}
Target Format #
{
"index": "<index>"
"collections": ["<collection>", "<anotherCollection>"]
}