简体   繁体   English

使用 NEST(ElasticSearch) 搜索嵌套数组

[英]Search on nested array using NEST(ElasticSearch)

I want to filter data in nested array using Nest.我想使用 Nest 过滤嵌套数组中的数据。 Given I have following classes:鉴于我有以下课程:

public class Package
{
    public IEnumerable<PackageItem> PackageItems { get; set; }
}

public class PackageItem
{
    public string Title { get; set; }
    public decimal Price { get; set; }
}

and I have index these data in ElasticSearch using NEST: Here is my data:我使用 NEST 在 ElasticSearch 中索引了这些数据:这是我的数据:

"hits" : [
  {
    "_index" : "packages",
    "_type" : "_doc",
    "_id" : "cf08b05c-c8b5-45cb-bca8-17267c3613fb",
    "_score" : 1.0,
    "_source" : {
      "id" : "cf08b05c-c8b5-45cb-bca8-17267c3613fb",
      "PackageItems" : [
        {
          "Title" : "some-title",
          "price" : 100000.0
        },
        {
          "Title" : "some-title",
          "price" : 200000.0
        }
      ]
    }
  },
  {
    "_index" : "packages",
    "_type" : "_doc",
    "_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
    "_score" : 1.0,
    "_source" : {
      "id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
      "PackageItems" : [
        {
          "Title" : "some-title",
          "price" : 100000.0
        },
        {
          "Title" : "some-title",
          "price" : 400000.0
        }
      ]
    }
  },
  {
    "_index" : "packages",
    "_type" : "_doc",
    "_id" : "cd5d3587-838b-46ec-babc-d756c5587396",
    "_score" : 1.0,
    "_source" : {
      "id" : "cd5d3587-838b-46ec-babc-d756c5587396",
      "PackageItems" : [
        {
          "Title" : "some-title",
          "price" : 300000.0
        },
        {
          "Title" : "some-title",
          "price" : 500000.0
        }
      ]
    }
  }
]

I want to filter packages with minimumPackageItemPrice:100000 and maximumPackageItemPrice:400000.我想过滤具有 minimumPackageItemPrice:100000 和 maximumPackageItemPrice:400000 的包。 What query should I execute using NEST?我应该使用 NEST 执行什么查询?

The result should be like this:结果应该是这样的:

"hits" : [
  {
    "_index" : "packages",
    "_type" : "_doc",
    "_id" : "cf08b05c-c8b5-45cb-bca8-17267c3613fb",
    "_score" : 1.0,
    "_source" : {
      "id" : "cf08b05c-c8b5-45cb-bca8-17267c3613fb",
      "PackageItems" : [
        {
          "Title" : "some-title",
          "price" : 100000.0
        },
        {
          "Title" : "some-title",
          "price" : 200000.0
        }
      ]
    }
  },
  {
    "_index" : "inventories",
    "_type" : "_doc",
    "_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
    "_score" : 1.0,
    "_source" : {
      "id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
      "PackageItems" : [
        {
          "Title" : "some-title",
          "price" : 100000.0
        },
        {
          "Title" : "some-title",
          "price" : 400000.0
        }
      ]
    }
  }
]

To trigger range queries on Elasticsearch through NEST , you can make use of the range query.要通过NEST触发 Elasticsearch 上的范围查询,您可以使用范围查询。 The approach is that you can construct a range query for your scenario and trigger that query through the NEST client's search method.方法是您可以为您的场景构建一个范围查询,并通过NEST客户端的搜索方法触发该查询。

Generating the range query for your case为您的案例生成范围查询

// This method builds the range query where price should be between 
// 100000.0 and 400000.0 (inclusive range in this case)
private static QueryContainer BuildNestedRangeQuery()
{
    return new QueryContainerDescriptor<Package>()
        .Nested(n =>
            n.Path(p => p.PackageItems)
             .Query(q => q
                .Range(r => r
                    .Field(f => f.PackageItems.FirstOrDefault().Price)
                    .GreaterThanOrEquals(100000.0)
                    .LessThanOrEquals(400000.0))
                )
             )
        )
}

Triggering the search触发搜索

Search can be triggered through the NEST client as shown below.搜索可以通过NEST客户端触发,如下所示。 This may vary according to your own implementation.这可能会根据您自己的实现而有所不同。 However, the above range query remains the same.但是,上述范围查询保持不变。

// Replace T with type of your choice and client is NEST client
var result = client.Search<T>(    
    .From(0)
    .Size(20)
    .Query(q => BuildNestedRangeQuery())
    // other methods that you want to chain go here
)

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

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