使用 PIT时间点ID 和 Search_After API -NEST ElasticSearch

问题描述

我一直在阅读有关 Point in time API内容,并希望在我的 .net application 中使用 nesT 来实现它。但是,在阅读那篇文章(.net 应用程序超链接)时,我看到了 Fluent DSL 示例,如下所示。有没有一种方法可以找到该 ID,而无需在控制台上访问 kibana 并进行查询搜索获取该 ID,然后将该 ID 放入“a-point-in-time-id”?或者“a-point-in-time-id”是否为您做这件事,就像映射到 ID 一样?

s => s
.PointInTime("a-point-in-time-id",p => p
.KeepAlive("1m"))

如果您这样做,我会在 kibana cli 控制台中知道:

POST /customer-simulation-es-app-logs*/_pit?keep_alive=5m 它会给你一个 PIT(时间点)ID。 你如何在 nesT 中检索它?

并且在阅读 search_after 并尝试使用 search after usage 使用 Fluent DSL 示例为 .net 客户端实现它时。我注意到他们有“项目”这个词,但没有说“项目​​在示例中。那到底是什么?

s => s
.sort(srt => srt
    .Descending(p => p.NumberOfCommits)
    .Descending(p => p.Name)
)
.SearchAfter(
    Project.First.NumberOfCommits,Project.First.Name
)

在这里,我尝试实现 .PointInTime().sort().SearchAfter(),但卡住了。

var response = await _elasticclient.SearchAsync<EsSource>(s => s
                  .Size(3000) // must see about this
                  .source(src => src.Includes(i => i
                                    .Fields(f => f.timestamp,fields => fields.messageTemplate,fields => fields.message)))
                  .Index("customer-simulation-es-app-logs*"
                  .Query(q => +q
                      .Daterange(dr => dr
                          .Field("@timestamp")
                              .GreaterThanorEquals("2021-06-12T16:39:32.727-05:00")
                              .LessthanorEquals(DateTime.Now))))
                  .PointInTime("",p => p
                                   .KeepAlive("5m"))
                  .sort(srt => srt
                               .Ascending(p => p.timestamp))
                  .SearchAfter()

我知道当您使用 PIT ID 时,您不需要搜索中的索引,但在超链接示例中,它没有显示您将如何实施该索引。所以对如何做到这一点有点迷茫。任何指示/指导/教程都会很棒!


在 CLI 中,当我运行 cmd POST /customer-simulation-es-app-logs*/_pit?keep_alive=1m 时,我得到一个 ID:

enter image description here

但只是想看看我如何在 nesT 中做到这一点,但如果你说它是 XPACK 的一部分,我会理解。

解决方法

我能够将 PIT 和 SearchAfter 与下面的代码示例结合起来:

var pit = string.IsNullOrEmpty(pitId) ? client.OpenPointInTime(new OpenPointInTimeDescriptor().KeepAlive(ServerDetail.TTL).Index(Indices.Index(Name))) : null;

            var request = new SearchRequest(Name)
            {
                SearchAfter = string.IsNullOrEmpty(pitId) ? null : new string[] { last },Size = ServerDetail.PageSize,PointInTime = new PointInTime(pit == null ? pitId : pit.Id)
            };
 List<FieldSort> sorts = new List<FieldSort>();
            foreach (var item in sortBy)
            {
                sorts.Add(new FieldSort() { Field = item.Field,Order = item.Direction == SortDirection.Asc ? SortOrder.Ascending : SortOrder.Descending });
            }
            request.Sort = sorts.ToArray();

您的 SearchAfter 值应该是您之前搜索结果的最后一个对象的排序字段中的值。

对于第一个搜索 pitId 为空,因此创建一个新的,对于后续请求,传递 pitId。

希望这对你有用吗?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...