Elastic Search嵌套查询应仅返回匹配的记录

时间:2018-08-01 06:26:09

标签: database elasticsearch

请参见数据库的以下结构-

{
  "_index": "books",
  "_type": "content",
  "_id": "meta/book1.json",
  "_version": 1,
  "_score": 1,
  "_source": {    
    "category": "General",
    "fileCategory": "ICD 10",
    "documentType": "metaJsonContent",
    "metaJsonContent": [
      {
        "Name": "10.43 -  Type 1 diabetes w diabetic autonomic (poly)neuropathy",
        "TargetId": "q/tu2.htm",
        "Locked": "false",
        "Anchor": "#10.436"
      },
      {
        "Name": "250.01 -  Diabetes mellitus without mention of complication",
        "items": [
          {
            "Name": "Diabetes Mellitus, Type 1 - 250.011",
            "TargetId": "q/tu2.htm"           
          },
          {
            "Name": "Diabetes Mellitus, Type 1 - 250.0141",
            "TargetId": "q/tu2.htm"           
          }
        ]
      }
     ]
   }
}

查询

GET books/content/_search
{
  "collapse": {
    "field": "fileCategory",
    "inner_hits": {
      "name": "collapseByField",
      "_source": {
        "includes": [

        ]
      }
    }
  },
  "query": {
    "nested": {
      "path": "metaJsonContent",
      "query": {
        "bool": {
          "must": [
             {
              "match": {
                "metaJsonContent.Name": " fever  009.511"
              }
            },
            {"nested": {
                "path": "metaJsonContent.items",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "metaJsonContent.items.Name": "fever"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "inner_hits": {

      }
    }
  },
  "_source": {
    "includes": [
      ""
    ]
  }
}

以上查询返回结果,但我只需要获取匹配的父记录和子记录。当前,如果与父项匹配,它将返回所有子元素。我必须检查父母姓名和子女姓名,如果与父母姓名匹配,则应该返回父母姓名。如果与孩子匹配,则应返回孩子。或可能在父母和子女中都可用。有人可以帮助查询中的问题吗?

谢谢。

0 个答案:

没有答案