# Elasticsearch .Net client ignoring Term in Query?

**URL:** <https://discuss.elastic.co/t/elasticsearch-net-client-ignoring-term-in-query/376749>\
**Category:** Elastic Search\
**Created:** [April 3, 2025, 2:13pm UTC](https://discuss.elastic.co/t/elasticsearch-net-client-ignoring-term-in-query/376749 "2025-04-03T14:13:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kk123](https://avatars.discourse-cdn.com/v4/letter/k/e56c9b/32.png) [@kk123](https://discuss.elastic.co/u/kk123)\
**Post date:** [April 3, 2025, 2:13pm UTC](https://discuss.elastic.co/t/elasticsearch-net-client-ignoring-term-in-query/376749/1 "2025-04-03T14:13:59Z")

</div>

Hi, I am trying to update\_by\_query using the following

```auto
var updateResult = await ElasticSearch.ClientPool.Client.UpdateByQueryAsync<ElasticSearch.ESMPacketDocument>("mpackets", u => u
    .Query(q => q
        .Bool(b => b                                
            .Must(f => f
                .Term(t => t.Field("fields.processed").Value(0))
                .Prefix(p => p.Field("mqtt.topic").Value(TopicFilter))
            )
        )
    )
    .MaxDocs(MessagesPerQuery)
    .Script(s => s
        .Source($"ctx._source.fields.processed = {CycleKey}")
    )
    .Refresh(true), stoppingToken);

```

However the generated query (turning debug info on) is

```auto
# Audit trail of this API call:
 - [2] MaxRetriesReached: Took: 00:00:00.0000006
# OriginalException: Elastic.Transport.TransportException: Maximum number of retries reached. Call: Status code 409 from: POST /mpackets/_update_by_query?pretty=true&error_trace=true&refresh=true
# Request:
{
  "max_docs": 1000,
  "query": {
    "bool": {
      "must": {
        "prefix": {
          "mqtt.topic": {
            "value": "topicname1"
          }
        }
      }
    }
  },
  "script": {
    "source": "ctx._source.fields.processed = 1287344637"
  }
}

```

As you can see the term part of the query is ignored... Any thoughts?

However it seems fine when used one a Search as below

```auto
  var searchResponse = await ElasticSearch.ClientPool.Client.SearchAsync<ElasticSearch.ESMPacketDocument>(u => u
                        .Index("mpackets")
                        .Query(q => q
                            .Bool(b => b
                                .Filter(f => f
                                    .Term(t => t.Field(F => F.fields.processed).Value(CycleKey))
                                )
                            )
                        )
                        .Size(MessagesPerQuery)
                        .Sort(s => s
                            .Field(f => f.timestamp, s => s.Order(SortOrder.Asc))
                        ), stoppingToken);

```

Update---  
It seems to have to do with having two items in there... The .Term and .Prefix. If I swap the order the other one comes through... how do I get both in the query?

---

<div class="post-metadata">

**Author:** ![Artem\_Shelkovnikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artem_shelkovnikov/32/134322_2.png) [@Artem\_Shelkovnikov](https://discuss.elastic.co/u/Artem_Shelkovnikov)\
**Post date:** [April 15, 2025, 8:53am UTC](https://discuss.elastic.co/t/elasticsearch-net-client-ignoring-term-in-query/376749/2 "2025-04-15T08:53:58Z")

</div>

Hi @kk123,

I'm not _super_ familiar with .net client for Elasticsearch, but I've looked into [docs](https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/bool-queries.html) and found examples like this:

```auto
var firstSearchResponse = client.Search<Project>(s => s
    .Query(q => q
        .Term(p => p.Name, "x") && q
        .Term(p => p.Name, "y")
    )
);

```

Might it work if you change your query to be like this?

```auto
var updateResult = await ElasticSearch.ClientPool.Client.UpdateByQueryAsync<ElasticSearch.ESMPacketDocument>("mpackets", u => u
    .Query(q => q
        .Bool(b => b                                
            .Must(f => f
                .Term(t => t.Field("fields.processed").Value(0)) && f
                .Prefix(p => p.Field("mqtt.topic").Value(TopicFilter))
            )
        )
    )
    .MaxDocs(MessagesPerQuery)
    .Script(s => s
        .Source($"ctx._source.fields.processed = {CycleKey}")
    )
    .Refresh(true), stoppingToken);

```

---

<div class="post-metadata">

**Author:** ![kk123](https://avatars.discourse-cdn.com/v4/letter/k/e56c9b/32.png) [@kk123](https://discuss.elastic.co/u/kk123)\
**Post date:** [April 23, 2025, 12:01pm UTC](https://discuss.elastic.co/t/elasticsearch-net-client-ignoring-term-in-query/376749/3 "2025-04-23T12:01:47Z")

</div>

Hi @Artem_Shelkovnikov, thanks for your reply. I managed to solve it in the end through trial and error. The problem was that I should not chain f.Term(...).Prefix(...)... I should rather have done

f=\> f.Term(...), f=\> f.Prefix(...)

so the Query portion should read

```auto
.Query(q => q
        .Bool(b => b                                
            .Must(f => f.Term(t => t.Field("fields.processed").Value(0)),
                       f => f.Prefix(p => p.Field("mqtt.topic").Value(TopicFilter))
            )
        )
    )

```

Hopefully that is useful to others...
