如何使用NEST为ElasticSearchMap范围过滤器中的时间戳参数?

6xfqseft  于 4个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(59)

尝试使用时间戳字段按范围筛选数据。查询如下:

var scanResults = await _elasticClient.SearchAsync<OemCatalogModel>(s => s.Index(indexName)
                .Source(sf => sf
                    .Includes(i => i
                        .Fields(
                                    f => f.Event,
                                    f => f.MemberId,
                                    f => f.IsInternalUser,
                                    f => f.IndexName,
                                    f => f.IsMobile,
                                    f => f.VinNumber,
                                    f => f.Timestamp
                        )
                    )
                )
                .Query(q => q.Range(p => p.Field<Timestamp>(t=>t.Timestamp)
                             .GreaterThanOrEquals(sd)
                             .LessThanOrEquals(ed)
                    ))
                .Size(10000).Scroll("60s"));

字符串
从DatePicker获取startDateendDate作为DateTimeOffset,并像下面这样初始化sded

var sd = startDate.Value.ToUnixTimeSeconds();
        var ed = endDate.Value.ToUnixTimeSeconds();


时间戳字段在Map模型中看起来如下

[Date(Name = "timestamp")]
    public Int64 Timestamp { get; set; }


此查询引发“parse_exception”异常:

"reason" : {
      "type" : "parse_exception",
      "reason" : "failed to parse date field [1.6540308E9] with format [epoch_second]: [failed to parse date field [1.6540308E9] with format [epoch_second]]",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "failed to parse date field [1.6540308E9] with format [epoch_second]",
        "caused_by" : {
          "type" : "date_time_parse_exception",
          "reason" : "date_time_parse_exception: Failed to parse with all enclosed parsers"
        }
      }
    }


NEST生成的ElasticSearch查询如下:
{“查询”:{“range”:{“timestamp”:{“gte”:1654030800.0,“lte”:1654203599.0}}},“size”:10000,"_source”:{“includes”:[“event”,“member_id”,“is_internal_user”,“indexName”,“is_移动的”,“vin_number”,“timestamp”]}}
这个过滤器不工作,因为范围参数添加.0.我检查了这个witout .0和它的作品.例如结果为_source看起来像:

"_source" : {
      "member_id" : 69500,
      "is_mobile" : false,
      "event" : "close_unit_card",
      "is_internal_user" : false,
      "timestamp" : 1654066236
    }


如何正确Map范围过滤器与时间戳?可能我必须使用DataRange?如果是,如何将sded转换为ElasticSearch中使用的时间戳?

也尝试执行以下操作,但得到不同的异常:

查询类似于:

.Query(q => q.DateRange(p => p.Field(t=>t.Timestamp)
.GreaterThanOrEquals(DateTime.Parse(startDate.Value.ToString()))
.LessThanOrEquals(DateTime.Parse(endDate.Value.ToString()))))


以下例外情况上涨:

"reason" : {
          "type" : "parse_exception",
          "reason" : "failed to parse date field [2022-05-31T21:00:00Z] with format [epoch_second]: [failed to parse date field [2022-05-31T21:00:00Z] with format [epoch_second]]",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "failed to parse date field [2022-05-31T21:00:00Z] with format [epoch_second]",
            "caused_by" : {
              "type" : "date_time_parse_exception",
              "reason" : "date_time_parse_exception: Failed to parse with all enclosed parsers"
            }
          }
        }

b1payxdu

b1payxdu1#

奇怪的解决方案,但工作:
初始化开始和结束日期:

var sd = startDate.Value.ToUnixTimeSeconds();
    var ed = endDate.Value.ToUnixTimeSeconds();

字符串
DateRange看起来像:

.Query(q => q.DateRange(p => p.Field(t=>t.Timestamp).Format("epoch_second")
                                 .GreaterThanOrEquals(sd)
                                 .LessThanOrEquals(ed)
                                 ))


模型看起来像:

[Date(Format = DateFormat.epoch_second, Name = "timestamp")]
    public DateTime Timestamp { get; set; }

31moq8wy

31moq8wy2#

通过DateRange,您可以使用DateTime结构:

var searchResponse = client.Search<_Source>(s => s
    .Query(q => q
        .Bool(b => b
            .Filter(fi => fi
                .DateRange(r => r
                    .Field(f => f.timestamp) // Replace with your actual timestamp field
                    .GreaterThanOrEquals(new DateTime(2023, 11, 17, 12, 05, 0)) // Start time
                    .LessThanOrEquals(new DateTime(2023, 11, 17, 12, 10, 0)) // End time
                )
            )
            .Must(fi => fi
                .Term(t => t
                    .Field(f => f.host.name) // Replace with your actual host name field
                    .Value("evp-lwsj101")
                )
            )
         )
    )
    .From(0)
    .Size(100) // adjust the size based on your needs
);

字符串

相关问题