SDK
SDK Java v3.x
2

# 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.

Available since Kuzzle 2.2.0

You can restrict the scroll session maximum duration under the services.storage.maxScrollDuration configuration key.


# Arguments

Copied to clipboard!
public CompletableFuture<SearchResult> search(
      final String index,
      final String collection,
      final ConcurrentHashMap<String, Object> searchQuery,
      final SearchOptions options) throws NotConnectedException, InternalException

Arguments Type Description
index
String
Index
collection
String
Collection
searchQuery
ConcurrentHashMap
Search query
options
SearchOptions
Query options

# searchQuery body properties:

An empty body matches all documents in the queried collection.

# options

A SearchOptions object.

The following options can be set:

Options Type
(default)
Description
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)

# Return

Returns a SearchResult object.

# Usage

Copied to clipboard!
ConcurrentHashMap<String, Object> suv = new ConcurrentHashMap<>();
suv.put("category", "suv");
ConcurrentHashMap<String, Object> limousine = new ConcurrentHashMap<>();
limousine.put("category", "limousine");
CreateOptions options = new CreateOptions();
options.setWaitForRefresh(true);
for (int i = 0; i < 5; i += 1) {
  kuzzle.getDocumentController().create("nyc-open-data", "yellow-taxi", suv, options).get();
}
for (int i = 0; i < 10; i += 1) {
  kuzzle.getDocumentController().create("nyc-open-data", "yellow-taxi", limousine, options).get();
}
ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> query = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("category", "suv");
query.put("match", match);
searchQuery.put("query", query);
SearchResult results = kuzzle
  .getDocumentController()
  .search("nyc-open-data", "yellow-taxi", searchQuery).get();
  System.out.println("Successfully retrieved " + results.total + " documents");
/*
  {
    "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
*/