From 9f52256d8e0d0d21d687e824e226868a384d82e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Laleve=CC=81e?= Date: Mon, 30 May 2011 18:58:50 +0200 Subject: [PATCH] Produce xcontent compatible with json maps --- .../query/xcontent/BoolQueryBuilder.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/BoolQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/BoolQueryBuilder.java index 7b53a2ed4ac..7fb399bb315 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/BoolQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/BoolQueryBuilder.java @@ -113,18 +113,21 @@ public class BoolQueryBuilder extends BaseQueryBuilder { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("bool"); + List musts = new ArrayList(); + List mustNots = new ArrayList(); + List shoulds = new ArrayList(); for (Clause clause : clauses) { if (clause.occur == BooleanClause.Occur.MUST) { - builder.field("must"); - clause.queryBuilder.toXContent(builder, params); + musts.add(clause); } else if (clause.occur == BooleanClause.Occur.MUST_NOT) { - builder.field("must_not"); - clause.queryBuilder.toXContent(builder, params); + mustNots.add(clause); } else if (clause.occur == BooleanClause.Occur.SHOULD) { - builder.field("should"); - clause.queryBuilder.toXContent(builder, params); + shoulds.add(clause); } } + doXArrayContent("must", musts, builder, params); + doXArrayContent("must_not", mustNots, builder, params); + doXArrayContent("should", shoulds, builder, params); if (boost != -1) { builder.field("boost", boost); } @@ -137,6 +140,22 @@ public class BoolQueryBuilder extends BaseQueryBuilder { builder.endObject(); } + private void doXArrayContent(String field, List 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 { final XContentQueryBuilder queryBuilder; final BooleanClause.Occur occur;