# elasticsearch query for selecting records where agent1 is one of the agents but there are also other agents

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-for-selecting-records-where-agent1-is-one-of-the-agents-but-there-are-also-other-agents/366678>\
**Category:** Elasticsearch\
**Created:** [September 17, 2024, 1:47pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-selecting-records-where-agent1-is-one-of-the-agents-but-there-are-also-other-agents/366678 "2024-09-17T13:47:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali7/32/137175_2.png) [@ali7](https://discuss.elastic.co/u/ali7)\
**Post date:** [September 17, 2024, 1:47pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-selecting-records-where-agent1-is-one-of-the-agents-but-there-are-also-other-agents/366678/1 "2024-09-17T13:47:44Z")

</div>

I have an elasticsearch index that has this fields: user\_id, agent\_name, city, ...

I want to run a query for selecting records where agent\_name "agent1" is one of the agents but there are also other agents, and then select those that their agents are in more than 2 cities.

I used this query:

query = {

```
"size": 0, # Don't need the actual documents, just the aggregation results

"query": {

        {"match": {agent_name: 'agent1'}

},

"aggs": {

    "user_ids": {

        "terms": {"field": "user_id", "size": 60000},

        "aggs": {

            "city_count": {"cardinality": {"field": "city"}}

        }

    }

},

```

}

in above example if i run the query it first of all, selects records that just all of agent\_name values is 'agent1', then aggregates the result, this way i get the result if agent1 is active in 2 cities, but i want it to select a record if one of it's agent is 'agent1' and also have other agents other than agent1, how should I change 'query' part to get desired result?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 17, 2024, 2:21pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-selecting-records-where-agent1-is-one-of-the-agents-but-there-are-also-other-agents/366678/2 "2024-09-17T14:21:27Z")

</div>

Not sure I fully understood the use case.

You want a query which matches if agent is agent1 or agent is whoever?

Or agent is agent1 but there are other agents as well? Meaning that the agent is not alone?

---

<div class="post-metadata">

**Author:** ![ali7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali7/32/137175_2.png) [@ali7](https://discuss.elastic.co/u/ali7)\
**Post date:** [September 18, 2024, 9:30am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-selecting-records-where-agent1-is-one-of-the-agents-but-there-are-also-other-agents/366678/3 "2024-09-18T09:30:48Z")

</div>

I have this fields: customer\_id, agent\_name, city, ..  
assume an insurance company that agents call to customers for marketing,  
customers are moving to different cities, and city of customer at the calling time is recorded in my index.  
I want a query to get other agents that called 'customer1', if 'agent1' called 'customer1' and if the city of resulted records is different from 'agent1' and 'customer1' city
