search #
Searches document.
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, and you can't get over it even with the from and size pagination options.
When processing a large number of documents (i.e. more than 1000), it is advised to paginate the results using SearchResult.next rather than increasing the size parameter.
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 great 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
clauses are not supported for search queries.
:::: tabs ::: tab Java
Arguments #
public CompletableFuture<SearchResult> search(
String index,
String collection,
Map<String, Object> searchQuery) throws NotConnectedException, InternalException
public CompletableFuture<SearchResult> search(
String index,
String collection,
Map<String, Object> searchQuery,
String scroll) throws NotConnectedException, InternalException
public CompletableFuture<SearchResult> search(
String index,
String collection,
Map<String, Object> searchQuery,
Lang lang) throws NotConnectedException, InternalException
public CompletableFuture<SearchResult> search(
String index,
String collection,
Map<String, Object> searchQuery,
String scroll,
Lang lang) throws NotConnectedException, InternalException
public CompletableFuture<SearchResult> search(
String index,
String collection,
Map<String, Object> searchQuery,
String scroll,
Integer size,
Lang lang) throws NotConnectedException, InternalException
public CompletableFuture<SearchResult> search(
String index,
String collection,
Map<String, Object> searchQuery
Integer size,
Integer from,
Lang lang)
throws NotConnectedException, InternalException
Arguments | Type | Description |
---|---|---|
index | String | Index |
collection | String | Collection |
searchQuery | Map | Search query |
from | Integer ( 0 ) | Offset of the first document to fetch |
size | Integer ( 10 ) | Maximum number of documents to retrieve per page |
scroll | String ( "" ) | When set, gets a forward-only cursor having its ttl set to the given value (ie 1s ; cf elasticsearch time limits) |
lang | Lang | Specify the query language to use. By default, it's elasticsearch but koncorde can also be used. Available since 1.1.0 |
searchQuery body properties: #
query
: the search query itself, using the ElasticSearch Query DSL or the Koncorde Filters DSL syntax.aggregations
: control how the search results 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.
Return #
Returns a SearchResult object.
Usage #
With the ElasticSearch Query DSL syntax.
Map<String, Object> searchQuery = new HashMap<>();
Map<String, Object> query = new HashMap<>();
Map<String, Object> match = new HashMap<>();
match.put("category", "suv");
query.put("match", match);
searchQuery.put("query", query);
SearchResult results = kuzzle
.getDocumentController()
.search("nyc-open-data", "yellow-taxi", searchQuery).get();
/*
{
"aggregations"=undefined,
"hits"=[
{
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
...
]
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
With the Koncorde Filters DSL syntax.
Map<String, Object> searchQuery = new HashMap<>();
Map<String, Object> query = new HashMap<>();
Map<String, Object> equals = new HashMap<>();
equals.put("category", "suv");
query.put("equals", equals);
searchQuery.put("query", query);
SearchResult results = kuzzle
.getDocumentController()
.search("nyc-open-data", "yellow-taxi", searchQuery, Lang.KONCORDE).get();
/*
{
"aggregations"=undefined,
"hits"=[
{
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
...
]
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
::: ::: tab Kotlin
Arguments #
fun search(
index: String,
collection: String,
searchQuery: Map<String?, Any?>,
scroll: String? = null,
size: Int? = null,
from: Int = 0,
land: Lang = Lang.ELASTICSEARCH): CompletableFuture<SearchResult>
Arguments | Type | Description |
---|---|---|
index | String | Index |
collection | String | Collection |
searchQuery | Map | Search query |
from | Int ( 0 ) | Offset of the first document to fetch |
size | Int ( 10 ) | Maximum number of documents to retrieve per page |
scroll | String ( "" ) | When set, gets a forward-only cursor having its ttl set to the given value (ie 1s ; cf elasticsearch time limits) |
lang | Lang | Specify the query language to use. By default, it's elasticsearch but koncorde can also be used. Available since 1.1.0 |
searchQuery body properties: #
query
: the search query itself, using the ElasticSearch Query DSL or the Koncorde Filters DSL syntax.aggregations
: control how the search results 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.
Return #
Returns a SearchResult object.
Usage #
With the ElasticSearch Query DSL syntax.
val match: Map<String, Any?> =
HashMap<String, Any?>().apply {
put("category", "suv")
}
val query: Map<String, Any?> =
HashMap<String, Any?>().apply {
put("match", match)
}
val searchQuery: Map<String, Any?> =
HashMap<String, Any?>().apply {
put("query", query)
}
val results = kuzzle
.documentController
.search("nyc-open-data", "yellow-taxi", searchQuery).get();
/*
{
"aggregations"=undefined,
"hits"=[
{
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
...
]
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
With the Koncorde Filters DSL syntax.
val equals: Map<String, Any?> =
HashMap<String, Any?>().apply {
put("category", "suv")
}
val query: Map<String, Any?> =
HashMap<String, Any?>().apply {
put("equals", equals)
}
val searchQuery: Map<String, Any?> =
HashMap<String, Any?>().apply {
put("query", query)
}
val results = kuzzle
.documentController
.search("nyc-open-data", "yellow-taxi", searchQuery, lang = Lang.KONCORDE).get();
/*
{
"aggregations"=undefined,
"hits"=[
{
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
...
]
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
::: ::::