Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flattening/Resolving object references #264

Open
johnnncodes opened this issue May 1, 2014 · 1 comment
Open

Flattening/Resolving object references #264

johnnncodes opened this issue May 1, 2014 · 1 comment

Comments

@johnnncodes
Copy link

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

@mcleae
Copy link

mcleae commented Apr 28, 2015

would this help
#525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants