Creating an OJAI QueryCondition Object

The Java and Python OJAI clients support a QueryCondition interface. After you create a QueryCondition object, call methods in the class to construct your query condition.

Creating a QueryCondition Object

Java OJAI provides a QueryCondition.is() method for specifying query conditions. The method takes three arguments:

  • The field path to apply the condition to
  • The condition operator, represented as a QueryCondition.Op
  • The value to compare the field path against

The field path is either a field in a JSON document, a subfield within a nested document, or an array element. Starting in MapR 6.1, you can also specify a container field path. See OJAI Query Conditions Using Container Field Paths for details.

Depending on the type of the field path, you specify the comparison value as follows:

Scalar Data
You can specify the value using either a Java typed value (for example, int, float, or String) or a Java OJAI object. The API supports the following OJAI types:
  • ODate
  • OInterval
  • OTime
  • OTimestamp
Nested Documents
You can specify only equality and non-equality conditions on nested documents. You specify the nested document using a Java Map object. In the case of equality, all of the fields in the nested document must match. The order of the fields is not relevant.
Arrays
You can specify only equality and non-equality conditions on arrays. You specify an array using a Java List object. In the case of equality, the order of the elements and the element values must match.

In addition to QueryCondition.is(), QueryCondition also supports the following methods:

QueryCondition Method Description
equals()

notequals()

Match for equality or non-equality on nested documents and arrays
in() Search for individual elements in an array
like() Search for string values using SQL LIKE expressions

matches()

Search for string values using regular expressions.

You can use regular expressions that compose the Perl-Compatible Regular Expressions (PCRE) library as well as a subset of the regular expressions that are supported in java.util.regex.pattern. See HBase Java Regular Expressions Support for a list of supported regular expressions.

and() Begins a new AND condition block
or() Begins a new OR condition block
elementAnd() Begins a new elementAnd block. See OJAI Query Condition Operators for a detailed description of this operator.
close() Closes a compound condition block
build() Builds the condition
Note: The material described in this section is a subset of the QueryCondition API. It introduces you to the basics of the API. For the complete API, see the QueryCondition interface.

Python OJAI provides a QueryCondition.is_() method for specifying query conditions. The method takes three arguments:

  • The field path to apply the condition to
  • The condition operator, represented as a QueryConditionOp
  • The value to compare the field path against

The field path is either a field in a JSON document, a sub-field within a nested document, or an array element. Starting in MapR 6.1, you can also specify a container field path. See OJAI Query Conditions Using Container Field Paths for details.

Depending on the type of the field path, you specify the comparison value as follows:

Scalar Data
You can specify the value using either a Python scalar value (for example, int, float, or str) or a Python OJAI object. The API supports the following OJAI types:
  • ODate
  • OInterval
  • OTime
  • OTimestamp
Nested Documents
You can specify only equality and non-equality conditions on nested documents. You specify the nested document using a Python dictionary object. In the case of equality, all of the fields in the nested document must match. The order of the fields is not relevant.
Arrays
You can specify only equality and non-equality conditions on arrays. You specify an array using a Python list object. In the case of equality, the order of the elements and the element values must match.

In addition to QueryCondition.is_(), QueryCondition also supports the following methods:

QueryCondition Method Description
equals_()

not_equals_()

Match for equality or non-equality on nested documents and arrays
in_() Search for individual elements in an array
like_() Search for string values using SQL LIKE expressions

matches_()

Search for string values using regular expressions.

You can use regular expressions that comprise the Perl-Compatible Regular Expressions (PCRE) library as well as a subset of the regular expressions that are supported in java.util.regex.pattern. See HBase Java Regular Expressions Support for a list of supported regular expressions.

and_() Begins a new AND condition block
or_() Begins a new OR condition block
element_and()

Begins a new elementAnd block. See OJAI Query Condition Operators for a detailed description of this operator.

close() Closes a compound condition block
build() Builds the condition
Note:
  • The material described in this section is a subset of the QueryCondition API. It introduces you to the basics of the API. For the complete API, see the QueryCondition interface.
  • The preferred approach for creating query conditions in Python is to create the condition in a JSON format. See Creating an OJAI Query Condition Using a JSON String

Example: Creating a QueryCondition Object

The following example shows how to define a QueryCondition object for this query condition:

(a.b.[0].boolean == false && (a.c.d != 5 || a.b[1].decimal > 1 || a.b[1].decimal < 10))
QueryCondition qc = connection.newCondition()
    .and()
        .is("a.b[0].boolean", Op.EQUAL, false)
        .or()
            .is("a.c.d", Op.NOT_EQUAL, 5)
            .is("a.b[1].decimal", Op.GREATER, 1)
            .is("a.b[1].decimal", Op.LESS, 10)
            .close()
    .close()
    .build();

Pass the QueryCondition object to the Query.where method. See the Java - OJAI QueryCondition Object example at Querying with Conditions for a complete Java code example.

qc = connection.new_condition()
    .and_()
        .is_('a.b[0].boolean', QueryConditionOp.EQUAL, False)
        .or_()
            .is_('a.c.d', QueryConditionOp.NOT_EQUAL, 5)
            .is_('a.b[1].decimal', QueryConditionOp.GREATER, 1)
            .is_('a.b[1].decimal', QueryConditionOp.LESS, 10)
            .close()
    .close()
    .build()

Pass the QueryCondition object to the Query.where method. See the Python - OJAI QueryCondition Object example at Querying with Conditions for a complete Python code example.

Examples: Using the QueryCondition.elementAnd Method

The following example shows how to write the elementAnd condition described at Using elementAnd with Nested Documents, using a QueryCondition object:

QueryCondition qc = connection.newCondition()
    .elementAnd("grades[]")
        .is("course", QueryConditionOp.EQUALS, "history")
        .is("score", QueryConditionOp.EQUALS, 12)
        .close()
    .build();
qc = connection.new_condition()
    .element_and("grades[]")
        .is_("course", QueryConditionOp.EQUALS, "history")
        .is_("score", QueryConditionOp.EQUALS, 12)
        .close()
    .build()

The following code corresponds to the example described at Using elementAnd with Scalar Values using a QueryCondition object:

QueryCondition qc = connection.newCondition()
    .elementAnd("values[]")
        .is("$", QueryConditionOp.GREATER, 7)
        .is("$", QueryConditionOp.LESS, 14)
        .close()
    .build();
qc = connection.new_condition()
    .element_and("values[]")
        .is_("$", QueryConditionOp.GREATER, 7)
        .is_("$", QueryConditionOp.LESS, 14)
        .close()
    .build()