From f46c643c639957eb561e72e0078b8a3f382ecf20 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sat, 17 Apr 2010 20:10:15 +0300 Subject: [PATCH] add Map as a parameter where Json can be passed --- .../indices/create/CreateIndexRequest.java | 32 +++++++++++++++++++ .../mapping/put/PutMappingRequest.java | 17 ++++++++++ .../deletebyquery/DeleteByQueryRequest.java | 18 +++++++++++ .../action/mlt/MoreLikeThisRequest.java | 15 +++++++++ .../action/search/SearchRequest.java | 29 +++++++++++++++++ .../groovy/client/GClient.groovy | 6 ++++ .../groovy/client/GIndicesAdminClient.groovy | 4 +-- 7 files changed, 119 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index d42fadb832b..d0f08d52132 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -19,10 +19,12 @@ package org.elasticsearch.action.admin.indices.create; +import org.elasticsearch.ElasticSearchGenerationException; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.MasterNodeOperationRequest; import org.elasticsearch.util.TimeValue; +import org.elasticsearch.util.io.FastCharArrayWriter; import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.json.JsonBuilder; @@ -36,6 +38,7 @@ import java.util.concurrent.TimeUnit; import static com.google.common.collect.Maps.*; import static org.elasticsearch.action.Actions.*; import static org.elasticsearch.util.TimeValue.*; +import static org.elasticsearch.util.json.Jackson.*; import static org.elasticsearch.util.settings.ImmutableSettings.Builder.*; import static org.elasticsearch.util.settings.ImmutableSettings.*; @@ -132,6 +135,19 @@ public class CreateIndexRequest extends MasterNodeOperationRequest { return this; } + /** + * The settings to crete the index with (either json/yaml/properties format) + */ + public CreateIndexRequest settings(Map source) { + FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached(); + try { + defaultObjectMapper().writeValue(writer, source); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e); + } + return settings(writer.toString()); + } + /** * Adds mapping that will be added when the index gets created. * @@ -166,6 +182,22 @@ public class CreateIndexRequest extends MasterNodeOperationRequest { return this; } + /** + * Adds mapping that will be added when the index gets created. + * + * @param type The mapping type + * @param source The mapping source + */ + public CreateIndexRequest mapping(String type, Map source) { + FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached(); + try { + defaultObjectMapper().writeValue(writer, source); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e); + } + return mapping(type, writer.toString()); + } + Map mappings() { return this.mappings; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java index e1f1d84908e..18c8cc98684 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java @@ -19,20 +19,24 @@ package org.elasticsearch.action.admin.indices.mapping.put; +import org.elasticsearch.ElasticSearchGenerationException; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.MasterNodeOperationRequest; import org.elasticsearch.util.Required; import org.elasticsearch.util.TimeValue; +import org.elasticsearch.util.io.FastCharArrayWriter; import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.json.JsonBuilder; import java.io.IOException; +import java.util.Map; import java.util.concurrent.TimeUnit; import static org.elasticsearch.action.Actions.*; import static org.elasticsearch.util.TimeValue.*; +import static org.elasticsearch.util.json.Jackson.*; /** * Puts mapping definition registered under a specific type into one or more indices. Best created with @@ -127,6 +131,19 @@ public class PutMappingRequest extends MasterNodeOperationRequest { } } + /** + * The mapping source definition. + */ + @Required public PutMappingRequest source(Map mappingSource) { + FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached(); + try { + defaultObjectMapper().writeValue(writer, mappingSource); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + mappingSource + "]", e); + } + return source(writer.toString()); + } + /** * The mapping source definition. */ diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java index 9ab64106f91..f006064dd53 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/deletebyquery/DeleteByQueryRequest.java @@ -19,6 +19,7 @@ package org.elasticsearch.action.deletebyquery; +import org.elasticsearch.ElasticSearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.replication.IndicesReplicationOperationRequest; import org.elasticsearch.index.query.QueryBuilder; @@ -26,13 +27,16 @@ import org.elasticsearch.util.Required; import org.elasticsearch.util.Strings; import org.elasticsearch.util.TimeValue; import org.elasticsearch.util.Unicode; +import org.elasticsearch.util.io.FastByteArrayOutputStream; import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamOutput; import java.io.IOException; import java.util.Arrays; +import java.util.Map; import static org.elasticsearch.action.Actions.*; +import static org.elasticsearch.util.json.Jackson.*; /** * A request to delete all documents that matching a specific query. Best created with @@ -108,6 +112,20 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest { return query(Unicode.fromStringAsBytes(querySource)); } + /** + * The query source to execute in the form of a map. + */ + @Required public DeleteByQueryRequest query(Map querySource) { + FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached(); + try { + defaultObjectMapper().writeValue(os, querySource); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + querySource + "]", e); + } + this.querySource = os.copiedByteArray(); + return this; + } + /** * The query source to execute. */ diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java index 27e6be3d1c3..20beddd88f7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/mlt/MoreLikeThisRequest.java @@ -19,6 +19,7 @@ package org.elasticsearch.action.mlt; +import org.elasticsearch.ElasticSearchGenerationException; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; @@ -30,12 +31,15 @@ import org.elasticsearch.util.Bytes; import org.elasticsearch.util.Required; import org.elasticsearch.util.Strings; import org.elasticsearch.util.Unicode; +import org.elasticsearch.util.io.FastByteArrayOutputStream; import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamOutput; import java.io.IOException; +import java.util.Map; import static org.elasticsearch.search.Scroll.*; +import static org.elasticsearch.util.json.Jackson.*; /** * A more like this request allowing to search for documents that a "like" the provided document. The document @@ -312,6 +316,17 @@ public class MoreLikeThisRequest implements ActionRequest { return searchSource(Unicode.fromStringAsBytes(searchSource)); } + public MoreLikeThisRequest searchSource(Map searchSource) { + FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached(); + try { + defaultObjectMapper().writeValue(os, searchSource); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + searchSource + "]", e); + } + this.searchSource = os.copiedByteArray(); + return this; + } + /** * An optional search source request allowing to control the search request for the * more like this documents. diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java index 8270d8fbd8a..f43a2af9486 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -19,6 +19,7 @@ package org.elasticsearch.action.search; +import org.elasticsearch.ElasticSearchGenerationException; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; @@ -28,14 +29,17 @@ import org.elasticsearch.util.Bytes; import org.elasticsearch.util.Strings; import org.elasticsearch.util.TimeValue; import org.elasticsearch.util.Unicode; +import org.elasticsearch.util.io.FastByteArrayOutputStream; import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamOutput; import java.io.IOException; +import java.util.Map; import static org.elasticsearch.action.Actions.*; import static org.elasticsearch.search.Scroll.*; import static org.elasticsearch.util.TimeValue.*; +import static org.elasticsearch.util.json.Jackson.*; /** * A request to execute search against one or more indices (or all). Best created using @@ -194,6 +198,20 @@ public class SearchRequest implements ActionRequest { return source(Unicode.fromStringAsBytes(source)); } + /** + * The source of the search request in the form of a map. + */ + public SearchRequest source(Map source) { + FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached(); + try { + defaultObjectMapper().writeValue(os, source); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e); + } + this.source = os.copiedByteArray(); + return this; + } + /** * The search source to execute. */ @@ -216,6 +234,17 @@ public class SearchRequest implements ActionRequest { return extraSource(sourceBuilder.build()); } + public SearchRequest extraSource(Map extraSource) { + FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached(); + try { + defaultObjectMapper().writeValue(os, extraSource); + } catch (IOException e) { + throw new ElasticSearchGenerationException("Failed to generate [" + extraSource + "]", e); + } + this.extraSource = os.copiedByteArray(); + return this; + } + /** * Allows to provide additional source that will use used as well. */ diff --git a/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GClient.groovy b/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GClient.groovy index 54e1d6ff210..961c8ad240e 100644 --- a/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GClient.groovy +++ b/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GClient.groovy @@ -73,6 +73,12 @@ class GClient { SearchRequest.metaClass.source = {Closure c -> delegate.source(new JsonBuilder().buildAsBytes(c)) } + SearchRequest.metaClass.setExtraSource = {Closure c -> + delegate.extraSource(new JsonBuilder().buildAsBytes(c)) + } + SearchRequest.metaClass.extraSource = {Closure c -> + delegate.extraSource(new JsonBuilder().buildAsBytes(c)) + } MoreLikeThisRequest.metaClass.setSearchSource = {Closure c -> delegate.searchSource(new JsonBuilder().buildAsBytes(c)) diff --git a/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GIndicesAdminClient.groovy b/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GIndicesAdminClient.groovy index 61c207005d0..8f258751b13 100644 --- a/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GIndicesAdminClient.groovy +++ b/plugins/groovy/src/main/groovy/org/elasticsearch/groovy/client/GIndicesAdminClient.groovy @@ -51,10 +51,10 @@ import org.elasticsearch.groovy.util.json.JsonBuilder class GIndicesAdminClient { static { - CreateIndexRequest.metaClass.setSource = {Closure c -> + CreateIndexRequest.metaClass.setSettings = {Closure c -> delegate.settings(new JsonBuilder().buildAsString(c)) } - CreateIndexRequest.metaClass.source = {Closure c -> + CreateIndexRequest.metaClass.settings = {Closure c -> delegate.settings(new JsonBuilder().buildAsString(c)) } CreateIndexRequest.metaClass.mapping = {String type, Closure c ->