add the ability to provide params to ToJson (later will be used to control how json is generated)

This commit is contained in:
kimchy 2010-02-17 18:36:52 +02:00
parent 0545423837
commit ffbc2a9d8d
34 changed files with 88 additions and 59 deletions

View File

@ -36,6 +36,7 @@
<w>nanos</w>
<w>ngram</w>
<w>param</w>
<w>params</w>
<w>porterstem</w>
<w>rebalance</w>
<w>searchable</w>

View File

@ -85,7 +85,7 @@ public class SearchResponse implements ActionResponse, ToJson {
return response;
}
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
if (scrollId != null) {
builder.field("_scrollId", scrollId);
}
@ -94,7 +94,7 @@ public class SearchResponse implements ActionResponse, ToJson {
builder.field("successful", successfulShards());
builder.field("failed", failedShards());
builder.endObject();
internalResponse.toJson(builder);
internalResponse.toJson(builder, params);
}
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {

View File

@ -28,11 +28,11 @@ import java.io.IOException;
*/
public abstract class BaseJsonFilterBuilder implements JsonFilterBuilder {
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject();
doJson(builder);
doJson(builder, params);
builder.endObject();
}
protected abstract void doJson(JsonBuilder builder) throws IOException;
protected abstract void doJson(JsonBuilder builder, Params params) throws IOException;
}

View File

@ -32,18 +32,18 @@ public abstract class BaseJsonQueryBuilder implements JsonQueryBuilder {
@Override public String build() throws QueryBuilderException {
try {
JsonBuilder builder = JsonBuilder.jsonBuilder();
toJson(builder);
toJson(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
throw new QueryBuilderException("Failed to build query", e);
}
}
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject();
doJson(builder);
doJson(builder, params);
builder.endObject();
}
protected abstract void doJson(JsonBuilder builder) throws IOException;
protected abstract void doJson(JsonBuilder builder, Params params) throws IOException;
}

View File

