es nested

要想能用父子级联查询 首先 index得设置成 nested 类型,如

PUT my_index
{
“mappings”: {
“_doc”: {

  "properties": {
    "manager": { 


      "properties": {
        "age":  { "type": "integer" },
        "name": { "type": "text"  }
      }
    },
    "employees": { 

“type”: “nested”,
“properties”: {
“age”: { “type”: “integer” },
“name”: { “type”: “text” }
}
}
}
}
}
}
查询的时候 即可使用
GET my_index/_search
{
“query”: {
“match”: {
manager.name”: “Alice White”
}
},
“aggs”: {
“Employees”: {
“nested”: {
“path”: “employees”
},
“aggs”: {
“Employee Ages”: {
“histogram”: {
“field”: “employees.age”,
“interval”: 5
}
}
}
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Elasticsearch中的nested是一种数据类型,它允许在一个文档中嵌套另一个文档。这种嵌套结构可以用于处理复杂的数据结构,例如嵌套的数组或对象。使用nested类型可以方便地进行嵌套文档的查询和聚合操作。但是,由于nested类型需要额外的存储和计算资源,因此在使用时需要谨慎考虑性能问题。 ### 回答2: elasticsearch中的嵌套(nested)可以用于存储和查询具有多层级数据结构的文档。举一个例子,假设我们要存储一个具有以下结构的文档: ```json { "title": "book", "authors": [ { "name": "John Smith", "age": 25 }, { "name": "Jane Doe", "age": 30 } ] } ``` 在没有嵌套(nested)的情况下,我们可以使用对象(object)和嵌套数组(nested array)来实现此结构,但这样做可能会导致查询结果不准确,因为嵌套数组(nested array)会将子文档的属性(apparatus)拆分为单独的文档进行索引。 使用嵌套对象(nested object)可以避免这种情况。它是一个将所有子文档作为单独索引文档的新文档,同时它也是独立于父文档的索引文档,并且其父文档中的任何查询都不会影响其子文档。 以下是如何在elasticsearch中创建嵌套字段的示例映射(mapping): ```json PUT /books { "mappings": { "properties": { "title": { "type": "text" }, "authors": { "type": "nested", "properties": { "name": { "type": "text" }, "age": { "type": "integer" } } } } } } ``` 在查询中,我们可以使用嵌套查询(nested query)来查询特定子文档属性: ```json GET /books/_search { "query": { "nested": { "path": "authors", "query": { "bool": { "must": [ { "match": { "authors.name": "John Smith" } }, { "range": { "authors.age": { "gte": 25, "lte": 30 } } } ] } }, "inner_hits": {} } } } ``` 此查询将返回包含名称为"John Smith"和年龄在25到30之间的作者的所有书籍,并且它们的作者子文档可以使用内部内部(hit)查询通过“inner_hits”返回。 ### 回答3: Elasticsearch nested是Elasticsearch中的一种数据类型,用于处理嵌套对象的索引和查询。这种数据类型允许将一个对象嵌套在另一个对象中,形成一个更复杂的数据结构。在以往的Elasticsearch版本中,如果我们需要处理这样的嵌套数据结构,通常会使用parent/child的数据结构来处理。但是在大数据集的情况下,使用parent/child会导致性能问题,因为每次查询都需要合并来自多个分片的结果集。 相比于parent/child的数据结构,Elasticsearch nested允许我们将所有的相关数据存储在同一个文档中,并利用Elasticsearch的分布式查询引擎同时查询所有的文档。这就大大提高了查询性能。同时,nested支持多级嵌套对象的索引和查询,可以轻松地处理非常复杂的数据结构。 在创建映射时,需要将嵌套对象定义为nested类型,如下所示: ``` PUT /nested_index { "mappings": { "properties": { "name": {"type": "text"}, "age": {"type": "integer"}, "books": {"type": "nested", "properties": { "title": {"type": "text"}, "author": {"type": "text"} } } } } } ``` 在上述映射中,我们定义了一个nested类型的books属性,其中包含title和author两个嵌套属性。这样,我们就可以将books属性作为一个完整的对象存储在Elasticsearch中,并能够进行复杂的查询操作。例如,我们可以查询所有书中作者为"John"的书: ``` POST /nested_index/_search { "query": { "nested": { "path": "books", "query": { "match": { "books.author": "John" } } } } } ``` 通过将nested属性定义为nested类型,我们能够轻松处理复杂数据结构,并提高查询性能。但需要注意的是,嵌套对象需要占用更多的存储空间,因此需要合理使用。同时,在进行复杂的查询操作时,需要对查询进行优化,以避免影响性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值