# Buidling Elastic Search queries dynamically using the Java API

**URL:** <https://discuss.elastic.co/t/buidling-elastic-search-queries-dynamically-using-the-java-api/175111>\
**Category:** Elasticsearch\
**Created:** [April 3, 2019, 7:06am UTC](https://discuss.elastic.co/t/buidling-elastic-search-queries-dynamically-using-the-java-api/175111 "2019-04-03T07:06:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![titulum](https://avatars.discourse-cdn.com/v4/letter/t/a88e4f/32.png) [@titulum](https://discuss.elastic.co/u/titulum)\
**Post date:** [April 3, 2019, 7:06am UTC](https://discuss.elastic.co/t/buidling-elastic-search-queries-dynamically-using-the-java-api/175111/1 "2019-04-03T07:06:29Z")

</div>

Imagine the following documents:

```
[
  { "name" : "deepgreen", "colors" : [
    { "color" : "green" },
    { "color" : "black" }
    ]
  },
  { "name" : "lightblue", "colors" : [
    { "color" : "blue" },
    { "color" : "white" }
    ]
  },
  { "name" : "purple", "colors": [
    { "color" : "red" },
    { "color" : "blue" },
    { "color" : "white" }
    ]
  }
]

```

Where the `colors` field is of type `nested object` , this mapping is correct. The query I would execute to get, for instance, purple, is:

```
GET /colors/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "colors",
            "query": {
              "match_phrase": {
                "colors.color": "blue"
              }
            }
          }
        },
        {
          "nested": {
            "path": "colors",
            "query": {
              "match_phrase": {
                "colors.color": "blue"
              }
            }
          }
        },
        {
          "nested": {
            "path": "colors",
            "query": {
              "match_phrase": {
                "colors.color": "blue"
              }
            }
          }
        },
        {
          "match_phrase": {
            "name": "purple"
          }
        }
      ]
    }
  }
}

```

How could I dynamically generate this query using the ES Java API? What I'm trying to do can be understood by looking at this pseudo-code:

```
List<QueryBuilders> nestedQueryBuilders = new ArrayList<>();

colors.forEach(color -> {
    nestedQueryBuilders.add(new nestedQuery("colors",
            matchPhraseQuery("colors.color", color)),
            ScoreMode.Avg);
});

BoolQueryBuilder boolQuery = boolQuery()
    .must(matchPhraseQuery("name", "purple"))
    .must(nestedQueryBuilders);

SearchRequest request = new SearchRequest()
    .indices("colors")
    .source(new SearchSourceBuilder()
        .query(boolQuery));

client.search(request, RequestOptions.DEFAULT);

```

I have not encountered a method yet for adding "sub"-queries to a must-bool-query dynamically.

Anyone have some experience on this and could tell me how to accomplish this?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 1, 2019, 7:06am UTC](https://discuss.elastic.co/t/buidling-elastic-search-queries-dynamically-using-the-java-api/175111/2 "2019-05-01T07:06:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
