【问题标题】:Return only top level fields in elasticsearch query?仅返回弹性搜索查询中的顶级字段?
【发布时间】:2020-06-02 22:11:32
【问题描述】:

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

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

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

【问题讨论】:

  • 你能显示你现在发送的查询吗?
  • 是的,我已经更新了帖子

标签: elasticsearch


【解决方案1】:

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

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

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

【讨论】:

  • 是的,我一直在搜索是否有类似“排除”:“嵌套”之类的东西,以排除除顶级字段之外的所有内容,而不必在“排除”
猜你喜欢
  • 2016-07-28
  • 1970-01-01
  • 2016-03-27
  • 2018-11-03
  • 2021-02-10
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
相关资源
最近更新 更多