@ -68,18 +68,18 @@ public class BoolJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject("bool");
for (Clause clause : clauses) {
if (clause.occur == BooleanClause.Occur.MUST) {
builder.field("must");
clause.queryBuilder.toJson(builder);
clause.queryBuilder.toJson(builder, params);
} else if (clause.occur == BooleanClause.Occur.MUST_NOT) {
builder.field("mustNot");
clause.queryBuilder.toJson(builder);
clause.queryBuilder.toJson(builder, params);
} else if (clause.occur == BooleanClause.Occur.SHOULD) {
builder.field("should");
clause.queryBuilder.toJson(builder);
clause.queryBuilder.toJson(builder, params);
}
}
if (boost != -1) {

View File

@ -41,10 +41,10 @@ public class ConstantScoreQueryJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(ConstantScoreQueryJsonQueryParser.NAME);
builder.field("filter");
filterBuilder.toJson(builder);
filterBuilder.toJson(builder, params);
if (boost != -1) {
builder.field("boost", boost);
}

View File

@ -52,7 +52,7 @@ public class DisMaxJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject("disMax");
if (tieBreakerMultiplier != -1) {
builder.field("tieBreakerMultiplier", tieBreakerMultiplier);
@ -62,7 +62,7 @@ public class DisMaxJsonQueryBuilder extends BaseJsonQueryBuilder {
}
builder.startArray("queries");
for (JsonQueryBuilder queryBuilder : queries) {
queryBuilder.toJson(builder);
queryBuilder.toJson(builder, params);
}
builder.endArray();
builder.endObject();

View File

@ -44,12 +44,12 @@ public class FilteredQueryJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(FilteredQueryJsonQueryParser.NAME);
builder.field("query");
queryBuilder.toJson(builder);
queryBuilder.toJson(builder, params);
builder.field("filter");
filterBuilder.toJson(builder);
filterBuilder.toJson(builder, params);
if (boost != -1) {
builder.field("boost", boost);
}

View File

@ -42,7 +42,7 @@ public class MatchAllJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(MatchAllJsonQueryParser.NAME);
if (boost != -1) {
builder.field("boost", boost);

View File

@ -37,7 +37,7 @@ public class PrefixJsonFilterBuilder extends BaseJsonFilterBuilder {
this.value = value;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(PrefixJsonFilterParser.NAME);
builder.field(name, value);
builder.endObject();

View File

@ -44,7 +44,7 @@ public class PrefixJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(PrefixJsonQueryParser.NAME);
if (boost == -1) {
builder.field(name, value);

View File

@ -34,8 +34,8 @@ public class QueryJsonFilterBuilder extends BaseJsonFilterBuilder {
this.queryBuilder = queryBuilder;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.field(QueryJsonFilterParser.NAME);
queryBuilder.toJson(builder);
queryBuilder.toJson(builder, params);
}
}

View File

@ -109,7 +109,7 @@ public class QueryStringJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(QueryStringJsonQueryParser.NAME);
builder.field("query", queryString);
if (defaultField != null) {

View File

@ -102,7 +102,7 @@ public class RangeJsonFilterBuilder extends BaseJsonFilterBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(RangeJsonFilterParser.NAME);
builder.startObject(name);
builder.field("from", from);

View File

@ -109,7 +109,7 @@ public class RangeJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(RangeJsonQueryParser.NAME);
builder.startObject(name);
builder.field("from", from);

View File

@ -44,10 +44,10 @@ public class SpanFirstJsonQueryBuilder extends BaseJsonQueryBuilder implements J
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(SpanFirstJsonQueryParser.NAME);
builder.field("match");
matchBuilder.toJson(builder);
matchBuilder.toJson(builder, params);
builder.field("end", end);
if (boost != -1) {
builder.field("boost", boost);

View File

@ -65,7 +65,7 @@ public class SpanNearJsonQueryBuilder extends BaseJsonQueryBuilder implements Js
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) {
throw new QueryBuilderException("Must have at least one clause when building a spanNear query");
}
@ -75,7 +75,7 @@ public class SpanNearJsonQueryBuilder extends BaseJsonQueryBuilder implements Js
builder.startObject(SpanNearJsonQueryParser.NAME);
builder.startArray("clauses");
for (JsonSpanQueryBuilder clause : clauses) {
clause.toJson(builder);
clause.toJson(builder, params);
}
builder.endArray();
builder.field("slop", slop);

View File

@ -50,7 +50,7 @@ public class SpanNotJsonQueryBuilder extends BaseJsonQueryBuilder implements Jso
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
if (include == null) {
throw new QueryBuilderException("Must specify include when using spanNot query");
}
@ -59,9 +59,9 @@ public class SpanNotJsonQueryBuilder extends BaseJsonQueryBuilder implements Jso
}
builder.startObject(SpanNotJsonQueryParser.NAME);
builder.field("include");
include.toJson(builder);
include.toJson(builder, params);
builder.field("exclude");
exclude.toJson(builder);
exclude.toJson(builder, params);
if (boost == -1) {
builder.field("boost", boost);
}

View File

@ -44,14 +44,14 @@ public class SpanOrJsonQueryBuilder extends BaseJsonQueryBuilder implements Json
return this;
}
@Override protected void doJson(JsonBuilder builder) throws IOException {
@Override protected void doJson(JsonBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) {
throw new QueryBuilderException("Must have at least one clause when building a spanOr query");
}
builder.startObject(SpanOrJsonQueryParser.NAME);
builder.startArray("clauses");
for (JsonSpanQueryBuilder clause : clauses) {
clause.toJson(builder);
clause.toJson(builder, params);
}
builder.endArray();
if (boost != -1) {

View File

@ -64,7 +64,7 @@ public class SpanTermJsonQueryBuilder extends BaseJsonQueryBuilder implements Js
return this;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(SpanTermJsonQueryParser.NAME);
if (boost == -1) {
builder.field(name, value);

View File

@ -57,7 +57,7 @@ public class TermJsonFilterBuilder extends BaseJsonFilterBuilder {
this.value = value;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(TermJsonFilterParser.NAME);
builder.field(name, value);
builder.endObject();

View File

@ -64,7 +64,7 @@ public class TermJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(TermJsonQueryParser.NAME);
if (boost == -1) {
builder.field(name, value);

View File

@ -73,7 +73,7 @@ public class TermsJsonFilterBuilder extends BaseJsonFilterBuilder {
this.values = values;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(TermsJsonFilterParser.NAME);
builder.startArray(name);
for (Object value : values) {

View File

@ -44,7 +44,7 @@ public class WildcardJsonQueryBuilder extends BaseJsonQueryBuilder {
return this;
}
@Override public void doJson(JsonBuilder builder) throws IOException {
@Override public void doJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject(WildcardJsonQueryParser.NAME);
if (boost == -1) {
builder.field(name, value);

View File

@ -19,6 +19,8 @@
package org.elasticsearch.rest;
import org.elasticsearch.util.json.ToJson;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -26,7 +28,7 @@ import java.util.Set;
/**
* @author kimchy (Shay Banon)
*/
public interface RestRequest {
public interface RestRequest extends ToJson.Params {
enum Method {
GET, POST, PUT, DELETE

View File

@ -90,7 +90,7 @@ public class RestSearchAction extends BaseRestHandler {
try {
JsonBuilder builder = RestJsonBuilder.cached(request);
builder.startObject();
result.toJson(builder);
result.toJson(builder, request);
builder.endObject();
channel.sendResponse(new JsonRestResponse(request, OK, builder));
} catch (Exception e) {

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.builder;
import org.elasticsearch.index.query.json.JsonQueryBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import java.util.ArrayList;
import java.util.List;
@ -138,7 +139,7 @@ public class SearchSourceBuilder {
}
builder.field("query");
queryBuilder.toJson(builder);
queryBuilder.toJson(builder, ToJson.EMPTY_PARAMS);
if (explain != null) {
builder.field("explain", explain);
@ -174,7 +175,7 @@ public class SearchSourceBuilder {
}
if (facetsBuilder != null) {
facetsBuilder.json(builder);
facetsBuilder.toJson(builder, ToJson.EMPTY_PARAMS);
}
builder.endObject();

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.builder;
import org.elasticsearch.index.query.json.JsonQueryBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import java.io.IOException;
import java.util.List;
@ -30,7 +31,7 @@ import static com.google.common.collect.Lists.*;
/**
* @author kimchy (Shay Banon)
*/
public class SearchSourceFacetsBuilder {
public class SearchSourceFacetsBuilder implements ToJson {
private String queryExecution;
@ -49,7 +50,7 @@ public class SearchSourceFacetsBuilder {
return this;
}
void json(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
if (queryExecution == null && queryFacets == null) {
return;
}
@ -64,7 +65,7 @@ public class SearchSourceFacetsBuilder {
for (FacetQuery facetQuery : queryFacets) {
builder.startObject(facetQuery.name());
builder.field("query");
facetQuery.queryBuilder().toJson(builder);
facetQuery.queryBuilder().toJson(builder, params);
builder.endObject();
}
}

View File

@ -59,7 +59,7 @@ public class CountFacet implements Facet {
count += increment;
}
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
builder.field(name, count);
}

View File

@ -69,10 +69,10 @@ public class Facets implements Streamable, ToJson {
return null;
}
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject("facets");
for (Facet facet : facets) {
facet.toJson(builder);
facet.toJson(builder, params);
}
builder.endObject();
}

View File

@ -108,7 +108,7 @@ public class InternalSearchHit implements SearchHit {
return null;
}
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("_index", shard.index());
// builder.field("_shard", shard.shardId());

View File

@ -63,13 +63,13 @@ public class InternalSearchHits implements SearchHits {
return hits;
}
@Override public void toJson(JsonBuilder builder) throws IOException {
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
builder.startObject("hits");
builder.field("total", totalHits);
builder.field("hits");
builder.startArray();
for (SearchHit hit : hits) {
hit.toJson(builder);
hit.toJson(builder, params);
}
builder.endArray();
builder.endObject();

View File

@ -63,10 +63,10 @@ public class InternalSearchResponse implements Streamable, ToJson {
return response;
}
@Override public void toJson(JsonBuilder builder) throws IOException {
hits.toJson(builder);
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
hits.toJson(builder, params);
if (facets != null) {
facets.toJson(builder);
facets.toJson(builder, params);
}
}

View File

@ -20,11 +20,35 @@
package org.elasticsearch.util.json;
import java.io.IOException;
import java.util.Map;
/**
* @author kimchy (Shay Banon)
*/
public interface ToJson {
void toJson(JsonBuilder builder) throws IOException;
public static interface Params {
String param(String key);
}
public static final Params EMPTY_PARAMS = new Params() {
@Override public String param(String key) {
return null;
}
};
public static class MapParams implements Params {
private final Map<String, String> params;
public MapParams(Map<String, String> params) {
this.params = params;
}
@Override public String param(String key) {
return params.get(key);
}
}
void toJson(JsonBuilder builder, Params params) throws IOException;
}