c# - 如何使用Nest Elasticsearch更新嵌套对象?

标签 c# elasticsearch nest

我有产品索引,为简单起见,它具有两个字段Id和ProductAttributes作为嵌套对象,定义如下:

public class ProductType
{
    public Guid Id { get; set; }

    public List<ProductAttribute> ProductAttributes { get; set; }
 }

public class ProductAttribute
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public string Value { get; set; }
}

以及以下映射:
elasticClient.CreateIndex("product", i => i
       .Settings(s => s
                 .NumberOfShards(2)
                 .NumberOfReplicas(0)
                 )
                 .Mappings(m => m
                   .Map<ProductType>(map => map
                         .AutoMap()
                         .Properties(p => p
                          .Nested<ProductAttribute>(n => n
                            .Name(c => c.ProductAttributes)
                            .AutoMap()
                            .Properties(nc => nc
                               .Keyword(t => t
                                   .Name(nn => nn.Name)
                                   )
                              .Keyword(t => t
                                .Name(nn => nn.Value)
                             )
                  )
             )

现在,我正在尝试更新嵌套对象内的名称字段,并尝试使用脚本更新来实现该目标,如下所示:
        var scriptParams = new Dictionary<string, object>
                            {
                                {"name", "new name"}
                            };

        var result = elasticClient.UpdateByQuery<ProductType>(u => u
                              .Script(sn => sn
                                   .Inline(
                                          $"ctx._source.productAttributes= params.name;" 
                                      )
                                  .Params(scriptParams)
                              )
                              .Conflicts(Conflicts.Proceed)
                              .Refresh(true)
                          );

但是使用上述查询,我​​无法更新嵌套对象,能否请您告诉我如何使用嵌套ES使用 _update_by_query api更新嵌套对象?

最佳答案

最终,我发现了如何仅根据特定的嵌套对象的id更新名称属性,如下所示:

var result = elasticClient.UpdateByQuery<ProductType>(u => u
                  .Query(q => q
                        .Nested(n => n
                          .Path(Infer.Field<ProductType>(ff => ff.ProductAttributes))
                          .Query(nq => nq
                              .Term(Infer.Field<ProductType>(ff => ff.ProductAttributes.First().Id), productAttributeId)
                          )
                        )
                  )
                  .Script(ss => ss.Inline("if (ctx._source.productAttributes != null){for (item in ctx._source.productAttributes){if (item.id == params.id) {item.name = params.name;}}}")
                     .Params(new Dictionary<string, object>()
                     {
                         {"id", productAttributeId},
                         {"name", productAttributeName}
                     }).Lang("painless")
                  )
                  .Conflicts(Conflicts.Proceed)
                  .Refresh(true)
              );

这是生成的查询:
 POST product/producttype/_update_by_query?conflicts=proceed&refresh=true 
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "term": {
                "productAttributes.id": {
                  "value": "563243f0-8fbb-4adf-a78d-1339e5971a43"
                }
              }
            },
            "path": "productAttributes"
          }
        }
      ]
    }
  },
  "script": {
    "params": {
        "id":"563243f0-8fbb-4adf-a78d-1339e5971a43",
        "name": "CPU"
    },
    "lang": "painless",
    "inline": "if (ctx._source.productAttributes != null){for (item in ctx._source.productAttributes){if (item.id == params.id) {item.name = params.name;}}}"
  }
}

那么上面的查询是做什么的:

它首先搜索具有productAttribute且具有 563243f0-8fbb-4adf-a78d-1339e5971a43 id的产品,然后遍历productAttributes嵌套对象以仅更新具有该ID的属性,然后再次为文档重新索引。

我希望我的回答能帮助其他人在更新Elasticsearch中的嵌套对象时遇到问题。

关于c# - 如何使用Nest Elasticsearch更新嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54466057/

相关文章:

c# - 选择具有特定数量的嵌套文档项目的文档

c# - Unity 3D - 没有旋转轴的旋转游戏对象

c# - 属性最早出现在哪种语言中

elasticsearch - 在服务器之间移动 Elastic Search 数据的最简单方法是什么

sorting - Elasticsearch基于多个字段的排序

ElasticSearch 按字符串长度排序

c# - 使用 Elasticsearch .NET 和 NEST 6.x : How to MultiGet documents from multiples indices

c# - 如何使用 Autofac 注入(inject) asp.net mvc3 自定义成员提供程序?

c# - Windows 文件安全,删除访问规则

spring-boot - java中可用于 Elasticsearch 的不同客户端有哪些?