Class QueryOptions

java.lang.Object
com.couchbase.client.java.CommonOptions<QueryOptions>
com.couchbase.client.java.query.QueryOptions

public class QueryOptions extends CommonOptions<QueryOptions>
Allows customizing various N1QL query options.

This object is NOT thread safe and must be constructed on a single thread and then passed to the Cluster.query(String, QueryOptions) method.

Since:
3.0.0
  • Constructor Details

    • QueryOptions

      protected QueryOptions()
      The options should only be instantiated through the queryOptions() static method.
  • Method Details

    • queryOptions

      public static QueryOptions queryOptions()
      Creates new QueryOptions with all default params set.
      Returns:
      constructed QueryOptions with its default values.
    • raw

      public QueryOptions raw(String key, Object value)
      Allows providing custom JSON key/value pairs for advanced usage.

      If possible, it is recommended to use other methods on this object to customize the query. The "raw" method should only be used if no such setter can be found (i.e. if an undocumented property should be set or you are using an older client and a new server-configuration property has been added to the cluster).

      Note that the value will be passed through a JSON encoder, so do not provide already encoded JSON as the value. The value must be one of the following types:

      • String
      • Integer
      • Long
      • Double
      • Boolean
      • BigInteger
      • BigDecimal
      • JsonObject
      • JsonArray
      • Map (with String keys, and values from this list)
      • List (with element types from this list)
      Parameters:
      key - the parameter name (key of the JSON property) or empty.
      value - the parameter value (value of the JSON property).
      Returns:
      the same QueryOptions for chaining purposes.
    • adhoc

      public QueryOptions adhoc(boolean adhoc)
      Allows turning this request into a prepared statement query.

      If set to

      false
      , the SDK will transparently perform "prepare and execute" logic the first time this query is seen and then subsequently reuse the prepared statement name when sending it to the server. If a query is executed frequently, this is a good way to speed it up since it will save the server the task of re-parsing and analyzing the query.

      If you are using prepared statements, make sure that if certain parts of the query string change you are using named or positional parameters. If the statement string itself changes it cannot be cached.

      Parameters:
      adhoc - if set to false this query will be turned into a prepared statement query.
      Returns:
      the same QueryOptions for chaining purposes.
    • scanConsistency

      public QueryOptions scanConsistency(QueryScanConsistency scanConsistency)
      Customizes the consistency guarantees for this query.

      Tuning the scan consistency allows to trade data "freshness" for latency and vice versa. By default QueryScanConsistency.NOT_BOUNDED is used, which means that the server returns the data it has in the index right away. This is fast, but might not include the most recent mutations. If you want to include all the mutations up to the point of the query, use QueryScanConsistency.REQUEST_PLUS.

      Note that you cannot use this method and consistentWith(MutationState) at the same time, since they are mutually exclusive. As a rule of thumb, if you only care to be consistent with the mutation you just wrote on the same thread/app, use consistentWith(MutationState). If you need "global" scan consistency, use QueryScanConsistency.REQUEST_PLUS on this method.

      Parameters:
      scanConsistency - the index scan consistency to be used for this query.
      Returns:
      the same QueryOptions for chaining purposes.
    • serializer

      public QueryOptions serializer(JsonSerializer serializer)
      Provides a custom JsonSerializer to be used for decoding the rows as they return from the server.

      If no serializer is provided, the default one from the ClusterEnvironment will be used (which is sufficient for most tasks at hand).

      Parameters:
      serializer - a custom serializer for this request.
      Returns:
      the same QueryOptions for chaining purposes.
    • profile

      public QueryOptions profile(QueryProfile profile)
      Customizes the server profiling level for this query.

      Note that you only want to tune this if you want to gather profiling/performance metrics for debugging. Turning this on in production (depending on the level) will likely have performance impact on the server query engine as a whole and on this query in particular!

      This is an Enterprise Edition feature. On Community Edition the parameter will be accepted, but no profiling information returned.

      Parameters:
      profile - the custom query profile level for this query.
      Returns:
      the same QueryOptions for chaining purposes.
    • clientContextId

      public QueryOptions clientContextId(String clientContextId)
      Supports providing a custom client context ID for this query.

      If no client context ID is provided by the user, a UUID is generated and sent automatically so by default it is always possible to identify a query when debugging. If you do not want to send one, pass either null or an empty string to this method.

      Parameters:
      clientContextId - the client context ID - if null or empty it will not be sent.
      Returns:
      the same QueryOptions for chaining purposes.
    • metrics

      public QueryOptions metrics(boolean metrics)
      Enables per-request metrics in the trailing section of the query.

      If this method is set to true, the server will send metrics back to the client which are available through the QueryMetaData.metrics() section. As opposed to profile(QueryProfile), returning metrics is rather cheap and can also be enabled in production if needed.

      Parameters:
      metrics - set to true if the server should return simple query metrics.
      Returns:
      the same QueryOptions for chaining purposes.
    • scanWait

      public QueryOptions scanWait(Duration wait)
      Allows customizing how long the query engine is willing to wait until the index catches up to whatever scan consistency is asked for in this query.

      Note that if QueryScanConsistency.NOT_BOUNDED is used, this method doesn't do anything at all. If no value is provided to this method, the server default is used.

      Parameters:
      wait - the maximum duration the query engine is willing to wait before failing.
      Returns:
      the same QueryOptions for chaining purposes.
    • maxParallelism

      public QueryOptions maxParallelism(int maxParallelism)
      Allows overriding the default maximum parallelism for the query execution on the server side.

      If 0 or a negative value is set, the parallelism is disabled. If not provided, the server default will be used.

      Parameters:
      maxParallelism - the maximum parallelism for this query, 0 or negative values disable it.
      Returns:
      the same QueryOptions for chaining purposes.
    • readonly

      public QueryOptions readonly(boolean readonly)
      Allows explicitly marking a query as being readonly and not mutating and documents on the server side.

      In addition to providing some security in that you are not accidentally modifying data, setting this flag to true also helps the client to more proactively retry and re-dispatch a query since then it can be sure it is idempotent. As a result, if your query is readonly then it is a good idea to set this flag.

      If set to true, then (at least) the following statements are not allowed:

      1. CREATE INDEX
      2. DROP INDEX
      3. INSERT
      4. MERGE
      5. UPDATE
      6. UPSERT
      7. DELETE
      Parameters:
      readonly - true if readonly should be set, false is the default and will use the server side default.
      Returns:
      the same QueryOptions for chaining purposes.
    • scanCap

      public QueryOptions scanCap(int scanCap)
      Supports customizing the maximum buffered channel size between the indexer and the query service.

      This is an advanced API and should only be tuned with care. Use 0 or a negative number to disable.

      Parameters:
      scanCap - the scan cap size, use 0 or negative number to disable.
      Returns:
      the same QueryOptions for chaining purposes.
    • pipelineBatch

      public QueryOptions pipelineBatch(int pipelineBatch)
      Supports customizing the number of items execution operators can batch for fetch from the KV layer on the server.

      This is an advanced API and should only be tuned with care.

      Parameters:
      pipelineBatch - the pipeline batch size.
      Returns:
      the same QueryOptions for chaining purposes.
    • pipelineCap

      public QueryOptions pipelineCap(int pipelineCap)
      Allows customizing the maximum number of items each execution operator can buffer between various operators on the server.

      This is an advanced API and should only be tuned with care.

      Parameters:
      pipelineCap - the pipeline cap size.
      Returns:
      the same QueryOptions for chaining purposes.
    • parameters

      public QueryOptions parameters(JsonObject named)
      Sets named parameters for this query.

      Note that named and positional parameters cannot be used at the same time. If one is set, the other one is "nulled" out and not being sent to the server.

      Parameters:
      named - JsonObject the named parameters.
      Returns:
      the same QueryOptions for chaining purposes.
    • parameters

      public QueryOptions parameters(JsonArray positional)
      Sets positional parameters for this query.

      Note that named and positional parameters cannot be used at the same time. If one is set, the other one is "nulled" out and not being sent to the server.

      Parameters:
      positional - JsonArray the positional parameters.
      Returns:
      the same QueryOptions for chaining purposes.
    • consistentWith

      public QueryOptions consistentWith(MutationState mutationState)
      Sets the MutationTokens this query should be consistent with.

      These mutation tokens are returned from mutations (i.e. as part of a MutationResult) and if you want your N1QL query to include those you need to pass the mutation tokens into a MutationState.

      Note that you cannot use this method and scanConsistency(QueryScanConsistency) at the same time, since they are mutually exclusive. As a rule of thumb, if you only care to be consistent with the mutation you just wrote on the same thread/app, use this method. If you need "global" scan consistency, use QueryScanConsistency.REQUEST_PLUS on scanConsistency(QueryScanConsistency).

      Parameters:
      mutationState - the mutation state containing the mutation tokens.
      Returns:
      the same QueryOptions for chaining purposes.
    • flexIndex

      public QueryOptions flexIndex(boolean flexIndex)
      Tells the query engine to use a flex index (utilizing the search service).
      Parameters:
      flexIndex - if a flex index should be used, false is the default.
      Returns:
      the same QueryOptions for chaining purposes.
    • preserveExpiry

      @SinceCouchbase("7.1") public QueryOptions preserveExpiry(boolean preserveExpiry)
      Tells the query engine to preserve expiration values set on any documents modified by this query.

      This feature works from Couchbase Server 7.1.0 onwards.

      Parameters:
      preserveExpiry - if expiration values should be preserved, the default is false.
      Returns:
      the same QueryOptions for chaining purposes.
    • build

      @Internal public QueryOptions.Built build()
    • asTransaction

      @SinceCouchbase("7.0") public QueryOptions asTransaction()
      Performs this as a single query transaction, with default options.

      Returns:
      the same QueryOptions for chaining purposes.
    • asTransaction

      @SinceCouchbase("7.0") public QueryOptions asTransaction(SingleQueryTransactionOptions options)
      Performs this as a single query transaction.

      Parameters:
      options - configuration options to use.
      Returns:
      the same QueryOptions for chaining purposes.
    • useReplica

      @SinceCouchbase("7.6") public QueryOptions useReplica(@Nullable Boolean useReplica)
      Allows returning results from replicas.

      If true, the server is allowed to use data from replicas (which may be stale) when executing the query. If false, the server must use up-to-date data from primaries. If null (the default), honor the server-side configuration for this setting.

      Parameters:
      useReplica - if true this query will possible return results from replicas; if false, from primary. if not set, then according to the server.
      Returns:
      the same QueryOptions for chaining purposes.