# Elasticsearch query on specific key

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731>\
**Category:** Elasticsearch\
**Created:** [May 14, 2022, 3:21pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731 "2022-05-14T15:21:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mostafaelsayed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mostafaelsayed/32/82057_2.png) [@mostafaelsayed](https://discuss.elastic.co/u/mostafaelsayed)\
**Post date:** [May 14, 2022, 3:21pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/1 "2022-05-14T15:21:01Z")

</div>

Suppose I have these 3 docs in Elasticsearch

```auto
{
    id: "1",
    eventName: "ShipmentLoaded"
},
{
    id: "1",
    eventName: "ShipmentPlanned"
},
{
    id: "2",
    eventName: "ShipmentLoaded"
}

```

I want to retrieve the docs where ids are having docs with eventName=ShipmentLoaded and Not having other docs with eventName=ShipmentPlanned  
so the query will return only the last document with id=2 because it does have doc with eventName=ShipmentLoaded and does not have other doc with eventName=ShipmentPlanned

what kind of query that we can use to achieve this?

---

<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:** [May 14, 2022, 4:11pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/2 "2022-05-14T16:11:04Z")

</div>

Hi @mostafaelsayed

I don't know how your mapping but if **eventName** is a keyword you can use Term query.

```auto
{
  "query": {
    "term": {
      "eventName": {
        "value": "ShipmentLoaded"
      }
    }
  }
}

```

> **[Term query | Elasticsearch Guide \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html)**

---

<div class="post-metadata">

**Author:** ![mostafaelsayed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mostafaelsayed/32/82057_2.png) [@mostafaelsayed](https://discuss.elastic.co/u/mostafaelsayed)\
**Post date:** [May 14, 2022, 4:42pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/3 "2022-05-14T16:42:38Z")

</div>

Hi @RabBit_BR This will not work because it will also retrieve the doc with id=1 but I don't want to retrieve it because it has another doc with eventName=ShipmentPlanned

---

<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:** [May 14, 2022, 4:54pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/4 "2022-05-14T16:54:07Z")

</div>

Sorry, I read it wrong.

Do you need the doc with id:2 or the docs "eventName": "ShipmentLoaded"?  
Perhaps [Field Collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-collapse.html#search-request-collapse) could be an alternative.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 14, 2022, 4:54pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/5 "2022-05-14T16:54:25Z")

</div>

What you are describing would be a join between records, which Elasticsearch does not support. You could however create a separate index using a transform where there is one document per id, which could likely support your query.

---

<div class="post-metadata">

**Author:** ![mostafaelsayed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mostafaelsayed/32/82057_2.png) [@mostafaelsayed](https://discuss.elastic.co/u/mostafaelsayed)\
**Post date:** [May 14, 2022, 5:14pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/6 "2022-05-14T17:14:37Z")

</div>

Hi @RabBit_BR as mentioned by @Christian_Dahlqvist I basically want a join query where eventName: ShipmentLoaded and for those documents ids we don't have another documents with eventName: ShipmentPlanned

---

<div class="post-metadata">

**Author:** ![mostafaelsayed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mostafaelsayed/32/82057_2.png) [@mostafaelsayed](https://discuss.elastic.co/u/mostafaelsayed)\
**Post date:** [May 14, 2022, 5:17pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/7 "2022-05-14T17:17:15Z")

</div>

Thanks @Christian_Dahlqvist I guess this will be a way to achieve this or another way from the application side by making two queries

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 14, 2022, 5:44pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/8 "2022-05-14T17:44:25Z")

</div>

That may work as well.

---

<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:** [June 11, 2022, 5:45pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-specific-key/304731/9 "2022-06-11T17:45:00Z")

</div>

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