# How to add another condition to SearchRequest.Builder in Java API

**URL:** <https://discuss.elastic.co/t/how-to-add-another-condition-to-searchrequest-builder-in-java-api/321812>\
**Category:** Elasticsearch\
**Created:** [December 22, 2022, 7:17am UTC](https://discuss.elastic.co/t/how-to-add-another-condition-to-searchrequest-builder-in-java-api/321812 "2022-12-22T07:17:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cchuang4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cchuang4/32/114678_2.png) [@cchuang4](https://discuss.elastic.co/u/cchuang4)\
**Post date:** [December 22, 2022, 7:17am UTC](https://discuss.elastic.co/t/how-to-add-another-condition-to-searchrequest-builder-in-java-api/321812/1 "2022-12-22T07:17:48Z")

</div>

For new Java API, If I want to make a search like below:

 ![螢幕擷取畫面 2022-12-22 151915](https://us1.discourse-cdn.com/elastic/original/3X/c/3/c373804f11943c81d11f1fe2f62a1a6c6bdc2ecc.png)

my code below, but I will set range and terms in different place, how should I combination the second setting to exist condition.  
In my code, the second .query() will replace the first one.

```auto
SearchRequest.Builder builder = new SearchRequest.Builder();
builder.index("index1").query(qu->qu.bool(f->f.filter(r->r.range(rq->rq.field("PostDate").from("2022-02-01T13:30:01.000").to("2022-02-07T13:30:01.999")))));
......
List<FieldValue> data = new ArrayList<FieldValue>();
data.add(FieldValue.of( "news"));
builder.query(qu->qu.bool(f->f.filter(ft->ft.terms(ts->ts.field("Type").terms(TermsQueryField.of(t->t.value(data)))))));

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [December 22, 2022, 12:54pm UTC](https://discuss.elastic.co/t/how-to-add-another-condition-to-searchrequest-builder-in-java-api/321812/2 "2022-12-22T12:54:05Z")

</div>

Hi @cchuang4

I don't know if it's the best option, but what I would do is have a single point where I build the SearchRequest (it can be a method) and receive a BoolQuery object as a parameter.

Who will build this BoolQuery would be responsible for receiving the other clauses, in your case the rangeQuery and TermsQuery.

```auto
    var rangeQuery = RangeQuery.of(rq -> rq.field("PostDate").from("2022-02-01T13:30:01.000").to("2022-02-07T13:30:01.999"))._toQuery();

    List<FieldValue> data = new ArrayList<FieldValue>();
    data.add(FieldValue.of("news"));
    var termsQuery = TermsQuery.of(ts -> ts.field("Type").terms(TermsQueryField.of(t -> t.value(data))))._toQuery();

    var boolQuery = BoolQuery.of(bq -> bq.filter(rangeQuery, termsQuery));

    SearchRequest builder = SearchRequest.of(sr ->
        sr.index("index1").query(q -> q.bool(boolQuery))
    );

```

---

<div class="post-metadata">

**Author:** ![cchuang4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cchuang4/32/114678_2.png) [@cchuang4](https://discuss.elastic.co/u/cchuang4)\
**Post date:** [December 23, 2022, 2:36am UTC](https://discuss.elastic.co/t/how-to-add-another-condition-to-searchrequest-builder-in-java-api/321812/3 "2022-12-23T02:36:54Z")

</div>

Thank you, I got the point of how to combine different condition.

---

<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:** [January 20, 2023, 2:37am UTC](https://discuss.elastic.co/t/how-to-add-another-condition-to-searchrequest-builder-in-java-api/321812/4 "2023-01-20T02:37:53Z")

</div>

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