Can I flatten or resolve references from other collections before indexing into Elasticsearch?
For instance, consider this schema:
var PartSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
province : {
type : mongoose.Schema.ObjectId,
ref : 'Province',
required: true
},
});
When using mongoriver, the "province" property gets indexed as an ObjectId. This results in the search results displaying the province as an object id, which is not user-friendly. To make it more accessible by users, I need to flat/resolve the province property so that I can access its properties like part.province.name, part.province.createdAt, etc.
My initial approach involved script filters and mappings. Here's what I did:
First, I defined a mapping on ES:
curl -XPUT 'http://localhost:9200/parts/part/_mapping' -d '{"properties":{"__v":{"type":"long"},
"title":{"type":"string"},
"province":{
"type":"nested",
"properties": {
name : {"type": "string"}
}
}}}'
Then, I created a river:
curl -XPUT "localhost:9200/_river/pdm/_meta" -d '
{
"type": "mongodb",
"mongodb": {
"servers": [
{ "host": "localhost", "port": 27017 }
],
"db": "pdm",
"collection": "parts"
},
"index": {
"name": "parts",
"type": "part"
}
}'
Lastly, I developed a script:
ctx.document.province = {};
ctx.document.province.name = 'Static name to be inserted by script';
This solution works, but currently, the name in the script is static. I need to dynamically fetch it from the MongoDB database. My attempt to do this via a REST API using AJAX with lang-javascript failed due to limitations outside the browser. Even if successful, I'm unsure of its efficiency.
Do you have any suggestions on how to address this issue? Or are there alternative methods to flatten/reference object references before indexing into ES using mongoriver? Any insights would be appreciated.
Thanks in advance :)
Note: I also need to automatically update object references in previously indexed documents when modified.
Related issue: https://groups.google.com/forum/#!topic/elasticsearch/e3CelbOkgWk