# Search for documents matching some fields in a nested array

**URL:** <https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808>\
**Category:** Elasticsearch\
**Created:** [December 22, 2022, 6:44am UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808 "2022-12-22T06:44:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali5/32/113900_2.png) [@Ali5](https://discuss.elastic.co/u/Ali5)\
**Post date:** [December 22, 2022, 6:44am UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808/1 "2022-12-22T06:44:32Z")

</div>

I am learning to use Elasticsearch as a basic recommender engine. My elasticsearch document contains records with nested entities as follows

```auto
POST feeds/_doc
{
  "feed_id": "1",
  "title": "Mateen",
  "body": "This is mateen",
  "comment": [
    {
      "c_id": "11",
      "feed_id": "1",
      "c_text": "get well soon mateen"
    },
    {
      "c_id": "12",
      "feed_id": "1",
      "c_text": " Ali here"
    }
  ]
}
POST feeds/_doc
{
  
  "feed_id": "3",
  "title": "jose ",
  "body": "This is Jose allen",
  "comment": [
    {
      "c_id": "13",
      "feed_id": "2",
      "c_text": "hey"
    },
    {
      "c_id": "13",
      "feed_id": "3",
      "c_text": " here"
    }
  ]
}

```

The Mapping is

```auto
PUT feeds
{
  "mappings": {
    "properties": {
      "comment":{
        "type": "nested"
      }
    }
  }
}

```

I would like to search for feeds who specifically have title="mateen",body="Terminator" and comment.c\_text="Rambo". and if that text is not matced by any body and title and only matched in any comment.c\_text it will give me the body title and that matched comment.c\_text onlyyy

I have used a nested queries which currently looks like this

```auto
GET feeds/_search
{
  "query": {
    "nested": {
      "path": "comment",
      "query": {
        "multi_match": {
          "query": "ali",
          "fields": ["body","title","comment.c_text"]
        }
      }
    }
  }
}

```

and sceond try

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": {
              "query": "ali"
              
            }
          }
        },
        {
          "match": {
            "body": {
              "query": "ali"

            }
          }
        },
        {
          "nested": {
            "path": "comment",
            "query": {
              "match": {
                "comment.c_text": {
                  "query": "ali"
                  
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

however when i execute this queery i know there is no ali in body and title but it has ali in one of comment.c\_text so i am expecting to show me the body and title of that commenct as well can anyone help me in this senario

---

<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:** [December 22, 2022, 2:46pm UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808/2 "2022-12-22T14:46:03Z")

</div>

Hi @Ali5

Replace "must" to "should".

---

<div class="post-metadata">

**Author:** ![Ali5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali5/32/113900_2.png) [@Ali5](https://discuss.elastic.co/u/Ali5)\
**Post date:** [December 23, 2022, 5:13am UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808/3 "2022-12-23T05:13:55Z")

</div>

```auto
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.540445,
    "hits" : [
      {
        "_index" : "feeds",
        "_type" : "_doc",
        "_id" : "XYt7OIUBw_rYika3_AoI",
        "_score" : 1.540445,
        "_source" : {
          "feed_id" : "1",
          "title" : "Mateen",
          "body" : "This is mateen",
          "comment" : [
            {
              "c_id" : "11",
              "feed_id" : "1",
              "c_text" : "get well soon mateen"
            },
            {
              "c_id" : "12",
              "feed_id" : "1",
              "c_text" : " Ali here"
            }
          ]
        }
      }
    ]
  }
}

```

it showing me the full array i want if ali is mentioned 1 time in nested array then it show me only that conect of array that is matched @RabBit_BR

---

<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:** [December 23, 2022, 12:12pm UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808/4 "2022-12-23T12:12:43Z")

</div>

For get only the hit inside array you must use [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html#nested-inner-hits).

---

<div class="post-metadata">

**Author:** ![Ali5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali5/32/113900_2.png) [@Ali5](https://discuss.elastic.co/u/Ali5)\
**Post date:** [December 23, 2022, 12:18pm UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808/5 "2022-12-23T12:18:41Z")

</div>

i need the parent content as well as the ineer hit if it gona match something around only at nested array it will show me the parent fields as well as only mathced nested content

---

<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:** [January 20, 2023, 12:18pm UTC](https://discuss.elastic.co/t/search-for-documents-matching-some-fields-in-a-nested-array/321808/6 "2023-01-20T12:18:46Z")

</div>

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