# Searching two terms inside array of object

**URL:** <https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507>\
**Category:** Elasticsearch\
**Created:** [October 26, 2022, 11:16am UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507 "2022-10-26T11:16:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yashveer](https://avatars.discourse-cdn.com/v4/letter/y/74df32/32.png) [@yashveer](https://discuss.elastic.co/u/yashveer)\
**Post date:** [October 26, 2022, 11:16am UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507/1 "2022-10-26T11:16:37Z")

</div>

```auto
"rno" : 1,
"narratives": [
            {
              "compl": "call drop",
              "ts": "2015-05-01"
            },
            {
              "compl": "internet connection lost ",
              "ts": "2016-01-15"
            },
            {
              "compl": "water problem ",
              "ts": "2016-01-20"
            }
          ]

need to write query which can search "call" and "lost" in the narrative field.
i tried match, must and rest but both are not working on array of object.
```

---

<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:** [October 26, 2022, 11:48am UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507/2 "2022-10-26T11:48:10Z")

</div>

Hi @yashveer

If your field is nested type you need to use [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html).

```auto
{
  "query": {
    "nested": {
      "path": "narratives",
      "query": {
        "match": {
          "narratives.compl": "call lost"
        }
      }, "inner_hits": {}
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![yashveer](https://avatars.discourse-cdn.com/v4/letter/y/74df32/32.png) [@yashveer](https://discuss.elastic.co/u/yashveer)\
**Post date:** [October 26, 2022, 12:13pm UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507/3 "2022-10-26T12:13:48Z")

</div>

Hi @RabBit_BR

this query using OR operator and returning records with either call or lost or both. but i need to return records which has both of the keywords like "call and lost".

---

<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:** [October 26, 2022, 12:17pm UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507/4 "2022-10-26T12:17:10Z")

</div>

you can use operator and.

```auto
"match": {
          "narratives.compl": "call lost",
          "operator": "AND"
        }

```

---

<div class="post-metadata">

**Author:** ![yashveer](https://avatars.discourse-cdn.com/v4/letter/y/74df32/32.png) [@yashveer](https://discuss.elastic.co/u/yashveer)\
**Post date:** [October 26, 2022, 12:23pm UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507/5 "2022-10-26T12:23:07Z")

</div>

@RabBit_BR

we cant use operator with match, operator "and" is allowed with query. more over it will try to search both terms together in the same field. which is again not the case.

---

<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:** [November 23, 2022, 12:23pm UTC](https://discuss.elastic.co/t/searching-two-terms-inside-array-of-object/317507/6 "2022-11-23T12:23:44Z")

</div>

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