From 75ee01a6b49395ea4a425d1a4812fe0bfac81299 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sat, 14 Aug 2010 00:37:33 +0300 Subject: [PATCH] clean builder code when dealing with maps --- .../xcontent/builder/XContentBuilder.java | 25 +++++++++++++++---- .../xcontent/CustomScoreQueryBuilder.java | 3 +-- .../query/xcontent/ScriptFilterBuilder.java | 3 +-- .../geodistance/GeoDistanceFacetBuilder.java | 3 +-- .../HistogramScriptFacetBuilder.java | 3 +-- .../facets/range/RangeScriptFacetBuilder.java | 3 +-- .../StatisticalScriptFacetBuilder.java | 3 +-- .../facets/terms/TermsFacetBuilder.java | 3 +-- .../search/sort/ScriptSortBuilder.java | 3 +-- 9 files changed, 28 insertions(+), 21 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/builder/XContentBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/builder/XContentBuilder.java index f1b4e16d44d..63e353aade6 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/builder/XContentBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/builder/XContentBuilder.java @@ -82,11 +82,6 @@ public abstract class XContentBuilder { return builder; } - public T map(Map map) throws IOException { - XContentMapConverter.writeMap(generator, map); - return builder; - } - public T startObject(String name) throws IOException { field(name); startObject(); @@ -208,6 +203,12 @@ public abstract class XContentBuilder { return builder; } + public T field(String name, Map value) throws IOException { + field(name); + value(value); + return builder; + } + public T field(String name, Object value) throws IOException { if (value == null) { nullField(name); @@ -232,6 +233,8 @@ public abstract class XContentBuilder { field(name, (byte[]) value); } else if (value instanceof ReadableInstant) { field(name, (ReadableInstant) value); + } else if (value instanceof Map) { + field(name, (Map) value); } else { field(name, value.toString()); } @@ -358,6 +361,16 @@ public abstract class XContentBuilder { return builder; } + public T map(Map map) throws IOException { + XContentMapConverter.writeMap(generator, map); + return builder; + } + + public T value(Map map) throws IOException { + XContentMapConverter.writeMap(generator, map); + return builder; + } + public T value(Object value) throws IOException { Class type = value.getClass(); if (type == String.class) { @@ -378,6 +391,8 @@ public abstract class XContentBuilder { value((Date) value); } else if (value instanceof ReadableInstant) { value((ReadableInstant) value); + } else if (value instanceof Map) { + value((Map) value); } else { throw new IOException("Type not allowed [" + type + "]"); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/CustomScoreQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/CustomScoreQueryBuilder.java index b05366d31c1..9b99a235702 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/CustomScoreQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/CustomScoreQueryBuilder.java @@ -95,8 +95,7 @@ public class CustomScoreQueryBuilder extends BaseQueryBuilder { queryBuilder.toXContent(builder, params); builder.field("script", script); if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } if (boost != -1) { builder.field("boost", boost); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ScriptFilterBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ScriptFilterBuilder.java index 398fdb39e2c..b228248c844 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ScriptFilterBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ScriptFilterBuilder.java @@ -60,8 +60,7 @@ public class ScriptFilterBuilder extends BaseFilterBuilder { builder.startObject(ScriptFilterParser.NAME); builder.field("script", script); if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } builder.endObject(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetBuilder.java index 8c97564968a..5a4a1d6802c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetBuilder.java @@ -221,8 +221,7 @@ public class GeoDistanceFacetBuilder extends AbstractFacetBuilder { if (valueScript != null) { builder.field("value_script", valueScript); if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramScriptFacetBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramScriptFacetBuilder.java index a5253c74454..f8484bb3c12 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramScriptFacetBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramScriptFacetBuilder.java @@ -96,8 +96,7 @@ public class HistogramScriptFacetBuilder extends AbstractFacetBuilder { builder.field("interval", interval); } if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } if (comparatorType != null) { builder.field("comparator", comparatorType.description()); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeScriptFacetBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeScriptFacetBuilder.java index 0753580832b..628a3000953 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeScriptFacetBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeScriptFacetBuilder.java @@ -136,8 +136,7 @@ public class RangeScriptFacetBuilder extends AbstractFacetBuilder { builder.endArray(); if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } builder.endObject(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalScriptFacetBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalScriptFacetBuilder.java index dd7419845ed..df8f2cc6b49 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalScriptFacetBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalScriptFacetBuilder.java @@ -71,8 +71,7 @@ public class StatisticalScriptFacetBuilder extends AbstractFacetBuilder { builder.startObject(StatisticalFacetCollectorParser.NAME); builder.field("script", script); if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } builder.endObject(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetBuilder.java index c23105f3815..4c1ea888fdb 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetBuilder.java @@ -128,8 +128,7 @@ public class TermsFacetBuilder extends AbstractFacetBuilder { if (script != null) { builder.field("script", script); if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java index 464343e4925..af4de9d5a97 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java @@ -64,8 +64,7 @@ public class ScriptSortBuilder extends SortBuilder { builder.field("reverse", true); } if (this.params != null) { - builder.field("params"); - builder.map(this.params); + builder.field("params", this.params); } builder.endObject(); }