elasticsearch 实体关系处理

Application-side joins
  • 在应用程序端单独写代码处理
Data denormalization
  • 冗余信息
Nested objects
PUT /my_index/blogpost/1
{
  "title": "Nest eggs",
  "body":  "Making your money work...",
  "tags":  [ "cash", "shares" ],
  "comments": [ 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    },
    {
      "name":    "Alice White",
      "comment": "More like this please",
      "age":     31,
      "stars":   5,
      "date":    "2014-10-22"
    }
  ]
}
  • 将comments 作为数组内嵌到 blogpost 中
  • 注意事项:These extra nested documents are hidden; we can’t access them directly. To update, add, or remove a nested object, we have to reindex the whole document. It’s important to note that, the result returned by a search request is not the nested object alone; it is the whole document.
  • 如果nest的object改动比较频繁将会比较影响性能
Parent/child relationships
  • 优点
  • The parent document can be updated without reindexing the children.
  • Child documents can be added, changed, or deleted without affecting either the parent or other children. This is especially useful when child documents are large in number and need to be added or changed frequently.
  • Child documents can be returned as the results of a search request
  • 注意事项
  • it does place a limitation on the parent-child relationship: the parent document and all of its children must live on the same shard.

References