c# - 在 Elasticsearch 和嵌套中传递和比较具有不同时区的日期时间值

标签 c# datetime elasticsearch timezone nest

我正在尝试将日期时间值作为参数(时区“UTC”中的时区不可识别值)传递到一个查询中,该查询将参数与日期时间值(时区“-05:00”中的时区识别值)进行比较') 在 Elasticsearch 文档中并返回文档。

问题:
1. 我在下面的代码中做得对吗?
2. 在将日期时间参数与文档日期时间值进行比较之前,ES 是否会处理日期时间参数不同时区的复杂性?
3. ES 是否会返回具有原始日期时间时区值的文档?
4. 是否有任何文档揭示了 Elasticsearch 实例时区处理的内部结构?

下面的代码返回值,但我不确定它是否达到了预期的目标。
按照这个link但它仅限于一些基础知识
提前致谢。

ES源文件:

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },

  "hits" : {
    "total" : 2700881,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "doc",
        "_id" : "R22224!!5333e7e4-9ee3-45f4-9dc3-2a8b8d8cdcf8",
        "_score" : 1.0,
        "_source" : {
          "duration" : 14986283,
          "group_id" : "com",
          "var_time" : "2018-04-24T17:05:13.082-05:00",
          "var_name" : "2",
        }
      }
    ]
  }
}


public class RawDocument
{

    [PropertyName("duration")]
    public long Duration { get; set; }

    [PropertyName("group_id")]
    public string GroupId { get; set; }

    [PropertyName("var_time")]
    public DateTime Vartime { get; set; }

    [PropertyName("var_name")]
    public string Varname { get; set; }

}


static void Main(string[] args)
{

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultMappingFor<RawDocument>(m => m
              .IndexName(test_index)
              .TypeName("doc"));

    var client = new ElasticClient(settings);

    //argument being passed into the query is maxExtractionTime datetime values. maxExtractionTime is in UTC
    //maxExtractionTime is being compared to var_time datetime with timezone values
    DateTime maxExtractionTime;
    maxExtractionTime = DateTime oDate = DateTime.ParseExact("2019-02-08 16:10:40.000", "yyyy-MM-dd HH:mm.fff", System.Globalization.CultureInfo.InvariantCulture);

    var searchResponse = client.Search<RawDocument>(s => s
                .Size(5000)
                .Scroll("2m")
                .Query(q => q.DateRange(m => m.Field("var_time").GreaterThanOrEquals(maxExtractionTime.ToString("yyyy-MM-dd HH:mm:ss.fff")).TimeZone("+00:00")))                
                );


    while (searchResponse.Documents.Any())
        {

            foreach (var document in searchResponse.Hits)
            {
            //do some business logic on the values
            MessageBox.Show("document.Source.Duration", document.Source.Duration);
            MessageBox.Show("document.Source.Vartime", document.Source.Vartime);

            }
            searchResponse = client.Scroll<RawDocument>("2m", searchResponse.ScrollId);
        }   

    var clearScrollResponse = client.ClearScroll(c => c.ScrollId(searchResponse.ScrollId));         
}

最佳答案

从您提供链接的文档:

now is not affected by the time_zone parameter (dates must be stored as UTC)

从这个以及该页面上的其他示例,可以看出 time_zone 参数(或 C# 中的 .TimeZone(...))会影响查询的输入值。通过传递 +00:00,您表示输入值不需要调整。

日期本身必须以 UTC 格式存储在文档中才能正确执行范围查询。您不应将 var_time 存储为 2018-04-24T17:05:13.082-05:00,而应将其存储为 2018-04-24T17:10:13.082 Z。然后你可以查询它。

如有必要,存储两个字段,这样您就有一个用于查询,一个用于显示原始本地时间。

回答您的具体问题:

  1. Am I doing it correctly in my code below?

您的查询很好,但是 .TimeZone("+00:00") 不是必需的,因为它不会调整参数。但是,您确实需要确保文档中的时间戳以 UTC 格式存储。

  1. Is ES going to handle the complexity of different timezone of the datetime argument before comparing it to the document datetime values ?

只是为了方便调整参数。它不会调整文档中的值。

  1. Is ES going to return the documents with the original datetime timezone values ?

文件应正常返回。更改查询中的时区不会影响这一点。

  1. Any documentation unraveling the internals of elastic search instance timezone handling ?

除了您链接到的文档之外,我找不到太多内容。您应该通过实验来验证任何假设。

关于c# - 在 Elasticsearch 和嵌套中传递和比较具有不同时区的日期时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54716731/

相关文章:

pycharm - Pyspark 集成到 Pycharm 中

elasticsearch - 使用外部托管的Elasticsearch实例进行高级搜索

c# - 在 C# 中用 ffmpeg 覆盖两个音频文件

c# - 弹性查询以从mongodb数据C#中消除空格

python - 你能在不知道格式的情况下使用 datetime.strptime 吗?

java - 找出 YearMonth 实例的最后一天(在 LocalDate 中) - joda 库

java - 如何将 UTC 转换为日期时间?

c# - 如何在 C# 类库中使用 Ninject

c# - 更改 ToolStripSeparator 控件的 BackColor

performance - Elasticsearch应该在没有计算相关性的情况下进行查询(_score)