[英]How to compare two date fields in same document in elasticsearch
In my elastic search index, each document will have two date fields createdDate
and modifiedDate
.在我的弹性搜索索引中,每个文档都有两个日期字段createdDate
和modifiedDate
。 I'm trying to add a filter in kibana to fetch the documents where the modifiedDate
is greater than createdDate
.我正在尝试在 kibana 中添加一个过滤器来获取modifiedDate
大于createdDate
的文档。 How to create this filter in kibana?如何在kibana中创建这个过滤器?
Tried Using below query instead of greater than it is considering as gte and fetching all records尝试使用下面的查询而不是大于它被认为是 gte 并获取所有记录
GET index/_search
{
"query": {
"bool": {
"filter": {
"script": {
"script" : {
"inline" : "doc['modifiedTime'].value.getMillis() > doc['createdTime'].value.getMillis()",
"lang" : "painless"
}
}
}
}
}
}
There are a few options.有几个选项。
Option A: The easiest and most performant one is to store the difference of the two fields inside a new field of your document, eg选项 A:最简单和最高效的一种是将两个字段的差异存储在文档的新字段中,例如
{
"createDate": "2022-01-11T12:34:56Z",
"modifiedDate": "2022-01-11T12:34:56Z",
"diffMillis": 0
}
{
"createDate": "2022-01-11T12:34:56Z",
"modifiedDate": "2022-01-11T12:35:58",
"diffMillis": 62000
}
Then, in Kibana you can query on diffMillis > 0
and figure out all documents that have been modified after their creation.然后,在 Kibana 中,您可以查询diffMillis > 0
并找出在创建后已修改的所有文档。
Option B: You can use a script
query选项 B:您可以使用script
查询
GET index/_search
{
"query": {
"bool": {
"filter": {
"script": {
"script": """
return doc['createdDate'].value.millis < doc['modifiedDate'].value.millis;
"""
}
}
}
}
}
Note: depending on the amount of data you have, this option can potentially have disastrous performance, because it needs to be evaluated on ALL of your documents.注意:根据您拥有的数据量,此选项可能会带来灾难性的性能,因为需要对您的所有文档进行评估。
Option C: If you're using ES 7.11+, you can use runtime fields directly from the Kibana Discover view .选项 C:如果您使用的是 ES 7.11+,则可以直接从Kibana Discover 视图中使用运行时字段。
You can use the following script in order to add a new runtime field (eg name it diffMillis
) to your index pattern:您可以使用以下脚本向索引模式添加新的运行时字段(例如,将其命名为diffMillis
):
emit(doc['modifiedDate'].value.millis - doc['createdDate'].value.millis)
And then you can add the following query into your search bar然后您可以将以下查询添加到您的搜索栏中
diffMillis > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.