add Map as a parameter where Json can be passed

This commit is contained in:
kimchy 2010-04-17 20:10:15 +03:00
parent 9c1f2fdec5
commit f46c643c63
7 changed files with 119 additions and 2 deletions

View File

@ -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<String, String> mappings() {
return this.mappings;
}

View File

@ -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.
*/

View File

@ -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.
*/

View File

@ -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.

View File

@ -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.
*/

View File

@ -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))

View File

@ -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 ->