# How to apply logical operators to search query in golang?

**URL:** <https://discuss.elastic.co/t/how-to-apply-logical-operators-to-search-query-in-golang/386536>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 28, 2026, 6:00am UTC](https://discuss.elastic.co/t/how-to-apply-logical-operators-to-search-query-in-golang/386536 "2026-05-28T06:00:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scip](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scip/32/147443_2.png) [@scip](https://discuss.elastic.co/u/scip)\
**Post date:** [May 28, 2026, 6:00am UTC](https://discuss.elastic.co/t/how-to-apply-logical-operators-to-search-query-in-golang/386536/1 "2026-05-28T06:00:07Z")

</div>

Hi,

how can I specify if I want a query to use the `AND` or `OR` operator? For a SimpleQuery this is easy, just call `DefaultOperator(operator.Operator{"AND"})`. But how Do I specify this with a BoolQuery?

What I found is, that I can call an `Operator()` function on `esdsl.NewMultiMatchQuery()`, which I already use to search multiple fields for one pattern. But what if I want to search for (e.g.): `message=error`, `tenant=foo`? Currently BoolQuery seems to use the OR operator since it returns documents matching `message=error` of any tenant AND documents matching `tenant=foo` for ANY message, but I need AND in this case - or better I need it to be customizable.

Here's the code I have for this so far (just the relevant part):

```go
query := esdsl.NewBoolQuery()

for _, q := range queries {
	filter, err := NewFilter(q)
	if err != nil {
		return nil, err
	}

	// by default we match on a single field
	var match types.QueryVariant = esdsl.NewMatchQuery(filter.term, filter.filter)

	if filter.multi {
		// ok, match across multiple given fields
		match = esdsl.NewMultiMatchQuery(filter.filter).
                Fields(filter.mterm...).Operator(op) // ok
	}

	// apply logic
	switch filter.criteria {
	case Fmustnot:
		query.MustNot(match)
	case Fmust:
		query.Must(match)
	case Fshould:
		query.Should(match)

	// query.DefaultOperator() or something alike?
}

```

It is possible to add multiple MatchQueries to a BoolQuery, but I was unable to figure out how to specify an operator.

Thanks in advance,  
Tom

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [May 28, 2026, 12:43pm UTC](https://discuss.elastic.co/t/how-to-apply-logical-operators-to-search-query-in-golang/386536/2 "2026-05-28T12:43:57Z")

</div>

Hi @scip,

Welcome! You'll see from the [`bool` query documentation](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-bool-query) that each query under the `must` clause would equate to an AND clause, compared with `should` which equates to OR:

> |`must`|The clause (query) must appear in matching documents and will contribute to the score. Each query defined under a `must` acts as a logical "AND", returning only documents that match _all_ the specified queries.|  
> | --- | --- |  
> |`should`|The clause (query) should appear in the matching document. Each query defined under a `should` acts as a logical "OR", returning documents that match _any_ of the specified queries.|

So in your case the AND criteria should come under `query.Must` for example.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![scip](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scip/32/147443_2.png) [@scip](https://discuss.elastic.co/u/scip)\
**Post date:** [May 28, 2026, 12:57pm UTC](https://discuss.elastic.co/t/how-to-apply-logical-operators-to-search-query-in-golang/386536/3 "2026-05-28T12:57:13Z")

</div>

Hi Carly,

yes, that's it. I already found it myself but forgot to update here. Thanks a lot anyway!

bestm

Tom
