仅返回elasticsearch查询中的顶级字段?

时间:2020-06-02 05:01:22

标签: elasticsearch

我有一个包含嵌套字段的文档。示例:

    "mappings": {
        "blogpost": {
          "properties": {
            "title": { "type": "text"  },
            "body":  { "type": "text"  },
            "comments": {
              "type": "nested", 
              "properties": {
                "name":    { "type": "text"  },
                "comment": { "type": "text"  },
                "age":     { "type": "short"   },
                "stars":   { "type": "short"   },
                "date":    { "type": "date"    }
              }
            }
          }
        }
  }
}

能否修改查询,使响应仅包含非嵌套字段?

在此示例中,响应将仅包含bodytitle

使用_source可以排除/包含字段

GET /blogpost/_search
{
    "_source":{
        "excludes":["comments"]
    }
}

但是您必须将字段名称明确地放在排除中,我正在寻找一种排除所有嵌套字段而又不知道其字段名称的方法

1 个答案:

答案 0 :(得分:0)

您可以通过静态方式实现,这意味着您使用excludes关键字输入了字段名称,例如:

GET your_index/_search
{
    "_source": {
        "excludes": "comments"  
    },
    "query": {
        "match_all" : {}
    }
}

excludes可以接受array个字符串;不只是一个字符串。