假设我有一个文档,当文档设置为该状态时,它可以具有状态以及时间戳。 我想知道文档的当前状态以及文档的状态历史记录。
基本上,我看到了三种建模方法:
选项1:
我有一个嵌套的state
对象,其名称和时间戳。
put doc_idx
{
"mappings":
{
"state_doc": {
"properties": {
"state": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
},
"date": {
"type": "date"
}
}
}
}
}
}
}
这可能是最干净的方法,因为没有多余的信息。 但是我还没有找到如何按文档的当前状态过滤文档的方法,因为它将是一个嵌套查询,其中只有具有最大时间戳的嵌套对象才应与状态匹配。
选项2:
我有一个嵌套的state
对象,其名称和时间戳为 和一个current
标志。
put doc_idx
{
"mappings":
{
"state_doc": {
"properties": {
"state": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
},
"date": {
"type": "date"
},
"current": {
"type": "boolean"
}
}
}
}
}
}
}
这将使按文档的当前状态过滤文档更加容易。
但这很容易出错,因为带有current
标志的状态可能少于或多于1。
选项3:
我有一个嵌套的state_history
对象,其名称和时间戳。
此外,还有一个current_state
和一个current_state_since
字段。
put doc_idx
{
"mappings": {
"state_doc": {
"properties": {
"current_state": {
"type": "keyword"
},
"current_state_since": {
"type": "date"
},
"state_history": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
},
"date": {
"type": "date"
}
}
}
}
}
}
}
此版本使按文档的当前状态非常容易过滤。 但是,设置文档的新状态将需要每次都更新当前状态和状态历史记录。
还有另一个更好的选择吗?如果选择方法1:我将如何按文档的当前状态过滤文档?