Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / MatchScorer.java
blob6bb37a2e38efd969e60990055c42d5774c3c4057
1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search;
5 /**
6 * Assigns a document score based on term frequency.
8 * If you add a MatchScorer to a SortOptions as in the following code:
9 * <pre>
10 * SortOptions sortOptions = SortOptions.newBuilder()
11 * .setMatchScorer(MatchScorer().newBuilder())
12 * .build();
13 * </pre>
14 * then this will sort the documents in descending score order. The scores
15 * will be positive. If you want to sort in ascending order, then use the
16 * following code:
17 * <pre>
18 * SortOptions sortOptions = SortOptions.newBuilder()
19 * .setMatchScorer(MatchScorer.newBuilder())
20 * .addSortExpression(
21 * SortExpression.newBuilder()
22 * .setExpression(SortExpression.SCORE_FIELD_NAME)
23 * .setDirection(SortExpression.SortDirection.ASCENDING)
24 * .setDefaultValueNumeric(0.0))
25 * .build();
26 * </pre>
27 * The scores in this case will be negative.
30 public class MatchScorer {
32 /**
33 * A builder that constructs {@link MatchScorer MatchScorers}.
34 * A MatchScorer will invoke a scorer on each search result. The
35 * following code illustrates building a match scorer to score documents:
36 * <p>
37 * <pre>
38 * MatchScorer scorer = MatchScorer.newBuilder().build();
39 * </pre>
41 public static class Builder {
43 Builder() {
46 /**
47 * Builds a {@link MatchScorer} from the set values.
49 * @return a {@link MatchScorer} built from the set values
51 public MatchScorer build() {
52 return new MatchScorer(this);
56 /**
57 * Constructs a text sort specification using the values from the
58 * {@link Builder}.
60 MatchScorer(Builder builder) {
63 /**
64 * Copies the contents of the MatchScorer into a scorer
65 * spec protocol buffer.
67 * @return the protocol buffer builder with the contents of the MatchScorer
68 * scoring information
70 SearchServicePb.ScorerSpec.Builder copyToScorerSpecProtocolBuffer() {
71 SearchServicePb.ScorerSpec.Builder builder = SearchServicePb.ScorerSpec.newBuilder();
72 builder.setScorer(SearchServicePb.ScorerSpec.Scorer.MATCH_SCORER);
73 return builder;
76 /**
77 * Creates and returns a MatchScorer Builder.
79 * @return a new {@link MatchScorer.Builder}. Set the parameters for scorer
80 * on the Builder, and use the {@link Builder#build()} method
81 * to create a concrete instance of MatchScorer
83 public static Builder newBuilder() {
84 return new Builder();
87 @Override
88 public String toString() {
89 return "MatchScorer()";