简体   繁体   中英

Elasticsearch Rails Persistence Model: how to update nested objects?

I'm starting with source docs such as the following:

"books" : [
  { 
    "title" : "book one", 
    "editor" : "me",
    "chapters" : [
      {
        "number" : "one",
        "author" : "first author"
      },
      {
        "number" : "two",
        "author" : "second author"
      }
    ]
  },
 ...
]

After importing my initial data, I want to add and index a field ('pdf' of type 'attachment') to each 'chapter'. 'pdf', as the name implies, is a pdf of the full chapter.

How do I set this up with ElasticSearch persistence model? Specifically, - the mapping, and - the updating

So there are two issues here, indexing the pdf attachment, and updating "child" objects. As far as the pdf goes, I encourage you to look at the documentation for attachment types . But once you have the plugin set up you should be able to update existing documents any way you otherwise would.

With the setup you listed, you will have to update the entire document to add updates to each child document. I'm assuming that's not what you want to do, so you probably want to use a parent/child relationship .

I'll give you a basic example using the object structure you listed.

I can set up an index with two mappings, a parent and a child, as follows:

PUT /test_index
{
   "mappings": {
      "book": {
         "properties": {
            "title": {
               "type": "string"
            },
            "editor": {
               "type": "string"
            }
         }
      },
      "chapter": {
         "_parent": {
            "type": "book"
         },
         "properties": {
            "number": {
               "type": "string"
            },
            "author": {
               "type": "string"
            }
         }
      }
   }
}

Then I can index some documents using the bulk API , like this:

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"}

Now if I want to update a child document without changing existing properties, I can use the update API and pass a partial document:

POST /test_index/chapter/2/_update?parent=1
{
   "doc": {
      "another_field": "just text for illustration"
   }
}

Notice that I passed the parent id in the request; this is so ES can route the request appropriately, as shown here .

I haven't tried indexing a pdf attachment, but it should work the same way once you get the plugin installed.

Here is the code I used to test:

http://sense.qbox.io/gist/c2f7b676e27798bed4d910de03b537fd9f15de2d

EDIT: I just realized I didn't say anything about doing this in Rails. I don't actually know much about that part. I'm confident there's a way to translate the REST requests I've shown here into Rails, but I don't know how to do it off-hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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