简体   繁体   English

Elasticsearch Nest Client - 搜索嵌套属性

[英]Elasticsearch Nest Client - searching nested properties

I'm having a tough time finding information on how to search nested properties using the Nest client in C#.我很难找到有关如何使用 C# 中的 Nest 客户端搜索嵌套属性的信息。

I have email objects in an index with approximately this shape:我在索引中的电子邮件对象大致具有以下形状:

    {
      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
        }
      ]
    }

You should be able to see that there is an array in there called “custodians” which is a list of all the senders and recipients of the email.您应该能够看到其中有一个名为“custodians”的数组,它是电子邮件的所有发件人和收件人的列表。 In the Fluent-style query builder in .Net I can build the query just fine when I'm using subject, content, and other “first tier” properties.在 .Net 中的 Fluent 样式查询构建器中,当我使用主题、内容和其他“第一层”属性时,我可以很好地构建查询。 But I may only want to include custodians who have the routeType = 0 in some queries.但我可能只想在某些查询中包含 routeType = 0 的保管人。 I can't seem to find any guidance on how to accomplish this.我似乎无法找到有关如何实现这一点的任何指导。 Any ideas?有任何想法吗?

For instance, a query for the term “picnic” in the subject field would look like:例如,在主题字段中查询“野餐”一词将如下所示:

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

What would the query to only get messages from the index with routeType = 0 and lastName = “Jones” be?仅从具有 routeType = 0 和 lastName = “Jones” 的索引获取消息的查询是什么?

FYI: This is crossposted to the Elasticsearch forums.仅供参考:这是交叉发布到 Elasticsearch 论坛的。 If I get a good suggestion there, I will add it here.如果我在那里得到好的建议,我会在这里添加。

If you want to get messages that have a custodian with routeType == 0 :如果要获取具有routeType == 0保管人的消息:

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

If you want to get messages that have a custodian with lastName == "jones" :如果您想获取具有lastName == "jones"保管人的消息:

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

If you want to get messages that have a custodian with lastName == "jones" AND routeType == 0 :如果您想获取具有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")
      )
    )
  )
);

Note that custodians will need to be mapped as a nested field for the last query to work as expected.请注意,需要将custodians映射为嵌套字段,以便最后一个查询按预期工作。 See here for more about nested fields.有关嵌套字段的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM