diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java index 8b32311ed9c..39776421781 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchResponse.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.facets.Facets; import org.elasticsearch.search.internal.InternalSearchResponse; @@ -161,24 +162,36 @@ public class SearchResponse implements ActionResponse, ToXContent { return scrollId; } + static final class Fields { + static final XContentBuilderString _SCROLL_ID = new XContentBuilderString("_scroll_id"); + static final XContentBuilderString _SHARDS = new XContentBuilderString("_shards"); + static final XContentBuilderString TOTAL = new XContentBuilderString("total"); + static final XContentBuilderString SUCCESSFUL = new XContentBuilderString("successful"); + static final XContentBuilderString FAILED = new XContentBuilderString("failed"); + static final XContentBuilderString FAILURES = new XContentBuilderString("failures"); + static final XContentBuilderString INDEX = new XContentBuilderString("index"); + static final XContentBuilderString SHARD = new XContentBuilderString("shard"); + static final XContentBuilderString REASON = new XContentBuilderString("reason"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { if (scrollId != null) { - builder.field("_scrollId", scrollId); + builder.field(Fields._SCROLL_ID, scrollId); } - builder.startObject("_shards"); - builder.field("total", totalShards()); - builder.field("successful", successfulShards()); - builder.field("failed", failedShards()); + builder.startObject(Fields._SHARDS); + builder.field(Fields.TOTAL, totalShards()); + builder.field(Fields.SUCCESSFUL, successfulShards()); + builder.field(Fields.FAILED, failedShards()); if (shardFailures.length > 0) { - builder.startArray("failures"); + builder.startArray(Fields.FAILURES); for (ShardSearchFailure shardFailure : shardFailures) { builder.startObject(); if (shardFailure.shard() != null) { - builder.field("index", shardFailure.shard().index()); - builder.field("shard", shardFailure.shard().shardId()); + builder.field(Fields.INDEX, shardFailure.shard().index()); + builder.field(Fields.SHARD, shardFailure.shard().shardId()); } - builder.field("reason", shardFailure.reason()); + builder.field(Fields.REASON, shardFailure.reason()); builder.endObject(); } builder.endArray(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 9bbd5f68f20..dca059e94a3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -103,7 +103,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder startObject(XContentString name) throws IOException { + public XContentBuilder startObject(XContentBuilderString name) throws IOException { field(name); startObject(); return this; @@ -128,7 +128,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder array(XContentString name, String... values) throws IOException { + public XContentBuilder array(XContentBuilderString name, String... values) throws IOException { startArray(name); for (String value : values) { value(value); @@ -146,7 +146,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder array(XContentString name, Object... values) throws IOException { + public XContentBuilder array(XContentBuilderString name, Object... values) throws IOException { startArray(name); for (Object value : values) { value(value); @@ -161,7 +161,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder startArray(XContentString name) throws IOException { + public XContentBuilder startArray(XContentBuilderString name) throws IOException { field(name); startArray(); return this; @@ -177,19 +177,13 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name) throws IOException { + public XContentBuilder field(XContentBuilderString name) throws IOException { if (fieldCaseConversion == FieldCaseConversion.UNDERSCORE) { - if (cachedStringBuilder == null) { - cachedStringBuilder = new StringBuilder(); - } - generator.writeFieldName(Strings.toUnderscoreCase(name.getValue(), cachedStringBuilder)); + generator.writeFieldName(name.underscore()); } else if (fieldCaseConversion == FieldCaseConversion.CAMELCASE) { - if (cachedStringBuilder == null) { - cachedStringBuilder = new StringBuilder(); - } - generator.writeFieldName(Strings.toCamelCase(name.getValue(), cachedStringBuilder)); + generator.writeFieldName(name.camelCase()); } else { - generator.writeFieldName(name); + generator.writeFieldName(name.underscore()); } return this; } @@ -220,7 +214,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, char[] value, int offset, int length) throws IOException { + public XContentBuilder field(XContentBuilderString name, char[] value, int offset, int length) throws IOException { field(name); if (value == null) { generator.writeNull(); @@ -240,7 +234,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, String value) throws IOException { + public XContentBuilder field(XContentBuilderString name, String value) throws IOException { field(name); if (value == null) { generator.writeNull(); @@ -254,7 +248,7 @@ public final class XContentBuilder { return field(name, value.intValue()); } - public XContentBuilder field(XContentString name, Integer value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Integer value) throws IOException { return field(name, value.intValue()); } @@ -264,7 +258,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, int value) throws IOException { + public XContentBuilder field(XContentBuilderString name, int value) throws IOException { field(name); generator.writeNumber(value); return this; @@ -274,7 +268,7 @@ public final class XContentBuilder { return field(name, value.longValue()); } - public XContentBuilder field(XContentString name, Long value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Long value) throws IOException { return field(name, value.longValue()); } @@ -284,7 +278,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, long value) throws IOException { + public XContentBuilder field(XContentBuilderString name, long value) throws IOException { field(name); generator.writeNumber(value); return this; @@ -294,7 +288,7 @@ public final class XContentBuilder { return field(name, value.floatValue()); } - public XContentBuilder field(XContentString name, Float value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Float value) throws IOException { return field(name, value.floatValue()); } @@ -304,7 +298,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, float value) throws IOException { + public XContentBuilder field(XContentBuilderString name, float value) throws IOException { field(name); generator.writeNumber(value); return this; @@ -314,7 +308,7 @@ public final class XContentBuilder { return field(name, value.doubleValue()); } - public XContentBuilder field(XContentString name, Double value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Double value) throws IOException { return field(name, value.doubleValue()); } @@ -324,7 +318,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, double value) throws IOException { + public XContentBuilder field(XContentBuilderString name, double value) throws IOException { field(name); generator.writeNumber(value); return this; @@ -336,7 +330,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, Map value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Map value) throws IOException { field(name); value(value); return this; @@ -351,7 +345,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, List value) throws IOException { + public XContentBuilder field(XContentBuilderString name, List value) throws IOException { startArray(name); for (Object o : value) { value(o); @@ -369,7 +363,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, String... value) throws IOException { + public XContentBuilder field(XContentBuilderString name, String... value) throws IOException { startArray(name); for (String o : value) { value(o); @@ -387,7 +381,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, Object... value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Object... value) throws IOException { startArray(name); for (Object o : value) { value(o); @@ -405,7 +399,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, int... value) throws IOException { + public XContentBuilder field(XContentBuilderString name, int... value) throws IOException { startArray(name); for (Object o : value) { value(o); @@ -423,7 +417,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, long... value) throws IOException { + public XContentBuilder field(XContentBuilderString name, long... value) throws IOException { startArray(name); for (Object o : value) { value(o); @@ -441,7 +435,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, float... value) throws IOException { + public XContentBuilder field(XContentBuilderString name, float... value) throws IOException { startArray(name); for (Object o : value) { value(o); @@ -459,7 +453,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, double... value) throws IOException { + public XContentBuilder field(XContentBuilderString name, double... value) throws IOException { startArray(name); for (Object o : value) { value(o); @@ -512,7 +506,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, Object value) throws IOException { + public XContentBuilder field(XContentBuilderString name, Object value) throws IOException { if (value == null) { nullField(name); return this; @@ -593,7 +587,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, boolean value) throws IOException { + public XContentBuilder field(XContentBuilderString name, boolean value) throws IOException { field(name); generator.writeBoolean(value); return this; @@ -605,7 +599,7 @@ public final class XContentBuilder { return this; } - public XContentBuilder field(XContentString name, byte[] value) throws IOException { + public XContentBuilder field(XContentBuilderString name, byte[] value) throws IOException { field(name); generator.writeBinary(value); return this; @@ -616,7 +610,7 @@ public final class XContentBuilder { return value(date); } - public XContentBuilder field(XContentString name, ReadableInstant date) throws IOException { + public XContentBuilder field(XContentBuilderString name, ReadableInstant date) throws IOException { field(name); return value(date); } @@ -626,7 +620,7 @@ public final class XContentBuilder { return value(date, formatter); } - public XContentBuilder field(XContentString name, ReadableInstant date, DateTimeFormatter formatter) throws IOException { + public XContentBuilder field(XContentBuilderString name, ReadableInstant date, DateTimeFormatter formatter) throws IOException { field(name); return value(date, formatter); } @@ -636,7 +630,7 @@ public final class XContentBuilder { return value(date); } - public XContentBuilder field(XContentString name, Date date) throws IOException { + public XContentBuilder field(XContentBuilderString name, Date date) throws IOException { field(name); return value(date); } @@ -646,7 +640,7 @@ public final class XContentBuilder { return value(date, formatter); } - public XContentBuilder field(XContentString name, Date date, DateTimeFormatter formatter) throws IOException { + public XContentBuilder field(XContentBuilderString name, Date date, DateTimeFormatter formatter) throws IOException { field(name); return value(date, formatter); } @@ -656,8 +650,9 @@ public final class XContentBuilder { return this; } - public XContentBuilder nullField(XContentString name) throws IOException { - generator.writeNullField(name); + public XContentBuilder nullField(XContentBuilderString name) throws IOException { + field(name); + generator.writeNull(); return this; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilderString.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilderString.java new file mode 100644 index 00000000000..4cd514e351b --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilderString.java @@ -0,0 +1,45 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.xcontent; + +import org.elasticsearch.common.Strings; + +/** + * @author kimchy (shay.banon) + */ +public class XContentBuilderString { + + private final XContentString underscore; + + private final XContentString camelCase; + + public XContentBuilderString(String value) { + underscore = new XContentString(Strings.toUnderscoreCase(value)); + camelCase = new XContentString(Strings.toCamelCase(value)); + } + + public XContentString underscore() { + return underscore; + } + + public XContentString camelCase() { + return camelCase; + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/filter/InternalFilterFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/filter/InternalFilterFacet.java index 34fc6921d56..a5b89d5bf91 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/filter/InternalFilterFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/filter/InternalFilterFacet.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.facets.filter; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -88,10 +89,15 @@ public class InternalFilterFacet implements FilterFacet, InternalFacet { return new InternalFilterFacet(name, count); } + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", FilterFacetCollectorParser.NAME); - builder.field("count", count); + builder.field(Fields._TYPE, FilterFacetCollectorParser.NAME); + builder.field(Fields.COUNT, count); builder.endObject(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/InternalGeoDistanceFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/InternalGeoDistanceFacet.java index 8ca6c005e48..45c0dfd9248 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/InternalGeoDistanceFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/InternalGeoDistanceFacet.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -159,24 +160,38 @@ public class InternalGeoDistanceFacet implements GeoDistanceFacet, InternalFacet } } + + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _FIELD = new XContentBuilderString("_field"); + static final XContentBuilderString _VALUE_FIELD = new XContentBuilderString("_value_field"); + static final XContentBuilderString _UNIT = new XContentBuilderString("_unit"); + static final XContentBuilderString RANGES = new XContentBuilderString("ranges"); + static final XContentBuilderString FROM = new XContentBuilderString("from"); + static final XContentBuilderString TO = new XContentBuilderString("to"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + static final XContentBuilderString TOTAL = new XContentBuilderString("total"); + static final XContentBuilderString MEAN = new XContentBuilderString("mean"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", "geo_distance"); - builder.field("_field", fieldName); - builder.field("_value_field", valueFieldName); - builder.field("_unit", unit); - builder.startArray("ranges"); + builder.field(Fields._TYPE, "geo_distance"); + builder.field(Fields._FIELD, fieldName); + builder.field(Fields._VALUE_FIELD, valueFieldName); + builder.field(Fields._UNIT, unit); + builder.startArray(Fields.RANGES); for (Entry entry : entries) { builder.startObject(); if (!Double.isInfinite(entry.from)) { - builder.field("from", entry.from); + builder.field(Fields.FROM, entry.from); } if (!Double.isInfinite(entry.to)) { - builder.field("to", entry.to); + builder.field(Fields.TO, entry.to); } - builder.field("count", entry.count()); - builder.field("total", entry.total()); - builder.field("mean", entry.mean()); + builder.field(Fields.COUNT, entry.count()); + builder.field(Fields.TOTAL, entry.total()); + builder.field(Fields.MEAN, entry.mean()); builder.endObject(); } builder.endArray(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/InternalHistogramFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/InternalHistogramFacet.java index c282d308339..b9d83e4331e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/InternalHistogramFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/InternalHistogramFacet.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.trove.TLongDoubleIterator; import org.elasticsearch.common.trove.TLongLongHashMap; import org.elasticsearch.common.trove.TLongLongIterator; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -174,20 +175,33 @@ public class InternalHistogramFacet implements HistogramFacet, InternalFacet { return new InternalHistogramFacet(name, keyFieldName, valueFieldName, interval, comparatorType, counts, totals); } + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _KEY_FIELD = new XContentBuilderString("_key_field"); + static final XContentBuilderString _VALUE_FIELD = new XContentBuilderString("_value_field"); + static final XContentBuilderString _COMPARATOR = new XContentBuilderString("_comparator"); + static final XContentBuilderString _INTERVAL = new XContentBuilderString("_interval"); + static final XContentBuilderString ENTRIES = new XContentBuilderString("entries"); + static final XContentBuilderString KEY = new XContentBuilderString("key"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + static final XContentBuilderString TOTAL = new XContentBuilderString("total"); + static final XContentBuilderString MEAN = new XContentBuilderString("mean"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", HistogramFacetCollectorParser.NAME); - builder.field("_key_field", keyFieldName); - builder.field("_value_field", valueFieldName); - builder.field("_comparator", comparatorType.description()); - builder.field("_interval", interval); - builder.startArray("entries"); + builder.field(Fields._TYPE, HistogramFacetCollectorParser.NAME); + builder.field(Fields._KEY_FIELD, keyFieldName); + builder.field(Fields._VALUE_FIELD, valueFieldName); + builder.field(Fields._COMPARATOR, comparatorType.description()); + builder.field(Fields._INTERVAL, interval); + builder.startArray(Fields.ENTRIES); for (Entry entry : computeEntries()) { builder.startObject(); - builder.field("key", entry.key()); - builder.field("count", entry.count()); - builder.field("total", entry.total()); - builder.field("mean", entry.mean()); + builder.field(Fields.KEY, entry.key()); + builder.field(Fields.COUNT, entry.count()); + builder.field(Fields.TOTAL, entry.total()); + builder.field(Fields.MEAN, entry.mean()); builder.endObject(); } builder.endArray(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/internal/InternalFacets.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/internal/InternalFacets.java index 551efb454c8..e0851fc2f1f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/internal/InternalFacets.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/internal/InternalFacets.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.Facets; import org.elasticsearch.search.facets.filter.InternalFilterFacet; @@ -114,8 +115,12 @@ public class InternalFacets implements Facets, Streamable, ToXContent, Iterable< return (T) facetsAsMap().get(name); } + static final class Fields { + static final XContentBuilderString FACETS = new XContentBuilderString("facets"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject("facets"); + builder.startObject(Fields.FACETS); for (Facet facet : facets) { ((InternalFacet) facet).toXContent(builder, params); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/query/InternalQueryFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/query/InternalQueryFacet.java index 8fd890acaa8..427ad42fc2c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/query/InternalQueryFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/query/InternalQueryFacet.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.facets.query; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -88,10 +89,15 @@ public class InternalQueryFacet implements QueryFacet, InternalFacet { return new InternalQueryFacet(name, count); } + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", QueryFacetCollectorParser.NAME); - builder.field("count", count); + builder.field(Fields._TYPE, QueryFacetCollectorParser.NAME); + builder.field(Fields.COUNT, count); builder.endObject(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/InternalRangeFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/InternalRangeFacet.java index 8cb93001cb0..f7a71e3544f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/InternalRangeFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/InternalRangeFacet.java @@ -23,6 +23,7 @@ import org.elasticsearch.common.collect.ImmutableList; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -168,29 +169,43 @@ public class InternalRangeFacet implements RangeFacet, InternalFacet { } } + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _KEY_FIELD = new XContentBuilderString("_key_field"); + static final XContentBuilderString _VALUE_FIELD = new XContentBuilderString("_value_field"); + static final XContentBuilderString RANGES = new XContentBuilderString("ranges"); + static final XContentBuilderString FROM = new XContentBuilderString("from"); + static final XContentBuilderString FROM_STR = new XContentBuilderString("from_str"); + static final XContentBuilderString TO = new XContentBuilderString("to"); + static final XContentBuilderString TO_STR = new XContentBuilderString("to_str"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + static final XContentBuilderString TOTAL = new XContentBuilderString("total"); + static final XContentBuilderString MEAN = new XContentBuilderString("mean"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", "range"); - builder.field("_key_field", keyFieldName); - builder.field("_value_field", valueFieldName); - builder.startArray("ranges"); + builder.field(Fields._TYPE, "range"); + builder.field(Fields._KEY_FIELD, keyFieldName); + builder.field(Fields._VALUE_FIELD, valueFieldName); + builder.startArray(Fields.RANGES); for (Entry entry : entries) { builder.startObject(); if (!Double.isInfinite(entry.from)) { - builder.field("from", entry.from); + builder.field(Fields.FROM, entry.from); } if (entry.fromAsString != null) { - builder.field("from_str", entry.fromAsString); + builder.field(Fields.FROM_STR, entry.fromAsString); } if (!Double.isInfinite(entry.to)) { - builder.field("to", entry.to); + builder.field(Fields.TO, entry.to); } if (entry.toAsString != null) { - builder.field("to_str", entry.toAsString); + builder.field(Fields.TO_STR, entry.toAsString); } - builder.field("count", entry.count()); - builder.field("total", entry.total()); - builder.field("mean", entry.mean()); + builder.field(Fields.COUNT, entry.count()); + builder.field(Fields.TOTAL, entry.total()); + builder.field(Fields.MEAN, entry.mean()); builder.endObject(); } builder.endArray(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/InternalStatisticalFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/InternalStatisticalFacet.java index c31cf071469..cb2e7a26b1a 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/InternalStatisticalFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/InternalStatisticalFacet.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.facets.statistical; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -173,18 +174,31 @@ public class InternalStatisticalFacet implements StatisticalFacet, InternalFacet return new InternalStatisticalFacet(name, fieldName, min, max, total, sumOfSquares, count); } + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _FIELD = new XContentBuilderString("_field"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + static final XContentBuilderString TOTAL = new XContentBuilderString("total"); + static final XContentBuilderString MIN = new XContentBuilderString("min"); + static final XContentBuilderString MAX = new XContentBuilderString("max"); + static final XContentBuilderString MEAN = new XContentBuilderString("mean"); + static final XContentBuilderString SUM_OF_SQUARES = new XContentBuilderString("sum_of_squares"); + static final XContentBuilderString VARIANCE = new XContentBuilderString("variance"); + static final XContentBuilderString STD_DEVIATION = new XContentBuilderString("std_deviation"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", StatisticalFacetCollectorParser.NAME); - builder.field("_field", fieldName); - builder.field("count", count()); - builder.field("total", total()); - builder.field("min", min()); - builder.field("max", max()); - builder.field("mean", mean()); - builder.field("sum_of_squares", sumOfSquares()); - builder.field("variance", variance()); - builder.field("std_deviation", stdDeviation()); + builder.field(Fields._TYPE, StatisticalFacetCollectorParser.NAME); + builder.field(Fields._FIELD, fieldName); + builder.field(Fields.COUNT, count()); + builder.field(Fields.TOTAL, total()); + builder.field(Fields.MIN, min()); + builder.field(Fields.MAX, max()); + builder.field(Fields.MEAN, mean()); + builder.field(Fields.SUM_OF_SQUARES, sumOfSquares()); + builder.field(Fields.VARIANCE, variance()); + builder.field(Fields.STD_DEVIATION, stdDeviation()); builder.endObject(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/InternalTermsFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/InternalTermsFacet.java index e60bf9078ce..1c2e41176c9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/InternalTermsFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/InternalTermsFacet.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.trove.TObjectIntHashMap; import org.elasticsearch.common.trove.TObjectIntIterator; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.internal.InternalFacet; @@ -138,15 +139,23 @@ public class InternalTermsFacet implements InternalFacet, TermsFacet { return new InternalTermsFacet(name, fieldName, comparatorType, requiredSize, ordered); } + static final class Fields { + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _FIELD = new XContentBuilderString("_field"); + static final XContentBuilderString TERMS = new XContentBuilderString("terms"); + static final XContentBuilderString TERM = new XContentBuilderString("term"); + static final XContentBuilderString COUNT = new XContentBuilderString("count"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name); - builder.field("_type", TermsFacetCollectorParser.NAME); - builder.field("_field", fieldName); - builder.startArray("terms"); + builder.field(Fields._TYPE, TermsFacetCollectorParser.NAME); + builder.field(Fields._FIELD, fieldName); + builder.startArray(Fields.TERMS); for (Entry entry : entries) { builder.startObject(); - builder.field("term", entry.term()); - builder.field("count", entry.count()); + builder.field(Fields.TERM, entry.term()); + builder.field(Fields.COUNT, entry.count()); builder.endObject(); } builder.endArray(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java index e6436d16f84..0177078f983 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.compress.lzf.LZFDecoder; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.action.support.RestXContentBuilder; @@ -266,23 +267,38 @@ public class InternalSearchHit implements SearchHit { return this.matchedFilters; } + public static class Fields { + static final XContentBuilderString _INDEX = new XContentBuilderString("_index"); + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _ID = new XContentBuilderString("_id"); + static final XContentBuilderString _SCORE = new XContentBuilderString("_score"); + static final XContentBuilderString FIELDS = new XContentBuilderString("fields"); + static final XContentBuilderString HIGHLIGHT = new XContentBuilderString("highlight"); + static final XContentBuilderString SORT = new XContentBuilderString("sort"); + static final XContentBuilderString MATCH_FILTERS = new XContentBuilderString("matched_filters"); + static final XContentBuilderString _EXPLANATION = new XContentBuilderString("_explanation"); + static final XContentBuilderString VALUE = new XContentBuilderString("value"); + static final XContentBuilderString DESCRIPTION = new XContentBuilderString("description"); + static final XContentBuilderString DETAILS = new XContentBuilderString("details"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - builder.field("_index", shard.index()); + builder.field(Fields._INDEX, shard.index()); // builder.field("_shard", shard.shardId()); // builder.field("_node", shard.nodeId()); - builder.field("_type", type()); - builder.field("_id", id()); + builder.field(Fields._TYPE, type()); + builder.field(Fields._ID, id()); if (Float.isNaN(score)) { - builder.nullField("_score"); + builder.nullField(Fields._SCORE); } else { - builder.field("_score", score); + builder.field(Fields._SCORE, score); } if (source != null) { RestXContentBuilder.restDocumentSource(source, builder, params); } if (fields != null && !fields.isEmpty()) { - builder.startObject("fields"); + builder.startObject(Fields.FIELDS); for (SearchHitField field : fields.values()) { if (field.values().isEmpty()) { continue; @@ -301,7 +317,7 @@ public class InternalSearchHit implements SearchHit { builder.endObject(); } if (highlightFields != null && !highlightFields.isEmpty()) { - builder.startObject("highlight"); + builder.startObject(Fields.HIGHLIGHT); for (HighlightField field : highlightFields.values()) { builder.field(field.name()); if (field.fragments() == null) { @@ -317,21 +333,21 @@ public class InternalSearchHit implements SearchHit { builder.endObject(); } if (sortValues != null && sortValues.length > 0) { - builder.startArray("sort"); + builder.startArray(Fields.SORT); for (Object sortValue : sortValues) { builder.value(sortValue); } builder.endArray(); } if (matchedFilters.length > 0) { - builder.startArray("matched_filters"); + builder.startArray(Fields.MATCH_FILTERS); for (String matchedFilter : matchedFilters) { builder.value(matchedFilter); } builder.endArray(); } if (explanation() != null) { - builder.field("_explanation"); + builder.field(Fields._EXPLANATION); buildExplanation(builder, explanation()); } builder.endObject(); @@ -339,11 +355,11 @@ public class InternalSearchHit implements SearchHit { private void buildExplanation(XContentBuilder builder, Explanation explanation) throws IOException { builder.startObject(); - builder.field("value", explanation.getValue()); - builder.field("description", explanation.getDescription()); + builder.field(Fields.VALUE, explanation.getValue()); + builder.field(Fields.DESCRIPTION, explanation.getDescription()); Explanation[] innerExps = explanation.getDetails(); if (innerExps != null) { - builder.startArray("details"); + builder.startArray(Fields.DETAILS); for (Explanation exp : innerExps) { buildExplanation(builder, exp); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHits.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHits.java index 7a8aecbafce..4c03413650f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHits.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHits.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.trove.TIntObjectHashMap; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchShardTarget; @@ -150,15 +151,21 @@ public class InternalSearchHits implements SearchHits { return this.hits; } + static final class Fields { + static final XContentBuilderString HITS = new XContentBuilderString("hits"); + static final XContentBuilderString TOTAL = new XContentBuilderString("total"); + static final XContentBuilderString MAX_SCORE = new XContentBuilderString("max_score"); + } + @Override public void toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject("hits"); - builder.field("total", totalHits); + builder.startObject(Fields.HITS); + builder.field(Fields.TOTAL, totalHits); if (Float.isNaN(maxScore)) { - builder.nullField("max_score"); + builder.nullField(Fields.MAX_SCORE); } else { - builder.field("max_score", maxScore); + builder.field(Fields.MAX_SCORE, maxScore); } - builder.field("hits"); + builder.field(Fields.HITS); builder.startArray(); for (SearchHit hit : hits) { hit.toXContent(builder, params);