【问题标题】:Elasticsearch Nest Client - searching nested propertiesElasticsearch Nest Client - 搜索嵌套属性
【发布时间】:2019-09-08 00:57:18
【问题描述】:

我很难找到有关如何在 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 论坛的。如果我在那里得到好的建议,我会在这里添加。

【问题讨论】:

    标签: c# elasticsearch nest fluent


    【解决方案1】:

    如果您想获取具有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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多