在ElasticSearch文档列表中更新对象的属性? [英] Updating a property of an object in a list in ElasticSearch document?

查看:223
本文介绍了在ElasticSearch文档列表中更新对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ElasticSearch来说,我很新。我在.NET项目中使用它,我使用NEST客户端。现在我正在检查处理文件更新的方法。



我有一个如下所示的文档:

  public class Class1 
{
public string Prop1 {get;组; }
public string Prop2 {get;组; }
public List< Class2> PropList {get;组; }
}

当我想添加一些东西到PropList,我在做它由脚本:

  client.Update< Class1>(x => x 
.Id(1)
.Index(index_name)
.Script(ctx._source.propList + = prop)
.Params(p => p.Add(prop,newProp)) );

现在工作完美。问题是当我想更新propList中的对象的属性时。我现在正在做的是通过检索整个文档,查找列表中的对象,更新属性并再次对整个文档进行索引,这在某些时候可能会导致性能问题。



有更好的方法吗?可能使用脚本或其他方式?



谢谢。

解决方案

我不知道如何设置与巢,脱手,但我会去与一个父/子关系



作为玩具示例,我使用此映射设置了一个索引: p>

  PUT / test_index 
{
mappings:{
parent_type:{
property:{
num_prop:{
type:integer
},
str_prop:{
type string
}
}
},
child_type:{
_parent:{
type:parent_type

properties:{
child_num:{
type:integer
},
child_str:{
type:string
}
}
}
}
}

然后添加一些数据:

  POST / test_index / _bulk 
{ index:{_ type:parent_type,_ id:1}}
{num_prop:1,str_prop:hello}
{index _type:child_type,_ id:1,_ parent:1}}
{child_num:11,child_str:foo}
{index _type:child_type,_ id:2,_ parent:1}}
{child_num:12,child_str:bar}
{index _type:parent_type,_ id:2}}
{num_prop:2,str_prop:再见}
{index:{_ type:child_type ,_ id:3,_ parent:2}}
{child_num:21,child_str:baz}

现在,如果我想更新一个子文档,我可以发布一个新版本:

  POST / test_index / child_type / 2?parent = 1 
{
child_num:13,
child_str:bars
}

(请注意,我必须提供父ID,以便ES可以适当地路由请求) p>

如果我想要,我也可以执行部分​​,脚本更新:

  POST / test_index / child_type / 3 / _update?parent = 2 
{
script:ctx._source.child_num + = 1
}

我们可以通过搜索子类型证明这一点:

  POST / test_index / child_type / _search 
...
{
take:2,
timed_out:false,
_ $
total:5,
success:5,
failed:0
},
hits:{
total:3,
max_score:1,
hits:[
{
_index:test_index,
_type :child_type,
_id:1,
_score:1,
_source:{
child_num
child_str:foo
}
},
{
_index:test_index,
_type:child_type ,
_id:2,
_score:1,
_source:{
child_num:13,
child_str bar
}
},
{
_index:test_index,
_type:child_type,
_id :3,
_score:1,
_source:{
child_num:22,
child_str:baz
}
}
]
}
}

希望这有帮助。以下是我使用的代码,再加上几个例子:



http://sense.qbox.io/gist/73f6d2f347a08bfe0c254a977a4a05a68d2f3a8d


I'm fairly new to ElasticSearch. I'm using it in a .NET project and I'm using NEST client. Right now I'm examining ways of handling document updates.

I have a document that looks like this:

public class Class1
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public List<Class2> PropList { get; set; }
}

and when I want to add something to the PropList, I'm doing it by script:

client.Update<Class1>(x => x
            .Id(1)
            .Index("index_name")
            .Script("ctx._source.propList += prop")
            .Params(p => p.Add("prop", newProp)));

That works perfectly right now. The problem is when I want to update a property of an object inside propList. The way I'm doing it right now is by retrieving the entire document, finding the object in the list, update the property and index the entire document again, which at some point can result in performance issues.

Is there a way of doing this more efficiently? Maybe using scripts or some other way?

Thanks.

解决方案

I don't know how to set it up with nest, off-hand, but I would go with a parent/child relationship.

As a toy example, I set up an index with this mapping:

PUT /test_index
{
   "mappings": {
      "parent_type": {
         "properties": {
            "num_prop": {
               "type": "integer"
            },
            "str_prop": {
               "type": "string"
            }
         }
      },
      "child_type": {
         "_parent": {
            "type": "parent_type"
         },
         "properties": {
            "child_num": {
               "type": "integer"
            },
            "child_str": {
               "type": "string"
            }
         }
      }
   }
}

then added some data:

POST /test_index/_bulk
{"index":{"_type":"parent_type","_id":1}}
{"num_prop":1,"str_prop":"hello"}
{"index":{"_type":"child_type","_id":1,"_parent":1}}
{"child_num":11,"child_str":"foo"}
{"index":{"_type":"child_type","_id":2,"_parent":1}}
{"child_num":12,"child_str":"bar"}
{"index":{"_type":"parent_type","_id":2}}
{"num_prop":2,"str_prop":"goodbye"}
{"index":{"_type":"child_type","_id":3,"_parent":2}}
{"child_num":21,"child_str":"baz"}

Now if I want to update a child document I can just post a new version:

POST /test_index/child_type/2?parent=1
{
   "child_num": 13,
   "child_str": "bars"
}

(note that I have to provide the parent id so ES can route the request appropriately)

I can also do a partial, scripted update if I want to:

POST /test_index/child_type/3/_update?parent=2
{
   "script": "ctx._source.child_num+=1"
}

We can prove that this worked by searching the child types:

POST /test_index/child_type/_search
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "child_type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "child_num": 11,
               "child_str": "foo"
            }
         },
         {
            "_index": "test_index",
            "_type": "child_type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "child_num": 13,
               "child_str": "bars"
            }
         },
         {
            "_index": "test_index",
            "_type": "child_type",
            "_id": "3",
            "_score": 1,
            "_source": {
               "child_num": 22,
               "child_str": "baz"
            }
         }
      ]
   }
}

Hope this helps. Here is the code I used, plus a few more examples:

http://sense.qbox.io/gist/73f6d2f347a08bfe0c254a977a4a05a68d2f3a8d

这篇关于在ElasticSearch文档列表中更新对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