Elasticsearch Nest Client - 搜索嵌套属性

Elasticsearch Nest Client - searching nested properties

我很难找到有关如何使用 C# 中的 Nest 客户端搜索嵌套属性的信息。

我在索引中有电子邮件对象,形状大致如下:

    {
      subject: “This is a test”,
      content: “This is an email written as a test of the Elasticsearch system.  Thanks, Mr Tom Jones”,
      custodians: [
        {
          firstName: “Tom”,
          lastName: “Jones”,
          routeType: 0
        },
        {
          firstName: “Matthew”,
          lastName: “Billsley”,
          routeType: 1
        }
      ]
    }

你应该能看到里面有一个名为“custodians”的数组,它是邮件所有发件人和收件人的列表。在 .Net 的 Fluent 风格查询构建器中,当我使用主题、内容和其他“第一层”属性时,我可以很好地构建查询。但我可能只想在某些查询中包含 routeType = 0 的保管人。我似乎找不到有关如何完成此操作的任何指导。有什么想法吗?

例如,在主题字段中查询术语“野餐”将如下所示:

Client.SearchAsync(m => m
  .Query(q => q
    .Match(f => f
      .Field(msg => msg.Subject)
      .Query(“picnic”))));

仅从 routeType = 0 且 lastName = “Jones” 的索引中获取消息的查询是什么?

仅供参考:这是交叉发布到 Elasticsearch 论坛的。如果我在那里有好的建议,我会在这里添加。

如果您想要接收具有 routeType == 0 保管人的邮件:

Client.SearchAsync(m => m
  .Query(q => q
    .Term(t => t
      .Field(msg => msg.Custodians[0].RouteType)
      .Value(0))));

如果您想要接收具有 lastName == "jones" 管理员的消息:

Client.SearchAsync(m => m
  .Query(q => q
    .Term(t => t
      .Field(msg => msg.Custodians[0].LastName)
      .Value("jones"))));

如果您想要接收具有 lastName == "jones" AND routeType == 0 保管人的邮件:

Client.SearchAsync(m => m
  .Query(q => q
    .Nested(t => t
      .Path(msg => msg.Custodians)
      .Query(nq =>
        nq.Term(t => t.Field(msg => msg.Custodians[0].RouteType).Value(0) &&
        ng.Term(t => t.Field(msg => msg.Custodians[0].LastName).Value("jones")
      )
    )
  )
);

请注意,custodians 需要映射为嵌套字段,最后一个查询才能按预期工作。有关嵌套字段的更多信息,请参阅 here