Search code examples
elasticsearchnest

NEST query isn't working with ElasticSearch when query parent


Good day:

I have a nested object and I'm trying to query both the outer object and the inner nested object property called facilityManagements:

POST /dev/doc/_search?typed_keys=true
{  
   "query":{  
      "nested":{  
         "query":{  
            "bool":{  
               "must":[  
                  {  
                     "term":{  
                        "facilityManagements.userId":{  
                           "value":"ad73ef37-243b-400f-b68f-679fa9116769"
                        }
                     }
                  },
                  {  
                     "term":{  
                        "id":{  
                           "value":"ed08d01c-c791-46de-b3f7-2ab7e1f8823d"
                        }
                     }
                  }
               ]
            }
         },
         "path":"facilityManagements",
         "inner_hits":{  
            "name":"facilityManagement",
            "size":1
         }
      }
   }
}

This returns an empty response. However, when I do match_all it does return the document:

GET dev/doc/_search 
{

  "query" : {
"match_all": {}
  }
}

Response:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "dev",
        "_type": "doc",
        "_id": "ed08d01c-c791-46de-b3f7-2ab7e1f8823d",
        "_score": 1,
        "_routing": "ed08d01c-c791-46de-b3f7-2ab7e1f8823d",
        "_source": {
          "name": "Facility 123456",
          "status": 0,
          "registrationDate": "0001-01-01T00:00:00",
          "address": "22100 Sunset Hills Rd suite 150*",
          "zipCode": "20190",
          "city": "Reston",
          "state": "Virginia",
          "facilityManagements": [
            {
              "userId": "ad73ef37-243b-400f-b68f-679fa9116769",
              "operations": 1,
              "isOwner": true,
              "isActive": true,
              "createdDate": "2018-07-16T07:47:04.1491212Z",
              "id": "5520c28c-614e-40ca-8bde-5f84e83d3829"
            }
          ],
          "approved": false,
          "businessLocation": {
            "lat": 37.5536729,
            "lon": -122.3506495
          },
          "suggest": {
            "input": [
              "Reston",
              "Virginia",
              "20190",
              "Facility",
              "123456",
              "22100",
              "Sunset",
              "Hills",
              "Rd",
              "suite",
              "150*"
            ]
          },
          "joinField": "parent",
          "id": "ed08d01c-c791-46de-b3f7-2ab7e1f8823d"
        }
      }
    ]
  }
}

Also when I just have the facilityManagements.userId, it does return the result but, the inclusion of id makes the query not work.


Solution

  • The term query on the id field is currently inside of a nested bool query;

    • if the query should be on id field of facilityManagements, it should be prefixed with facilityManagements i.e. facilityManagements.id

    or

    • if the query is on the top level id field, it should be part of a bool query that contains the nested query, and not inside of the nested query.