【问题标题】:how to add days to date range elastic search in c#?如何在 C# 中将天数添加到日期范围弹性搜索?
【发布时间】:2021-12-17 23:15:29
【问题描述】:

我想做这样的事情:

var client = new ElasticClient();

client.Search<MyDocument>(s => s
    .Query(q => q
        .DateRange(dr => dr
            .Field(f => f.AgreementCancelDate)
            .GreaterThanOrEquals("now-30d/d")
        )
    )
);

但不是“现在”,而是添加一些日期字段。

例如检查 expireDate 是否大于注册日期后 30 天:

.Field(f => f.ExipreDate)
            .GreaterThanOrEquals("doc.RegisterDate+30d/d")

【问题讨论】:

    标签: c# elasticsearch


    【解决方案1】:

    在弹性搜索中,无法比较文档的两个字段。

    您可以在最新的弹性搜索版本中使用runtime_mapping

    {
      "runtime_mappings": {
        "daysToExpireFromRegisteration": {
          "type": "long",
          "script": {
            "source": "emit((doc['ExipreDate'].value.millis -doc['RegisterDate'].value.millis)/ 1000 / 60 / 60 / 24)"
          }
        }
      },
      "query": {
         "range": {
           "daysToExpireFromRegisteration": {
             "gte": 10
           }
         }
      }
    }
    

    为了提高性能,您还可以索引差异并对其进行查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      相关资源
      最近更新 更多