Class FieldSortBuilder

java.lang.Object
org.elasticsearch.search.sort.SortBuilder<FieldSortBuilder>
org.elasticsearch.search.sort.FieldSortBuilder
All Implemented Interfaces:
NamedWriteable, Writeable, Rewriteable<SortBuilder<?>>, ToXContent, ToXContentObject

public class FieldSortBuilder extends SortBuilder<FieldSortBuilder>
A sort builder to sort based on a document field.
  • Field Details

  • Constructor Details

    • FieldSortBuilder

      public FieldSortBuilder(FieldSortBuilder template)
      Copy constructor.
    • FieldSortBuilder

      public FieldSortBuilder(String fieldName)
      Constructs a new sort based on a document field.
      Parameters:
      fieldName - The field name.
    • FieldSortBuilder

      public FieldSortBuilder(StreamInput in) throws IOException
      Read from a stream.
      Throws:
      IOException
  • Method Details

    • writeTo

      public void writeTo(StreamOutput out) throws IOException
      Description copied from interface: Writeable
      Write this into the StreamOutput.
      Throws:
      IOException
    • getFieldName

      public String getFieldName()
      Returns the document field this sort should be based on.
    • missing

      public FieldSortBuilder missing(Object missing)
      Sets the value when a field is missing in a doc. Can also be set to _last or _first to sort missing last or first respectively.
    • missing

      public Object missing()
      Returns the value used when a field is missing in a doc.
    • unmappedType

      public FieldSortBuilder unmappedType(String type)
      Set the type to use in case the current field is not mapped in an index. Specifying a type tells Elasticsearch what type the sort values should have, which is important for cross-index search, if there are sort fields that exist on some indices only. If the unmapped type is null then query execution will fail if one or more indices don't have a mapping for the current field.
    • unmappedType

      public String unmappedType()
      Returns the type to use in case the current field is not mapped in an index.
    • sortMode

      public FieldSortBuilder sortMode(SortMode sortMode)
      Defines what values to pick in the case a document contains multiple values for the targeted sort field. Possible values: min, max, sum and avg

      The last two values are only applicable for number based fields.

    • sortMode

      public SortMode sortMode()
      Returns what values to pick in the case a document contains multiple values for the targeted sort field.
    • setNestedFilter

      @Deprecated public FieldSortBuilder setNestedFilter(QueryBuilder nestedFilter)
      Deprecated.
      set nested sort with setNestedSort(NestedSortBuilder) and retrieve with getNestedSort()
      Sets the nested filter that the nested objects should match with in order to be taken into account for sorting.
    • getNestedFilter

      @Deprecated public QueryBuilder getNestedFilter()
      Deprecated.
      set nested sort with setNestedSort(NestedSortBuilder) and retrieve with getNestedSort()
      Returns the nested filter that the nested objects should match with in order to be taken into account for sorting.
    • setNestedPath

      @Deprecated public FieldSortBuilder setNestedPath(String nestedPath)
      Deprecated.
      set nested sort with setNestedSort(NestedSortBuilder) and retrieve with getNestedSort()
      Sets the nested path if sorting occurs on a field that is inside a nested object. By default when sorting on a field inside a nested object, the nearest upper nested object is selected as nested path.
    • getNestedPath

      @Deprecated public String getNestedPath()
      Deprecated.
      set nested sort with setNestedSort(NestedSortBuilder) and retrieve with getNestedSort()
      Returns the nested path if sorting occurs in a field that is inside a nested object.
    • getNestedSort

      public NestedSortBuilder getNestedSort()
      Returns the NestedSortBuilder
    • setNestedSort

      public FieldSortBuilder setNestedSort(NestedSortBuilder nestedSort)
      Sets the NestedSortBuilder to be used for fields that are inside a nested object. The NestedSortBuilder takes a `path` argument and an optional nested filter that the nested objects should match with in order to be taken into account for sorting.
    • getNumericType

      public String getNumericType()
      Returns the numeric type that values should translated to or null if the original numeric type should be preserved.
    • setNumericType

      public FieldSortBuilder setNumericType(String numericType)
      Forces the numeric type to use for the field. The query will fail if this option is set on a field that is not mapped as a numeric in some indices. Specifying a numeric type tells Elasticsearch what type the sort values should have, which is important for cross-index search, if a field does not have the same type on all indices. Allowed values are long, double, date and date_nanos.
    • getFormat

      public String getFormat()
      Returns the external format that is specified via setFormat(String)
    • setFormat

      public FieldSortBuilder setFormat(String format)
      Specifies a format specification that will be used to format the output value of this sort field. Currently, only "date" and "data_nanos" date types support this external format (i.e., date format).
    • toXContent

      public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException
      Throws:
      IOException
    • build

      public SortFieldAndFormat build(SearchExecutionContext context) throws IOException
      Description copied from class: SortBuilder
      Create a SortFieldAndFormat from this builder.
      Specified by:
      build in class SortBuilder<FieldSortBuilder>
      Throws:
      IOException
    • canRewriteToMatchNone

      public boolean canRewriteToMatchNone()
    • isBottomSortShardDisjoint

      public boolean isBottomSortShardDisjoint(SearchExecutionContext context, SearchSortValuesAndFormats bottomSortValues) throws IOException
      Returns whether some values of the given SearchExecutionContext.getIndexReader() are within the primary sort value provided in the bottomSortValues.
      Throws:
      IOException
    • buildBucketedSort

      public BucketedSort buildBucketedSort(SearchExecutionContext context, BigArrays bigArrays, int bucketSize, BucketedSort.ExtraData extra) throws IOException
      Description copied from class: SortBuilder
      Create a BucketedSort which is useful for sorting inside of aggregations.
      Specified by:
      buildBucketedSort in class SortBuilder<FieldSortBuilder>
      Throws:
      IOException
    • hasPrimaryFieldSort

      public static boolean hasPrimaryFieldSort(SearchSourceBuilder source)
      Return true if the primary sort in the provided source is an instance of FieldSortBuilder.
    • getPrimaryFieldSortOrNull

      public static FieldSortBuilder getPrimaryFieldSortOrNull(SearchSourceBuilder source)
      Return the FieldSortBuilder if the primary sort in the provided source is an instance of this class, null otherwise.
    • getMinMaxOrNull

      public static MinAndMax<?> getMinMaxOrNull(SearchExecutionContext context, FieldSortBuilder sortBuilder) throws IOException
      Return the MinAndMax indexed value from the provided FieldSortBuilder or null if unknown. The value can be extracted on non-nested indexed mapped fields of type keyword, numeric or date, other fields and configurations return null.
      Throws:
      IOException
    • equals

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

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

      public String getWriteableName()
      Description copied from interface: NamedWriteable
      Returns the name of the writeable object
    • fromXContent

      public static FieldSortBuilder fromXContent(XContentParser parser, String fieldName) throws IOException
      Creates a new FieldSortBuilder from the query held by the XContentParser in XContent format.
      Parameters:
      parser - the input parser. The state on the parser contained in this context will be changed as a side effect of this method call
      fieldName - in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument
      Throws:
      IOException
    • rewrite

      public FieldSortBuilder rewrite(QueryRewriteContext ctx) throws IOException
      Description copied from interface: Rewriteable
      Rewrites this instance based on the provided context. The returned objects will be the same instance as this if no changes during the rewrite were applied.
      Throws:
      IOException