1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.appengine
.api
.search
.checkers
.FieldChecker
;
8 * Represents an expression bound to a returned Field with the given
9 * name. FieldExpressions are added to a {@link QueryOptions}, to request an
10 * expression computed and returned as the named Field value. For example,
12 * FieldExpression.newBuilder()
14 * .setExpression("snippet(\"good story\", content)")
17 * binds a snippet expression to a Field named "snippet", which will
18 * be returned in each search result. In this case the returned "snippet"
19 * Field will contain a HTML value containing text snippets of the
20 * "content" field matching the query "good story".
23 public class FieldExpression
{
26 * A field expression builder. A name and expression must be
29 public static final class Builder
{
31 private String expression
;
34 * Sets the name of the expression. The name must be a valid
37 * @param name the name of the expression
38 * @return this Builder
39 * @throws IllegalArgumentException if name is not a valid field name
41 public Builder
setName(String name
) {
42 this.name
= FieldChecker
.checkFieldName(name
);
47 * Sets the expression to evaluate to return in {@link ScoredDocument}.
49 * @param expression an expression to evaluate and return
50 * in a {@link ScoredDocument} field
51 * @return this Builder
52 * @throws IllegalArgumentException if the expression is not valid
54 public Builder
setExpression(String expression
) {
55 this.expression
= FieldChecker
.checkExpression(expression
);
60 * Builds the FieldExpression. An expression and name for the expression
63 * @return the built FieldExpression
64 * @throws IllegalArgumentException if the name is not a valid field name
66 public FieldExpression
build() {
67 return new FieldExpression(this);
71 private final String name
;
72 private final String expression
;
75 * Constructs a FieldExpression from a Builder.
77 * @param builder the builder to set field expression attributes from
78 * @throws IllegalArgumentException if the name is not a valid field name
80 private FieldExpression(Builder builder
) {
81 name
= FieldChecker
.checkFieldName(builder
.name
);
82 expression
= builder
.expression
;
86 * @return the name of the expression
88 public String
getName() {
93 * @return the expression string used to create a field
95 public String
getExpression() {
100 * @return returns a new FieldExpression builder.
102 public static Builder
newBuilder() {
103 return new Builder();
107 * Copies the attributes of this FieldExpression into a protocol buffer
108 * FieldSpec.Expression builder.
110 * @return the field expression protocol buffer builder initialized from
111 * this field expression
113 SearchServicePb
.FieldSpec
.Expression
.Builder
copyToProtocolBuffer() {
114 SearchServicePb
.FieldSpec
.Expression
.Builder builder
=
115 SearchServicePb
.FieldSpec
.Expression
.newBuilder();
116 builder
.setName(getName());
117 builder
.setExpression(getExpression());
122 public String
toString() {
123 return String
.format("FieldExpression(name=%s, expression=%s)", name
, expression
);