Elasticsearch Nest Client-搜索嵌套属性

时间:2019-04-17 17:44:37

标签: c# elasticsearch nest fluent

我很难找到有关如何使用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
        }
      ]
    }

您应该能够看到其中有一个称为“保管人”的数组,该数组是电子邮件的所有发件人和收件人的列表。在.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论坛的。如果在那里有好的建议,我会在这里添加。

1 个答案:

答案 0 :(得分:2)

如果您想获得托管人为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"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

相关问题