summaryrefslogtreecommitdiff
path: root/core/src/main/java/org
diff options
context:
space:
mode:
authorAreek Zillur <areek.zillur@elasticsearch.com>2015-11-03 20:01:47 -0500
committerAreek Zillur <areek.zillur@elasticsearch.com>2015-11-07 17:50:56 -0500
commitd37e01152658a111853de40809fb8ea94b2a0a2e (patch)
tree2b680b4f56476869ae85ca2c4a33c8b68c19d541 /core/src/main/java/org
parent40022e2168a514a9259bde0ab00d808124a10e9a (diff)
add query context builders
Diffstat (limited to 'core/src/main/java/org')
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java4
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java101
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java15
-rw-r--r--core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java166
4 files changed, 162 insertions, 124 deletions
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java
index 62fec54a33..23c9ca730b 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java
@@ -153,11 +153,11 @@ public class CategoryContextMapping extends ContextMapping {
Token token = parser.nextToken();
if (token == Token.START_OBJECT || token == Token.VALUE_STRING) {
CategoryQueryContext parse = CategoryQueryContext.parse(parser);
- queryContexts.add(new QueryContext(parse.context.toString(), parse.boost, parse.isPrefix));
+ queryContexts.add(new QueryContext(parse.getCategory().toString(), parse.getBoost(), parse.isPrefix()));
} else if (token == Token.START_ARRAY) {
while (parser.nextToken() != Token.END_ARRAY) {
CategoryQueryContext parse = CategoryQueryContext.parse(parser);
- queryContexts.add(new QueryContext(parse.context.toString(), parse.boost, parse.isPrefix));
+ queryContexts.add(new QueryContext(parse.getCategory().toString(), parse.getBoost(), parse.isPrefix()));
}
}
return queryContexts;
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java
index 977587ffed..ee2655ebdd 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryQueryContext.java
@@ -33,78 +33,105 @@ import static org.elasticsearch.search.suggest.completion.context.CategoryContex
* Defines the query context for {@link CategoryContextMapping}
*/
public final class CategoryQueryContext implements ToXContent {
+ private final CharSequence category;
+ private final boolean isPrefix;
+ private final int boost;
- public CharSequence context;
-
- public boolean isPrefix = false;
-
- public int boost = 1;
+ private CategoryQueryContext(CharSequence category, int boost, boolean isPrefix) {
+ this.category = category;
+ this.boost = boost;
+ this.isPrefix = isPrefix;
+ }
/**
- * Creates a query context with a provided context and a
- * boost of 1
+ * Returns the category of the context
*/
- public CategoryQueryContext(CharSequence context) {
- this(context, 1);
+ public CharSequence getCategory() {
+ return category;
}
/**
- * Creates a query context with a provided context and boost
+ * Returns if the context should be treated as a prefix
*/
- public CategoryQueryContext(CharSequence context, int boost) {
- this(context, boost, false);
+ public boolean isPrefix() {
+ return isPrefix;
}
/**
- * Creates a query context with a provided context and boost
- * Allows specifying whether the context should be treated as
- * a prefix or not
+ * Returns the query-time boost of the context
*/
- public CategoryQueryContext(CharSequence context, int boost, boolean isPrefix) {
- this.context = context;
- this.boost = boost;
- this.isPrefix = isPrefix;
+ public int getBoost() {
+ return boost;
}
- private CategoryQueryContext() {
+ public static Builder builder() {
+ return new Builder();
}
- void setContext(CharSequence context) {
- this.context = context;
- }
+ public static class Builder {
+ private CharSequence category;
+ private boolean isPrefix = false;
+ private int boost = 1;
- void setIsPrefix(boolean isPrefix) {
- this.isPrefix = isPrefix;
- }
+ public Builder() {
+ }
- void setBoost(int boost) {
- this.boost = boost;
+ /**
+ * Sets the category of the context.
+ * This is a required field
+ */
+ public Builder setCategory(CharSequence context) {
+ this.category = context;
+ return this;
+ }
+
+ /**
+ * Sets if the context should be treated as a prefix or not.
+ * Defaults to false
+ */
+ public Builder setPrefix(boolean prefix) {
+ this.isPrefix = prefix;
+ return this;
+ }
+
+ /**
+ * Sets the query-time boost of the context.
+ * Defaults to 1.
+ */
+ public Builder setBoost(int boost) {
+ this.boost = boost;
+ return this;
+ }
+
+ public CategoryQueryContext build() {
+ return new CategoryQueryContext(category, boost, isPrefix);
+ }
}
- private static ObjectParser<CategoryQueryContext, CategoryContextMapping> CATEGORY_PARSER = new ObjectParser<>("category", null);
+ private static ObjectParser<Builder, Void> CATEGORY_PARSER = new ObjectParser<>("category", null);
static {
- CATEGORY_PARSER.declareString(CategoryQueryContext::setContext, new ParseField("context"));
- CATEGORY_PARSER.declareInt(CategoryQueryContext::setBoost, new ParseField("boost"));
- CATEGORY_PARSER.declareBoolean(CategoryQueryContext::setIsPrefix, new ParseField("prefix"));
+ CATEGORY_PARSER.declareString(Builder::setCategory, new ParseField("context"));
+ CATEGORY_PARSER.declareInt(Builder::setBoost, new ParseField("boost"));
+ CATEGORY_PARSER.declareBoolean(Builder::setPrefix, new ParseField("prefix"));
}
public static CategoryQueryContext parse(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken();
- CategoryQueryContext queryContext = new CategoryQueryContext();
+ Builder builder = builder();
if (token == XContentParser.Token.START_OBJECT) {
- CATEGORY_PARSER.parse(parser, queryContext);
+ CATEGORY_PARSER.parse(parser, builder);
} else if (token == XContentParser.Token.VALUE_STRING) {
- queryContext.setContext(parser.text());
+ builder.setCategory(parser.text());
} else {
throw new ElasticsearchParseException("category context must be an object or string");
}
- return queryContext;
+ return builder.build();
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
- builder.field(CONTEXT_VALUE, context);
+ builder.field(CONTEXT_VALUE, category);
builder.field(CONTEXT_BOOST, boost);
builder.field(CONTEXT_PREFIX, isPrefix);
builder.endObject();
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java
index 149bfa665c..d4ff2f106b 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoContextMapping.java
@@ -245,27 +245,26 @@ public class GeoContextMapping extends ContextMapping {
List<QueryContext> queryContextList = new ArrayList<>();
for (GeoQueryContext queryContext : queryContexts) {
int minPrecision = this.precision;
- if (queryContext.precision != -1) {
- minPrecision = Math.min(minPrecision, queryContext.precision);
+ if (queryContext.getPrecision() != -1) {
+ minPrecision = Math.min(minPrecision, queryContext.getPrecision());
}
- GeoPoint point = queryContext.geoPoint;
+ GeoPoint point = queryContext.getGeoPoint();
final Collection<String> locations = new HashSet<>();
String geoHash = GeoHashUtils.stringEncode(point.getLon(), point.getLat(), minPrecision);
locations.add(geoHash);
- if (queryContext.neighbours.isEmpty() && geoHash.length() == this.precision) {
+ if (queryContext.getNeighbours().isEmpty() && geoHash.length() == this.precision) {
GeoHashUtils.addNeighbors(geoHash, locations);
- } else if (queryContext.neighbours.isEmpty() == false) {
- for (Integer neighbourPrecision : queryContext.neighbours) {
+ } else if (queryContext.getNeighbours().isEmpty() == false) {
+ for (Integer neighbourPrecision : queryContext.getNeighbours()) {
if (neighbourPrecision < geoHash.length()) {
String truncatedGeoHash = geoHash.substring(0, neighbourPrecision);
locations.add(truncatedGeoHash);
GeoHashUtils.addNeighbors(truncatedGeoHash, locations);
}
-
}
}
for (String location : locations) {
- queryContextList.add(new QueryContext(location, queryContext.boost, location.length() < this.precision));
+ queryContextList.add(new QueryContext(location, queryContext.getBoost(), location.length() < this.precision));
}
}
return queryContextList;
diff --git a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java
index 291f65091e..75cab1e8e8 100644
--- a/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java
+++ b/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java
@@ -29,7 +29,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -39,127 +38,140 @@ import static org.elasticsearch.search.suggest.completion.context.GeoContextMapp
* Defines the query context for {@link GeoContextMapping}
*/
public final class GeoQueryContext implements ToXContent {
- public GeoPoint geoPoint;
- public int boost = 1;
- public int precision = -1;
- public List<Integer> neighbours = new ArrayList<>(0);
+ private final GeoPoint geoPoint;
+ private final int boost;
+ private final int precision;
+ private final List<Integer> neighbours;
- /**
- * Creates a query context for a given geo point with a boost of 1
- * and a precision of {@value GeoContextMapping#DEFAULT_PRECISION}
- */
- public GeoQueryContext(GeoPoint geoPoint) {
- this(geoPoint.geohash());
+ private GeoQueryContext(GeoPoint geoPoint, int boost, int precision, List<Integer> neighbours) {
+ this.geoPoint = geoPoint;
+ this.boost = boost;
+ this.precision = precision;
+ this.neighbours = neighbours;
}
/**
- * Creates a query context for a given geo point with a
- * provided boost
+ * Returns the geo point of the context
*/
- public GeoQueryContext(GeoPoint geoPoint, int boost) {
- this(geoPoint.geohash(), boost);
+ public GeoPoint getGeoPoint() {
+ return geoPoint;
}
/**
- * Creates a query context with a given geo hash with a boost of 1
- * and a precision of {@value GeoContextMapping#DEFAULT_PRECISION}
+ * Returns the query-time boost of the context
*/
- public GeoQueryContext(CharSequence geoHash) {
- this(geoHash, 1);
+ public int getBoost() {
+ return boost;
}
/**
- * Creates a query context for a given geo hash with a
- * provided boost
+ * Returns the precision (length) for the geohash
*/
- public GeoQueryContext(CharSequence geoHash, int boost) {
- this(geoHash, boost, -1);
+ public int getPrecision() {
+ return precision;
}
/**
- * Creates a query context for a geo point with
- * a provided boost and enables generating neighbours
- * at specified precisions
+ * Returns the precision levels at which geohash cells neighbours are considered
*/
- public GeoQueryContext(CharSequence geoHash, int boost, int precision, Integer... neighbours) {
- this(GeoPoint.fromGeohash(geoHash.toString()), boost, precision, neighbours);
+ public List<Integer> getNeighbours() {
+ return neighbours;
}
- /**
- * Creates a query context for a geo hash with
- * a provided boost and enables generating neighbours
- * at specified precisions
- */
- public GeoQueryContext(GeoPoint geoPoint, int boost, int precision, Integer... neighbours) {
- this.geoPoint = geoPoint;
- this.boost = boost;
- this.precision = precision;
- Collections.addAll(this.neighbours, neighbours);
+ public static Builder builder() {
+ return new Builder();
}
- private GeoQueryContext() {
- }
+ public static class Builder {
+ private GeoPoint geoPoint;
+ private int boost = 1;
+ private int precision = -1;
+ private List<Integer> neighbours = Collections.emptyList();
- void setBoost(int boost) {
- this.boost = boost;
- }
+ public Builder() {
+ }
- void setPrecision(int precision) {
- this.precision = precision;
- }
+ /**
+ * Sets the query-time boost for the context
+ * Defaults to 1
+ */
+ public Builder setBoost(int boost) {
+ this.boost = boost;
+ return this;
+ }
- void setNeighbours(List<Integer> neighbours) {
- this.neighbours = neighbours;
- }
+ /**
+ * Sets the precision level for computing the geohash from the context geo point.
+ * Defaults to using index-time precision level
+ */
+ public Builder setPrecision(int precision) {
+ this.precision = precision;
+ return this;
+ }
- void setGeoPoint(GeoPoint geoPoint) {
- this.geoPoint = geoPoint;
- }
+ /**
+ * Sets the precision levels at which geohash cells neighbours are considered.
+ * Defaults to only considering neighbours at the index-time precision level
+ */
+ public Builder setNeighbours(List<Integer> neighbours) {
+ this.neighbours = neighbours;
+ return this;
+ }
- private double lat = Double.NaN;
- void setLat(double lat) {
- this.lat = lat;
- }
+ /**
+ * Sets the geo point of the context.
+ * This is a required field
+ */
+ public Builder setGeoPoint(GeoPoint geoPoint) {
+ this.geoPoint = geoPoint;
+ return this;
+ }
- private double lon = Double.NaN;
- void setLon(double lon) {
- this.lon = lon;
- }
+ private double lat = Double.NaN;
+ void setLat(double lat) {
+ this.lat = lat;
+ }
+
+ private double lon = Double.NaN;
+ void setLon(double lon) {
+ this.lon = lon;
+ }
- void finish() {
- if (geoPoint == null) {
- if (Double.isNaN(lat) == false && Double.isNaN(lon) == false) {
- geoPoint = new GeoPoint(lat, lon);
- } else {
- throw new ElasticsearchParseException("no geohash or geo point provided");
+ public GeoQueryContext build() {
+ if (geoPoint == null) {
+ if (Double.isNaN(lat) == false && Double.isNaN(lon) == false) {
+ geoPoint = new GeoPoint(lat, lon);
+ } else {
+ throw new IllegalArgumentException("no geohash or geo point provided");
+ }
}
+ return new GeoQueryContext(geoPoint, boost, precision, neighbours);
}
}
- private static ObjectParser<GeoQueryContext, GeoContextMapping> GEO_CONTEXT_PARSER = new ObjectParser<>("geo", null);
+ private static ObjectParser<GeoQueryContext.Builder, Void> GEO_CONTEXT_PARSER = new ObjectParser<>("geo", null);
static {
GEO_CONTEXT_PARSER.declareField((parser, geoQueryContext, geoContextMapping) -> geoQueryContext.setGeoPoint(GeoUtils.parseGeoPoint(parser)), new ParseField("context"), ObjectParser.ValueType.OBJECT);
- GEO_CONTEXT_PARSER.declareInt(GeoQueryContext::setBoost, new ParseField("boost"));
+ GEO_CONTEXT_PARSER.declareInt(GeoQueryContext.Builder::setBoost, new ParseField("boost"));
// TODO : add string support for precision for GeoUtils.geoHashLevelsForPrecision()
- GEO_CONTEXT_PARSER.declareInt(GeoQueryContext::setPrecision, new ParseField("precision"));
+ GEO_CONTEXT_PARSER.declareInt(GeoQueryContext.Builder::setPrecision, new ParseField("precision"));
// TODO : add string array support for precision for GeoUtils.geoHashLevelsForPrecision()
- GEO_CONTEXT_PARSER.declareIntArray(GeoQueryContext::setNeighbours, new ParseField("neighbours"));
- GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext::setLat, new ParseField("lat"));
- GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext::setLon, new ParseField("lon"));
+ GEO_CONTEXT_PARSER.declareIntArray(GeoQueryContext.Builder::setNeighbours, new ParseField("neighbours"));
+ GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext.Builder::setLat, new ParseField("lat"));
+ GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext.Builder::setLon, new ParseField("lon"));
}
public static GeoQueryContext parse(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken();
- GeoQueryContext queryContext = new GeoQueryContext();
+ GeoQueryContext.Builder builder = new Builder();
if (token == XContentParser.Token.START_OBJECT) {
- GEO_CONTEXT_PARSER.parse(parser, queryContext);
+ GEO_CONTEXT_PARSER.parse(parser, builder);
} else if (token == XContentParser.Token.VALUE_STRING) {
- queryContext.setGeoPoint(GeoPoint.fromGeohash(parser.text()));
+ builder.setGeoPoint(GeoPoint.fromGeohash(parser.text()));
} else {
throw new ElasticsearchParseException("geo context must be an object or string");
}
- queryContext.finish();
- return queryContext;
+ return builder.build();
}
@Override