Skip to content

Flattening/Resolving object references #264

@johnnncodes

Description

@johnnncodes

Is it possible to flatten or resolve references to other collections before indexing to Elasticsearch?

Example I have this schema:

var PartSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  province : {
    type : mongoose.Schema.ObjectId,
    ref : 'Province',
    required: true
  },
});

Using mongoriver, the province property is being indexed as an ObjectId so in the search results of my application, the province is an object id so it's not useful to the users so I wanted to flatten/resolve the province property so I can access the properties of province like: part.province.name, part.province.createdAt etc...

My initial solution was to use script filters and mappings. Here's what I did:

  1. 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"}
    }
}}}'
  1. 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"
  }
}'
  1. Created a script
ctx.document.province = {};
ctx.document.province.name = 'Static name to be inserted by script';

It works, but currently the name on the script is just static. Obviously I need to fetch it dynamically from mongodb database but I think it is not possible to use elasticsearch lang-javascript to query from mongodb?

Any ideas on how to solve my problem? Or maybe there are other ways to flatten/resolve object references before indexing to ES using mongoriver? Any suggestions will be greatly appreciated.

Thanks in advance :)

EDIT:
I'm thinking of using lang-javascript and communicate with MongoDB via a REST api using ajax but not sure if this is an efficient solution.

EDIT:
Tried using ajax, unfortunately it doesn't work since I think it's not possible to use ajax outside the browser.

Related problem: https://groups.google.com/forum/#!topic/elasticsearch/e3CelbOkgWk

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions