Ends-With Query


  • You can query for documents having a field that ends with some specified string.

  • Unless explicitly specified, the string comparisons are case-insensitive by default.

  • Note:
    This postfix search causes the server to perform a full index scan.
    Instead, consider using a static index that indexes the field in reverse order
    and then query with a prefix search, which is much faster.

  • In this page:


EndsWith

List<Product> products = session
    .Query<Product>()
     // Call 'EndsWith' on the field
     // Pass the postfix to search by
    .Where(x => x.Name.EndsWith("Lager"))
    .ToList();

// Results will contain only Product documents having a 'Name' field
// that ends with any case variation of 'lager'
List<Product> products = await asyncSession
    .Query<Product>()
     // Call 'EndsWith' on the field
     // Pass the postfix to search by
    .Where(x => x.Name.EndsWith("Lager"))
    .ToListAsync();

// Results will contain only Product documents having a 'Name' field
// that ends with any case variation of 'lager'
List<Product> products = session.Advanced
    .DocumentQuery<Product>()
     // Call 'WhereEndsWith'
     // Pass the document field and the postfix to search by
    .WhereEndsWith(x => x.Name, "Lager")
    .ToList();

// Results will contain only Product documents having a 'Name' field
// that ends with any case variation of 'lager'
from "Products"
where endsWith(Name, "Lager")

EndsWith (case-sensitive)

List<Product> products = session
    .Query<Product>()
     // Pass 'exact: true' to search for an EXACT postfix match
    .Where(x => x.Name.EndsWith("Lager"), exact: true)
    .ToList();

// Results will contain only Product documents having a 'Name' field
// that ends with 'Lager'
List<Product> products = await asyncSession
    .Query<Product>()
     // Pass 'exact: true' to search for an EXACT postfix match
    .Where(x => x.Name.EndsWith("Lager"), exact: true)
    .ToListAsync();

// Results will contain only Product documents having a 'Name' field
// that ends with 'Lager'
List<Product> products = session.Advanced
    .DocumentQuery<Product>()
     // Call 'WhereEndsWith'
     // Pass 'exact: true' to search for an EXACT postfix match
    .WhereEndsWith(x => x.Name, "Lager", exact: true)
    .ToList();

// Results will contain only Product documents having a 'Name' field
// that ends with 'Lager'
from "Products"
where exact(endsWith(Name, "Lager"))

Negate EndsWith

List<Product> products = session
    .Query<Product>()
     // Call 'EndsWith' on the field
     // Pass the postfix to search by
    .Where(x => x.Name.EndsWith("Lager") == false)
    .ToList();

// Results will contain only Product documents having a 'Name' field
// that does NOT end with 'lager' or any other case variations of it
List<Product> products = await asyncSession
    .Query<Product>()
     // Call 'EndsWith' on the field
     // Pass the postfix to search by
    .Where(x => x.Name.EndsWith("Lager") == false)
    .ToListAsync();

// Results will contain only Product documents having a 'Name' field
// that does NOT end with 'lager' or any other case variations of it
List<Product> products = session.Advanced
    .DocumentQuery<Product>()
     // Call 'Not' to negate the next predicate
    .Not
     // Call 'WhereEndsWith'
     // Pass the document field and the postfix to search by
    .WhereEndsWith(x => x.Name, "Lager")
    .ToList();

// Results will contain only Product documents having a 'Name' field
// that does NOT end with 'lager' or any other case variations of it
from "Products"
where (true and not endsWith(Name, "Lager"))