I currently have an issue with elasticsearch, when I try to index some array of tags wich I extracted from a MySQL database with node.js and sails. With the following mapping, it only index the first element of the array and not the entire thing ... no clue why.
I already tried with "nested" type mapping and with a "object" type too.
Am I missing something in the mapping of the "tags" object or is it the loop that build my array ? or something else ...
thanks by advance for any help !
var tagsTab = [];
async.series([
function(CALLBACK)
{
pool.getConnection(function (err, conn1)
{
for (var i in tags)
{
conn1.query("select * from XXXXX where TAGID = ?", [tags[i].TAGID], function (err, values)
{
if (err)
{
sails.log.error(err);
}
else
{
tagsTab.push({id:values[0].TAGID, name:values[0].TAG});
}
if(i == tags.length-1)
{
// Doesn't WORK
jsonPost.tags = tagsTab;
console.log(jsonPost.tags);
// Console.log result => [{ id: 98, name: 'XXXXX' }, { id: 98, name: 'YYYYY' }]
// It WORKS !
jsonPost.tags = [{ id: 99, name: 'XXXXX' },{ id: 98, name: 'YYYYY' }];
console.log(jsonPost.tags);
// Console.log result => [{ id: 13, name: 'XXXXX' }, { id: 12, name: 'YYYYY' }]
conn1.release();
CALLBACK();
}
});
}
});
},
function(CALLBACK)
{
//-- Send to es
ESService.setIndex('XXXXX');
ESService.index('source', jsonPost.id, jsonPost, function (err, res)
{
if (err)
{
sails.log.error(err);
sails.log.error('Failed to index '+jsonPost.id);
}
CALLBACK();
});
}
], function (err) {
if (err)
{
sails.log.error('Error while indexing '.jsonPost.id+' : '+err);
}
else
{
sails.log.info('Indexing done for media '+jsonPost.id);
}
});
MAPPING :
"tags":{
"properties":{
"id":{"type":"long"},
"name":{"type":"string"}
}
}