查询父级时,NEST查询不适用于ElasticSearch

时间:2018-07-16 09:00:00

标签: elasticsearch nest

美好的一天:

我有一个嵌套对象,我正在尝试查询称为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
         }
      }
   }
}

这将返回一个空响应。但是,当我执行match_all时,它确实会返回文档:

GET dev/doc/_search 
{

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

响应:

{
  "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"
        }
      }
    ]
  }
}

此外,当我只有facilityManagements.userId时,它确实会返回结果,但是包含id会使查询不起作用。

1 个答案:

答案 0 :(得分:1)

term字段上的id查询当前在nested bool查询中;

  • 如果查询应位于id的{​​{1}}字段上,则应以facilityManagements为前缀,即facilityManagements

  • 如果查询位于顶级facilityManagements.id字段中,则它应该是包含id查询的bool查询的一部分,而不是nested的内部查询。