Class SearchQueryBuilder

java.lang.Object
com.oracle.content.sdk.request.core.SearchQueryBuilder

public class SearchQueryBuilder extends Object
This helper class will construct the raw "filter" string to use when specifying a filter for SearchAssetsRequest. A type is usually specified when creating the builder, then field expressions can be added using andField(String, QueryOperator, String) )} or orField(String, QueryOperator, String). For example to construct a search query string such as 'type eq "fruit" AND fields.color EQ "red"', which searches items of type 'fruit' that also has a field called 'color' with the value of red.
 
  String redFruitFilterQuery filterBuilder =
      new SearchQueryBuilder("fruit").andField("color", QueryOperator.EQUALS, "red").build();
 
More complex nested expressions can be constructed using startGroup(QueryOperator). For example, to create a query such as 'type eq "menu_type" AND (fields.item_type eq "breakfast" OR fields.item_type eq "lunch")':
 
 String searchQuery =
   new SearchQueryBuilder("menu_type")
   .startGroup(SearchQueryBuilder.QueryOperator.AND)
    .orField(
     "item_type", SearchQueryBuilder.QueryOperator.EQUALS, "breakfast")
    .orField(
     "item_type", SearchQueryBuilder.QueryOperator.EQUALS, "lunch")
     .endGroup()
    .build();
 
  • Constructor Details

    • SearchQueryBuilder

      public SearchQueryBuilder(String type)
      Constructor that takes a type and starts the filter with the expression type eq 'typename', where 'typename' is the parameter. This is the most common way to start an expression, but a blank expression can be created as well by calling SearchQueryBuilder() and then calling startExpression(String, QueryOperator, String)
      Parameters:
      type - String for the type to match
    • SearchQueryBuilder

      public SearchQueryBuilder()
      Start with a blank builder
  • Method Details

    • getFieldName

      public static String getFieldName(String fieldName)
      If the field name is not a top-level name such as "type" or "name" or "description" it must be prefixed with "fields."
      Parameters:
      fieldName - Name of field
      Returns:
      Name of field with "fields." prefix if necessary
    • startExpression

      public SearchQueryBuilder startExpression(String field, SearchQueryBuilder.QueryOperator operator, String value)
      Called to start an expression at the beginning of the filter.
      Parameters:
      field - name of the field to match
      operator - the operation to use (e.g. "co" or "eq")
      value - the value to match
      Returns:
      builder
    • startExpression

      public SearchQueryBuilder startExpression(String expression)
      Called to start an expression at the beginning of the filter with a fully formed expression
      Parameters:
      expression - expression to start
      Returns:
      builder
    • addExpression

      public SearchQueryBuilder addExpression(String expression)
      Add an additional filter expression using "and" to an expression already started.
      Parameters:
      expression - fully formed filter expression
      Returns:
      builder
    • addClause

      public SearchQueryBuilder addClause(String clause, String field, String operator, String value)
      Specify your own clause such as "and", "or". This isn't restricted to the QueryOperator enum but in general should be used for clause and operators.
      Parameters:
      clause - field such as "and", "or"
      field - name of the field to match
      operator - the operation to use (e.g. "co" or "eq")
      value - the value to match
      Returns:
      builder
    • andField

      public SearchQueryBuilder andField(String field, SearchQueryBuilder.QueryOperator operator, String value)
      Adds a filter expression of form "AND field operator value" to the filter request.
      Parameters:
      field - name of the field to match
      operator - see SearchQueryBuilder.QueryOperator for possible values
      value - the value to match
      Returns:
      builder
    • startGroup

      Start a new expression group with parenthesis, for example "AND (expression)".
      Parameters:
      operator - Operator to proceed the group expression
      Returns:
      builder
    • startGroup

      public SearchQueryBuilder startGroup()
    • endGroup

      public SearchQueryBuilder endGroup()
      End expression group with parenthesis.
      Returns:
      builder
    • orField

      public SearchQueryBuilder orField(String field, SearchQueryBuilder.QueryOperator operator, String value)
      Adds a filter expression of form "OR field operator value" to the filter.
      Parameters:
      field - name of the field to match
      operator - see SearchQueryBuilder.QueryOperator for possible values
      value - the value to match
      Returns:
      builder
    • matchList

      public SearchQueryBuilder matchList(String field, List<String> stringList, boolean groupExpression)
      Given a list of strings, adds to the filter a query expression to match any of the strings. For example, if string list contains "a, b, c", then and call is matchList("id", list), then result would be "id eq a OR id eq b OR id eq c"
      Parameters:
      field - field to match such as "id"
      stringList - list of string values
      groupExpression - if true, will put expressions in a group ( expr )
      Returns:
      matching expression.
    • matchIdList

      public static String matchIdList(String field, List<String> stringList, boolean groupExpression)
      Given a list of strings, returns a query expression to match any of the strings. For example, if string list contains "a, b, c", then and call is matchIdList("id", list), then result would be "id eq a OR id eq b OR id eq c"
      Parameters:
      field - field to match such as "id"
      stringList - list of string values
      groupExpression - if true, will put expressions in a group ( expr )
      Returns:
      matching expression.
    • getFieldList

      public static String getFieldList(List<String> fieldList)
    • build

      public String build()
      Return the fully constructed filter string to use for search call
      Returns:
      filter expression to use for search