java.lang.Object
org.springframework.data.elasticsearch.core.query.Criteria

public class Criteria extends Object
Criteria is the central class when constructing queries. It follows more or less a fluent API style, which allows to easily chain together multiple criteria.

A Criteria references a field and has Criteria.CriteriaEntry sets for query and filter context. When building the query, the entries from the criteria entries are combined in a bool must query (if more than one.

A Criteria also has a Criteria.CriteriaChain which is used to build a collection of Criteria with the fluent API. The value of isAnd() and isOr() describes whether the queries built from the criteria chain should be put in a must (and) or a should (or) clause when a query is built from it, it is not used to build some relationship between the elements of the criteria chain.
Author:
Rizwan Idrees, Mohsin Husen, Franck Marchand, Peter-Josef Meisch, Ezequiel AntĂșnez Camacho
  • Field Details

  • Constructor Details

    • Criteria

      public Criteria()
    • Criteria

      public Criteria(String fieldName)
      Creates a new Criteria with provided field name
      Parameters:
      fieldName - the field name
    • Criteria

      public Criteria(Field field)
      Creates a new Criteria for the given field
      Parameters:
      field - field to create the Criteria for
    • Criteria

      protected Criteria(List<Criteria> criteriaChain, String fieldName)
      Creates a Criteria for the given field, sets it's criteriaChain to the given value and adds itself to the end of the chain.
      Parameters:
      criteriaChain - the chain to add to
      fieldName - field to create the Criteria for
    • Criteria

      protected Criteria(List<Criteria> criteriaChain, Field field)
      Creates a Criteria for the given field, sets it's criteriaChain to the given value and adds itself to the end of the chain.
      Parameters:
      criteriaChain - the chain to add to
      field - field to create the Criteria for
  • Method Details

    • and

      public static Criteria and()
      Returns:
      factory method to create an and-Criteria that is not bound to a field
      Since:
      4.1
    • or

      public static Criteria or()
      Returns:
      factory method to create an or-Criteria that is not bound to a field
      Since:
      4.1
    • where

      public static Criteria where(String fieldName)
      Static factory method to create a new Criteria for field with given name
      Parameters:
      fieldName - field to create the Criteria for
    • where

      public static Criteria where(Field field)
      Static factory method to create a new Criteria for provided field
      Parameters:
      field - field to create the Criteria for
    • getField

      @Nullable public Field getField()
      Returns:
      the Field targeted by this Criteria
    • getQueryCriteriaEntries

      public Set<Criteria.CriteriaEntry> getQueryCriteriaEntries()
    • getFilterCriteriaEntries

      public Set<Criteria.CriteriaEntry> getFilterCriteriaEntries()
    • getOperator

      public Criteria.Operator getOperator()
    • getCriteriaChain

      public List<Criteria> getCriteriaChain()
    • not

      public Criteria not()
      Sets the negating flag
      Returns:
      this object
    • isNegating

      public boolean isNegating()
    • boost

      public Criteria boost(float boost)
      Sets the boost factor.
      Parameters:
      boost - boost factor
      Returns:
      this object
    • getBoost

      public float getBoost()
    • isAnd

      public boolean isAnd()
    • isOr

      public boolean isOr()
    • getSubCriteria

      public Set<Criteria> getSubCriteria()
      Returns:
      the set ob subCriteria
      Since:
      4.1
    • and

      public Criteria and(Field field)
      Chain a new and-Criteria
      Parameters:
      field - the field for the new Criteria
      Returns:
      the new chained Criteria
    • and

      public Criteria and(String fieldName)
      Chain a new and- Criteria
      Parameters:
      fieldName - the field for the new Criteria
      Returns:
      the new chained Criteria
    • and

      public Criteria and(Criteria criteria)
      Chain a Criteria to this object.
      Parameters:
      criteria - the Criteria to add
      Returns:
      this object
    • and

      public Criteria and(Criteria... criterias)
      Chain an array of Criteria to this object.
      Parameters:
      criterias - the Criteria to add
      Returns:
      this object
    • or

      public Criteria or(Field field)
      Chain a new or-Criteria
      Parameters:
      field - the field for the new Criteria
      Returns:
      the new chained Criteria
    • or

      public Criteria or(String fieldName)
      Chain a new or-Criteria
      Parameters:
      fieldName - the field for the new Criteria
      Returns:
      the new chained Criteria
    • or

      public Criteria or(Criteria criteria)
      Chain a new or-Criteria. The new Criteria uses the getField(), getQueryCriteriaEntries() and getFilterCriteriaEntries() of the passed in parameter. the new created criteria is added to the criteria chain.
      Parameters:
      criteria - contains the information for the new Criteria
      Returns:
      the new chained criteria
    • subCriteria

      public Criteria subCriteria(Criteria criteria)
      adds a Criteria as subCriteria
      Parameters:
      criteria - the criteria to add, must not be null
      Returns:
      this object
      Since:
      4.1
    • is

      public Criteria is(Object o)
      Add a Criteria.OperationKey.EQUALS entry to the queryCriteriaEntries
      Parameters:
      o - the argument to the operation
      Returns:
      this object
    • exists

      public Criteria exists()
      Add a Criteria.OperationKey.EXISTS entry to the queryCriteriaEntries
      Returns:
      this object
      Since:
      4.0
    • between

      public Criteria between(@Nullable Object lowerBound, @Nullable Object upperBound)
      Adds a OperationKey.BETWEEN entry to the queryCriteriaEntries. Only one of the parameters may be null to define an unbounded end of the range.
      Parameters:
      lowerBound - the lower bound of the range, null for unbounded
      upperBound - the upper bound of the range, null for unbounded
      Returns:
      this object
    • startsWith

      public Criteria startsWith(String s)
      Add a Criteria.OperationKey.STARTS_WITH entry to the queryCriteriaEntries
      Parameters:
      s - the argument to the operation
      Returns:
      this object
    • contains

      public Criteria contains(String s)
      Add a Criteria.OperationKey.CONTAINS entry to the queryCriteriaEntries
      NOTE: mind your schema as leading wildcards may not be supported and/or execution might be slow.
      Parameters:
      s - the argument to the operation
      Returns:
      this object
    • endsWith

      public Criteria endsWith(String s)
      Add a Criteria.OperationKey.ENDS_WITH entry to the queryCriteriaEntries
      NOTE: mind your schema as leading wildcards may not be supported and/or execution might be slow.
      Parameters:
      s - the argument to the operation
      Returns:
      this object
    • in

      public Criteria in(Object... values)
      Add a Criteria.OperationKey.IN entry to the queryCriteriaEntries. This will create a terms query, so don't use it with text fields as these are analyzed and changed by Elasticsearch (converted to lowercase with the default analyzer). If used for Strings, these should be marked as field type Keyword.
      Parameters:
      values - the argument to the operation
      Returns:
      this object
    • in

      public Criteria in(Iterable<?> values)
      Add a Criteria.OperationKey.IN entry to the queryCriteriaEntries. See the comment at in(Object...).
      Parameters:
      values - the argument to the operation
      Returns:
      this object
    • notIn

      public Criteria notIn(Object... values)
      Add a Criteria.OperationKey.NOT_IN entry to the queryCriteriaEntries. See the comment at in(Object...).
      Parameters:
      values - the argument to the operation
      Returns:
      this object
    • notIn

      public Criteria notIn(Iterable<?> values)
      Add a Criteria.OperationKey.NOT_IN entry to the queryCriteriaEntries. See the comment at in(Object...).
      Parameters:
      values - the argument to the operation
      Returns:
      this object
    • expression

      public Criteria expression(String s)
      Add a Criteria.OperationKey.EXPRESSION entry to the queryCriteriaEntries allowing native elasticsearch expressions
      Parameters:
      s - the argument to the operation
      Returns:
      this object
    • fuzzy

      public Criteria fuzzy(String s)
      Add a Criteria.OperationKey.FUZZY entry to the queryCriteriaEntries
      Parameters:
      s - the argument to the operation
      Returns:
      this object
    • lessThanEqual

      public Criteria lessThanEqual(Object upperBound)
      Add a Criteria.OperationKey.LESS_EQUAL entry to the queryCriteriaEntries
      Parameters:
      upperBound - the argument to the operation
      Returns:
      this object
    • lessThan

      public Criteria lessThan(Object upperBound)
      Add a Criteria.OperationKey.LESS entry to the queryCriteriaEntries
      Parameters:
      upperBound - the argument to the operation
      Returns:
      this object
    • greaterThanEqual

      public Criteria greaterThanEqual(Object lowerBound)
      Add a Criteria.OperationKey.GREATER_EQUAL entry to the queryCriteriaEntries
      Parameters:
      lowerBound - the argument to the operation
      Returns:
      this object
    • greaterThan

      public Criteria greaterThan(Object lowerBound)
      Add a Criteria.OperationKey.GREATER entry to the queryCriteriaEntries
      Parameters:
      lowerBound - the argument to the operation
      Returns:
      this object
    • matches

      public Criteria matches(Object value)
      Add a Criteria.OperationKey.MATCHES entry to the queryCriteriaEntries. This will build a match query with the OR operator.
      Parameters:
      value - the value to match
      Returns:
      this object
      Since:
      4.1
    • matchesAll

      public Criteria matchesAll(Object value)
      Add a Criteria.OperationKey.MATCHES entry to the queryCriteriaEntries. This will build a match query with the AND operator.
      Parameters:
      value - the value to match
      Returns:
      this object
      Since:
      4.1
    • empty

      public Criteria empty()
      Add a Criteria.OperationKey.EMPTY entry to the queryCriteriaEntries.
      Returns:
      this object
      Since:
      4.3
    • notEmpty

      public Criteria notEmpty()
      Add a Criteria.OperationKey.NOT_EMPTY entry to the queryCriteriaEntries.
      Returns:
      this object
      Since:
      4.3
    • regexp

      public Criteria regexp(String value)
      Add a Criteria.OperationKey.REGEXP entry to the queryCriteriaEntries.
      Parameters:
      value - the regexp value to match
      Returns:
      this object
      Since:
      5.1
    • boundedBy

      public Criteria boundedBy(GeoBox boundingBox)
      Adds a new filter CriteriaEntry for location GeoBox bounding box
      Parameters:
      boundingBox - GeoBox bounding box(left top corner + right bottom corner)
      Returns:
      this object
    • boundedBy

      public Criteria boundedBy(Box boundingBox)
      Adds a new filter CriteriaEntry for location Box bounding box
      Parameters:
      boundingBox - GeoBox bounding box(left top corner + right bottom corner)
      Returns:
      this object
    • boundedBy

      public Criteria boundedBy(String topLeftGeohash, String bottomRightGeohash)
      Adds a new filter CriteriaEntry for bounding box created from points
      Parameters:
      topLeftGeohash - left top corner of bounding box as geohash
      bottomRightGeohash - right bottom corner of bounding box as geohash
      Returns:
      this object
    • boundedBy

      public Criteria boundedBy(GeoPoint topLeftPoint, GeoPoint bottomRightPoint)
      Adds a new filter CriteriaEntry for bounding box created from points
      Parameters:
      topLeftPoint - left top corner of bounding box
      bottomRightPoint - right bottom corner of bounding box
      Returns:
      this object
    • boundedBy

      public Criteria boundedBy(Point topLeftPoint, Point bottomRightPoint)
      Adds a new filter CriteriaEntry for bounding box created from points
      Parameters:
      topLeftPoint - left top corner of bounding box
      bottomRightPoint - right bottom corner of bounding box
      Returns:
      this object
    • within

      public Criteria within(GeoPoint location, String distance)
      Adds a new filter CriteriaEntry for location WITHIN distance
      Parameters:
      location - GeoPoint center coordinates
      distance - String radius as a string (e.g. : '100km'). Distance unit : either mi/miles or km can be set
      Returns:
      this object
    • within

      public Criteria within(Point location, Distance distance)
      Adds a new filter CriteriaEntry for location WITHIN distance
      Parameters:
      location - Point center coordinates
      distance - Distance radius .
      Returns:
      this object
    • within

      public Criteria within(String geoLocation, String distance)
      Adds a new filter CriteriaEntry for geoLocation WITHIN distance
      Parameters:
      geoLocation - String center point supported formats: lat on = > "41.2,45.1", geohash = > "asd9as0d"
      distance - String radius as a string (e.g. : '100km'). Distance unit : either mi/miles or km can be set
      Returns:
      this object
    • intersects

      public Criteria intersects(GeoJson<?> geoShape)
      Adds a new filter CriteriaEntry for GEO_INTERSECTS.
      Parameters:
      geoShape - the GeoJson shape
      Returns:
      this object
    • isDisjoint

      public Criteria isDisjoint(GeoJson<?> geoShape)
      Adds a new filter CriteriaEntry for GEO_IS_DISJOINT.
      Parameters:
      geoShape - the GeoJson shape
      Returns:
      this object
    • within

      public Criteria within(GeoJson<?> geoShape)
      Adds a new filter CriteriaEntry for GEO_WITHIN.
      Parameters:
      geoShape - the GeoJson shape
      Returns:
      this object
    • contains

      public Criteria contains(GeoJson<?> geoShape)
      Adds a new filter CriteriaEntry for GEO_CONTAINS.
      Parameters:
      geoShape - the GeoJson shape
      Returns:
      this object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object