Limit size of nested collection on Query Result


(Rupanjan Baidya) #1

I am fairly a new user of Elasticsearch. Struggling a bit to build a query which limits the no of collections on a given nested field.
Elasticsearch version 6.1

One product can have multiple stores as below,

 "hits" : {
    "total" : 1,
    "max_score" : 7.826804,
    "hits" : [
      {
        "_index" : "product_v1",
        "_type" : "product",
        "_id" : "500474",
        "_score" : 7.826804,
        "_source" : {
          "@version" : "1",
          "priorty" : 3378,
          "sku_parent_code" : null,
          "sku_description" : "What It Is\nA gentle and safe toothbrush made with ultra-soft Tynex bristles to suit the delicate gums of babies. It is suitable for babies aged 6 months and above and comes in bright and fun colors that children will love.\n\nWhat It Does\nThe perfect soluti",
          "store" : [
            {
              "batch_no" : 29742,
              "camp_type" : null,
              "list_price" : 30.0,
              "ofc_code" : "110",
              "sku_shelf_life" : 6,
              "disc_percentage" : 0,
              "sku_available_unit" : 9,
              "camp_code" : null,
              "default_price" : 30.0
            },
            {
              "batch_no" : 29742,
              "camp_type" : null,
              "list_price" : 30.0,
              "ofc_code" : "176",
              "sku_shelf_life" : 6,
              "disc_percentage" : 0,
              "sku_available_unit" : 9,
              "camp_code" : null,
              "default_price" : 30.0
            }
          ],
          "category" : [
            {
              "category_code" : 3233,
              "category_name" : "Baby Essentials"
            },
            {
              "category_code" : 3293,
              "category_name" : "Toothbrushes"
            }
          ],,
          "sku_name" : "Johnsons Baby Soft Toothbrush",
          "sku_code" : "500474",
          "sku_title" : "Johnsons Baby Toothbrush",
          "brand" : {
            "brand_code" : 5135,
            "brand_name" : "Johnson & Johnson"
          }
        }
      }
    ]
  }

sample query I have tried with,

{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "sku_code": "500474"
               }
            },
	        "nested": {
	            "path": "store",
	            "size" : 1,
	            "query": {
	               "bool": {
	                  "must": [
	                     {
	                        "term": {
	                           "store.ofc_code": "110"
	                        }
	                     }
	                  ]
	               }
	            }
	        }
         ]
      }
   }
}

Response should contain only the matched store element when queried with sku_code & store_code

 "hits" : [
      {
        "_index" : "product_v1",
        "_type" : "product",
        "_id" : "500474",
        "_score" : 7.826804,
        "_source" : {
          "@version" : "1",
          "priorty" : 3378,
          "sku_parent_code" : null,
          "sku_description" : "What It Is\nA gentle and safe toothbrush made with ultra-soft Tynex bristles to suit the delicate gums of babies. It is suitable for babies aged 6 months and above and comes in bright and fun colors that children will love.\n\nWhat It Does\nThe perfect soluti",
          "store" : [
            {
              "batch_no" : 29742,
              "camp_type" : null,
              "list_price" : 30.0,
              "ofc_code" : "176",
              "sku_shelf_life" : 6,
              "disc_percentage" : 0,
              "sku_available_unit" : 9,
              "camp_code" : null,
              "default_price" : 30.0
            }
          ],
          "category" : [
            {
              "category_code" : 3233,
              "category_name" : "Baby Essentials"
            },
            {
              "category_code" : 3293,
              "category_name" : "Toothbrushes"
            }
          ],,
          "sku_name" : "Johnsons Baby Soft Toothbrush",
          "sku_code" : "500474",
          "sku_title" : "Johnsons Baby Toothbrush",
          "brand" : {
            "brand_code" : 5135,
            "brand_name" : "Johnson & Johnson"
          }
        }
      }
    ]
  }
}

Any help will be greatly appreciated.


(system) #2

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