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

CodeCool
  • 193
  • 2
  • 12

1 Answers1

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
          }
        }
      ]
    }
  }
}
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • @CodeCool please go through the answer, and let me know if this resolves your issue ? – ESCoder Apr 28 '21 at 09:49
  • kindly advice me on this https://stackoverflow.com/questions/67298541/run-several-indexes-as-a-service-using-fscrawler – Denn Apr 28 '21 at 11:50
  • @ESCoder It will work . but its giving multiple hits. How it will work in case of large data sets – CodeCool Apr 28 '21 at 12:58