Produce xcontent compatible with json maps

This commit is contained in:
Nicolas Lalevée 2011-05-30 18:58:50 +02:00 committed by kimchy
parent 2cd77088f1
commit 9f52256d8e
1 changed files with 25 additions and 6 deletions

View File

@ -113,18 +113,21 @@ public class BoolQueryBuilder extends BaseQueryBuilder {
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("bool"); builder.startObject("bool");
List<Clause> musts = new ArrayList<Clause>();
List<Clause> mustNots = new ArrayList<Clause>();
List<Clause> shoulds = new ArrayList<Clause>();
for (Clause clause : clauses) { for (Clause clause : clauses) {
if (clause.occur == BooleanClause.Occur.MUST) { if (clause.occur == BooleanClause.Occur.MUST) {
builder.field("must"); musts.add(clause);
clause.queryBuilder.toXContent(builder, params);
} else if (clause.occur == BooleanClause.Occur.MUST_NOT) { } else if (clause.occur == BooleanClause.Occur.MUST_NOT) {
builder.field("must_not"); mustNots.add(clause);
clause.queryBuilder.toXContent(builder, params);
} else if (clause.occur == BooleanClause.Occur.SHOULD) { } else if (clause.occur == BooleanClause.Occur.SHOULD) {
builder.field("should"); shoulds.add(clause);
clause.queryBuilder.toXContent(builder, params);
} }
} }
doXArrayContent("must", musts, builder, params);
doXArrayContent("must_not", mustNots, builder, params);
doXArrayContent("should", shoulds, builder, params);
if (boost != -1) { if (boost != -1) {
builder.field("boost", boost); builder.field("boost", boost);
} }
@ -137,6 +140,22 @@ public class BoolQueryBuilder extends BaseQueryBuilder {
builder.endObject(); builder.endObject();
} }
private void doXArrayContent(String field, List<Clause> clauses, XContentBuilder builder, Params params) throws IOException {
if (clauses.isEmpty()) {
return;
}
if (clauses.size() == 1) {
builder.field(field);
clauses.get(0).queryBuilder.toXContent(builder, params);
} else {
builder.startArray(field);
for (Clause clause : clauses) {
clause.queryBuilder.toXContent(builder, params);
}
builder.endArray();
}
}
private static class Clause { private static class Clause {
final XContentQueryBuilder queryBuilder; final XContentQueryBuilder queryBuilder;
final BooleanClause.Occur occur; final BooleanClause.Occur occur;