0

I have a record in elastic search index:-

 "hits" : {
"total" : {
  "value" : 1,
  "relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
  {
    "_index" : "index",
    "_type" : "_doc",
    "_id" : "C3wfF3kBVSP1PGuoao73",
    "_score" : 0.0,
    "_source" : {
      "rId" : 1066,
      “categoriesData" : [
        {
          "categoryNumber" : 13,
          "depNumber : 98,
          "storeIds" : [
            "3", 
            "6"
          ],
          "fData" : {
            "Type" : “Single
          }
        },
        {
          "categoryNumber" : 12,
          "depNumber" : 97,
          "storeIds" : [
            "3629",
            "3628"
          ],
          "Data" : {
            "Type" : “DOUBLE
          }
        }
      ],
      "sId" : "35EB8012-AA52-4872-A1A2-92522BD3925F"
    }
  }
]

}

Here categoriesData is nested.I am trying to query categoryNumber 13 and depNumber 98. I want only that dict object with categoryNumber 13 and depNumber 98

    This is my query:- 
    GET /index/_search
     {
     "query": {
       "bool": {
         "must": [
        {
          "nested": {
             "path": "categoriesData",
            "query": {
             "bool": {
              "must": [
              {
                "term": {
                  "categoriesData.categoryNumber": 13
                }
              },
              {
                "term": {
                  "categoriesData.depNumber": 98
                }
              }
            ]
          }
        }
      }
    }
  ]
  }
  }
 }

When trying to above query it is giving me all the document. My requirement is to get only documents with depNumber and catagegory number Expected Output:- Only one object from nested field.

   "categoriesData" : [
    {
      "categoryNumber" : 13,
      "depNumber : 98,
      "storeIds" : [
        "3", 
        "6"
      ],
      "fData" : {
        "Type" : “Single
      }
    }

My Mapping :-

     {"mappings": {"properties": 
                                    {"rId": {  "type": "integer"},
                                    "sId": { "type": "keyword" },
                                    "CategoriesData" : {"type":"nested","properties" :{"depnumber”: {"type":"integer"},"categoryNumber”: {"type":"integer"},"storeIds": {"type":"keyword"},"fData" : {"type":"object"}}}}}}

      

Is there any way to write the query to get only specific objects with matching terms

1 Answer 1

2

You can use inner_hits to get only the exact matching object from the nested documents

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "categoriesData",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "categoriesData.categoryNumber": 13
                    }
                  },
                  {
                    "term": {
                      "categoriesData.depNumber": 98
                    }
                  }
                ]
              }
            },
            "inner_hits":{}         // note this
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@CodeCool please go through the answer, and let me know if this resolves your issue ?
@ESCoder It will work . but its giving multiple hits. How it will work in case of large data sets

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.