alanning / meteor-elasticsearch

Wraps the ElasticSearch NPM package and provides helper functions.
MIT License
12 stars 2 forks source link

Tried updating ES in hook, internal server error #1

Open jonlachlan opened 8 years ago

jonlachlan commented 8 years ago

Hi there, I'm working on integrating elasticsearch using this library and the example (https://github.com/Meteor-NY/devshop-elasticsearch). I have a hook that looks like this:

VolunteerProfiles.after.insert((userId, doc) => {
  console.log("[VolunteerProfiles.insert.hook]", doc)
  ES.insert(doc, {fieldsToInclude: FIELDS_TO_INCLUDE});

  console.log("[VolunteerProfiles.insert.hook] ...Adding record to ElasticSearch");
});

However when I insert a new doc, e.g. VolunteerProfiles.insert({interests: "Bike jumping, skydiving, political research, Javascript programming"}) I have the following errors in Meteor:

[VolunteerProfiles.insert.hook] { interests: 'Bike jumping, skydiving, political research, Javascript programming', _id: 'DaFmuaNKFqtEzrnKy' }
Exception while invoking method '/volunteer-profiles/insert' Error: [mapper_parsing_exception] failed to parse

And here's what it says in the ElasticSearch log:

[DEBUG][action.index             ] [Clea] failed to execute [index {[volunteers][profile][DaFmuaNKFqtEzrnKy], source[{"interests":"Bike jumping, skydiving, political research, Javascript programming","_id":"DaFmuaNKFqtEzrnKy"}]}] on [[volunteers][1]]
[...]
Caused by: java.lang.IllegalStateException: Mixing up field types: class org.elasticsearch.index.mapper.core.StringFieldMapper$StringFieldType != class org.elasticsearch.index.mapper.internal.IdFieldMapper$IdFieldType on field _id

Unfortunately I'm a bit stumped by this. It looks like it has to do with the _id mapping and the dynamic type detection of Elastic.

Olabayo commented 7 years ago

Hi was able to resolve this by manually modifying the packages script to remove the doc._id field, where you have this

alanning_elasticsearch.js file in package folder Before:

// create a document in Elastic Search self.EsClient.index({ index: config.index, type: config.type, id: doc._id, body: doc });

After:

doc.listing_id = doc._id.toString();
delete doc._id;

// create a document in Elastic Search
self.EsClient.index({
  index: config.index,
  type: config.type,
  id: doc.listing_id,
  body: doc
}); 

Just troubleshooting so the proper thing will be to modify the package so that in insert or update into elastic search, it renames the field _id into another field when saving the body (Elasticsearch throws this error because it uses a field called _id internally as well)