![](/img/trans.png)
[英]How to set up a elasticsearch facet search on a nested rails model
[英]Elasticsearch Rails Persistence Model: how to update nested objects?
我从下面的源文档开始:
"books" : [
{
"title" : "book one",
"editor" : "me",
"chapters" : [
{
"number" : "one",
"author" : "first author"
},
{
"number" : "two",
"author" : "second author"
}
]
},
...
]
导入初始数据后,我想为每个“章节”添加一个字段(“附件”类型的“ pdf”)并建立索引。 顾名思义,“ pdf”是整章的pdf。
如何使用ElasticSearch持久性模型进行设置? 具体来说,-映射和-更新
因此,这里有两个问题,为pdf附件建立索引,并更新“子”对象。 就pdf而言,我鼓励您查看附件类型的文档。 但是,一旦设置好插件,您就应该能够以其他方式更新现有文档。
使用列出的设置,您将必须更新整个文档以向每个子文档添加更新。 我假设这不是您想要做的,所以您可能想使用父子关系 。
我将使用列出的对象结构为您提供一个基本示例。
我可以使用两个映射(父映射和子映射)设置索引,如下所示:
PUT /test_index
{
"mappings": {
"book": {
"properties": {
"title": {
"type": "string"
},
"editor": {
"type": "string"
}
}
},
"chapter": {
"_parent": {
"type": "book"
},
"properties": {
"number": {
"type": "string"
},
"author": {
"type": "string"
}
}
}
}
}
然后,我可以使用批量API为某些文档建立索引,如下所示:
POST /test_index/_bulk
{"index":{"_type":"book","_id":1}}
{"title":"book one","editor" : "me"}
{"index":{"_type":"chapter","_id":1,"_parent":1}}
{"number":"one","author":"first author"}
{"index":{"_type":"chapter","_id":2,"_parent":1}}
{"number":"two","author":"second author"}
现在,如果我想在不更改现有属性的情况下更新子文档,则可以使用update API并传递部分文档:
POST /test_index/chapter/2/_update?parent=1
{
"doc": {
"another_field": "just text for illustration"
}
}
注意,我在请求中传递了父ID。 这是如此ES可以路由该请求适当地,如图所示在这里 。
我没有尝试索引pdf附件,但是一旦安装了插件,它应该以相同的方式工作。
这是我用来测试的代码:
http://sense.qbox.io/gist/c2f7b676e27798bed4d910de03b537fd9f15de2d
编辑:我只是意识到我没有对在Rails中进行任何说明。 我实际上对该部分并不了解。 我相信有一种方法可以将我在这里显示的REST请求转换为Rails,但是我不知道该如何立即使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.