How to get matched values from array without getting all the values in it, using a dynamic template without type nested?

I tried to search inside array of object using match query but I got all the values inside the array, because the array is not of type nested.

I am using dynamic template to create index.

Is there any way to search single object which matches the given criteria instead of getting all the values in the array? I had tried multiple methods using query but the issue is the template is not of type nested.

Kindly provide any insights regarding this or any workaround is there to get single object from array which matches.

I am not using type nested since it has certain limitations.

My document looks like this.

{
  "productpk_s": "PROD001",
  "product": {
    "productpk_s": "PROD001",
    "mfgname_s": "TEST MFG"
  },
  "salesorder": [
    {
      "salesorderpk_s": "SO001",
      "productpk_s": "PROD001",
      "vendorname_s": "Mitsubishi"
    },
    {
      "salesorderpk_s": "SO002",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    },
    {
      "salesorderpk_s": "SO003",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    },
    {
      "salesorderpk_s": "SO004",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    },
    {
      "salesorderpk_s": "SO005",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    }
  ]
}

Sample query I tried:

{
  "_source": [
    "product.productpk_s",
    "salesorder.salesorderpk_s",
    "salesorder.vendorname_s"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": { "salesorder.vendorname_s.keyword": "Mitsubishi" }
        }
      ]
    }
  }
}

I think I am doing something wrong.

Kindly suggest, how the search can be performed without type nested in dynamic template.

What you are describing is exactly what nested documents are for so i am not aware of any way to do this using flat documents and get the benefits without the drawbacks.

Use script_fields source

{
  "size": 10,
  "query": {
    "match": {
      "acc_names": {
        "query": "network"
      }
    }
  },
  "script_fields": {
    "position": {
      "script": {
        "source": "def newarr=[];for(an in doc['name_arr'].values) if (an.toLowerCase().indexOf('search_text')!=-1) newarr.push(an); return newarr;"
      }
    },
    "other_fields": {
      "script": {
        "source": "doc['other_fields'].value"
      }
    },
    "one_more_field": {
      "script": {
        "source": "doc['one_more_field'].values"
      }
    }
  }
}

is there any reason you are not using the template
http://localhost:9200/_template

No reason Ramkumar for not using _template.

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