Class SortOptions.Builder (2.0.0)

public static final class SortOptions.Builder

A builder that constructs SortOptionss. A SortOptions will evaluate each of the SortExpressions on each search result and apply a sort order with priority given to the sort expressions from left to right. The following code illustrates creating a SortOptions specification to sort documents based on decreasing product rating and then within rating showing cheapest products based on price plus tax, sorting at most 2000 documents.


   SortOptions sortOptions = SortOptions.newBuilder()
       .addSortExpression(SortExpression.newBuilder()
           .setExpression("rating")
           .setDirection(SortExpression.SortDirection.DESCENDING)
           .setDefaultValueNumeric(0))
       .addSortExpression(SortExpression.newBuilder()
           .setExpression("price + tax")
           .setDirection(SortExpression.SortDirection.ASCENDING)
           .setDefaultValueNumeric(99999999.00))
       .setLimit(1000)
       .build();
 

The following code fragment shows how the score from a MatchScorer can be used in an expression that combines the score with one thousandth of an "importance" field. At most 1000 documents are scored and sorted.


   SortOptions sortOptions = SortOptions.newBuilder()
       .setMatchScorer(MatchScorer.newBuilder())
       .addSortExpression(SortExpression.newBuilder()
           .setExpression(String.format(
               "%s + (importance * .001)", SortExpression.SCORE_FIELD_NAME))
           .setDirection(SortExpression.SortDirection.DESCENDING)
           .setDefaultValueNumeric(0))
       .setLimit(1000)
       .build();
 

Inheritance

java.lang.Object > SortOptions.Builder

Methods

addSortExpression(SortExpression sortExpression)

public SortOptions.Builder addSortExpression(SortExpression sortExpression)

Adds a SortExpression to the list of sort expressions.

Parameter
Name Description
sortExpression SortExpression

an expression to sort documents by

Returns
Type Description
SortOptions.Builder

this Builder

addSortExpression(SortExpression.Builder builder)

public SortOptions.Builder addSortExpression(SortExpression.Builder builder)

Adds a SortExpression built from the builder to the list of sort expressions.

Parameter
Name Description
builder SortExpression.Builder

a builder of SortExpression to sort documents by

Returns
Type Description
SortOptions.Builder

this Builder

build()

public SortOptions build()

Builds a SortOptions from the set values.

Returns
Type Description
SortOptions

a SortOptions built from the set values

setLimit(int limit)

public SortOptions.Builder setLimit(int limit)

Sets the limit on the number of documents to score or sort.

Parameter
Name Description
limit int

the maximum number of documents to score or sort

Returns
Type Description
SortOptions.Builder

this builder

setMatchScorer(MatchScorer matchScorer)

public SortOptions.Builder setMatchScorer(MatchScorer matchScorer)

Sets a MatchScorer or RescoringMatchScorer to base some SortExpressions on. The value of the matchScorer can be accessed by using the field name "_score".

Parameter
Name Description
matchScorer MatchScorer

the rescoring/match matchScorer to use in an expression built on "_score"

Returns
Type Description
SortOptions.Builder

this builder

setMatchScorer(MatchScorer.Builder builder)

public SortOptions.Builder setMatchScorer(MatchScorer.Builder builder)

Sets the matchScorer to the MatchScorer built from the builder. See Also: #setMatchScorer(MatchScorer)

Parameter
Name Description
builder MatchScorer.Builder

a builder of a MatchScorer

Returns
Type Description
SortOptions.Builder

this builder

setMatchScorer(RescoringMatchScorer.Builder builder)

public SortOptions.Builder setMatchScorer(RescoringMatchScorer.Builder builder)

Sets the matchScorer to the RescoringMatchScorer built from the builder. See Also: #setMatchScorer(MatchScorer)

Parameter
Name Description
builder RescoringMatchScorer.Builder

a builder of a RescoringMatchScorer

Returns
Type Description
SortOptions.Builder

this builder