Java SDK Query Predicate builders

Ask about this Page
Copy for LLM
View as Markdown

Create readable and maintainable Query Predicates using the Java SDK.

Alternatively, you can use query predicates directly as strings with input variables. Input variables allow you to parameterize predicates and avoid string concatenation:
Use input variables with Query Predicatesjava
// Using an input variable for a single value
CustomerPagedQueryResponse response = httpApiRoot
  .customers()
  .get()
  .withPredicateVar("name", "John")
  .withWhere("firstName = :name")
  .executeBlocking()
  .getBody();

// Using an input variable for an array of values
CartPagedQueryResponse cartResponse = httpApiRoot
  .carts()
  .get()
  .addPredicateVar("statuses", Arrays.asList("Open", "Merged"))
  .withWhere("cartState in :statuses")
  .executeBlocking()
  .getBody();

Example usage

The following examples demonstrate how to use the Query Predicate builder for different scenarios. More examples can be found here.
Build a Query Predicate for Customersjava
import static com.commercetools.api.predicates.query.CustomerQueryBuilderDsl.*;

// Finds Customers called "John Doe"
String predicate = CustomerQueryBuilderDsl.of()
      .firstName().is("John")
      .and()
      .lastName().is("Doe")
      .build();

Equality comparisons

Equality comparisonsjava
// Finds a Cart where the `id` is "abc"
CartQueryBuilderDsl.of().id().is("abc");

// Finds all Customers born on 12 October 2005
CustomerQueryBuilderDsl.of().dateOfBirth().is(LocalDate.parse("2005-10-12"));

// Finds all Tax Rates with an `amount` of 19%
TaxRateQueryBuilderDsl.of().amount().is(0.19);

Logical operators

Logical operatorsjava
// Finds Carts where the `id` is not "abc"
CartQueryBuilderDsl.of().id().is("abc").not();

// Finds Carts where the `id` is "abc" and `customerId` is "def"
CartQueryBuilderDsl.of().id().is("abc").and(p -> p.customerId().is("def"));


// Finds Carts where the `id` is "abc" or `customerId` is "def"
CartQueryBuilderDsl.of().id().is("abc").or(p -> p.customerId().is("def"));

Range comparisons

Range comparisonsjava
// Finds Carts where `version` < 10
CartQueryBuilderDsl.of().version().isLessThan(10L);

// Finds Carts where `version` > 10
CartQueryBuilderDsl.of().version().isGreaterThan(10L);

// Finds all Tax Rates with an `amount` of less than 19%
TaxRateQueryBuilderDsl.of().amount().isLessThan(0.19);

// Finds all Customers born after 12 October 2005
CustomerQueryBuilderDsl.of().dateOfBirth().isGreaterThan(LocalDate.parse("2005-10-12"));

Collection queries

Collection queriesjava
// Finds Carts where `key` is `foo` or `bar`
CartQueryBuilderDsl.of().key().isIn(Arrays.asList("foo", "bar"));

// Finds Tax Rates where the amount is 19% or 20%
TaxRateQueryBuilderDsl.of().amount().isIn(Arrays.asList(0.19, 0.20));

Nested queries

Nested queriesjava
// Finds a Product where the current projection's English slug is "super-product" and the English name is "Super Product"
ProductQueryBuilderDsl.of()
  .masterData(m -> m.current(c -> c.slug(l -> l.with(Locale.ENGLISH).is("super-product"))
    .and(t -> t.name(l -> l.with(Locale.ENGLISH).is("Super Product")))));

Special cases

Special casesjava
// Finds Carts which have a `key`
CartQueryBuilderDsl.of().key().isDefined();

// Finds Carts which do not have a `key`
CartQueryBuilderDsl.of().key().isNotDefined();

// Finds Carts without Line Items
CartQueryBuilderDsl.of().lineItems().isEmpty();

// Finds Types where the `resourceTypeIds` contains `approval-flow`
TypeQueryBuilderDsl.of().resourceTypeIds().containsAnyVar("approval-flow");