Search for documents matching some fields in a nested array

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

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

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

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

and sceond try

{
  "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

Hi @Ali5

Replace "must" to "should".

{
  "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

For get only the hit inside array you must use inner_hits.

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

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