2

I'm using node and mongoose to update a watson rank and retrieve database. I'm trying to add documents to the collection. I can add a single document. However, the json object I'm creating for multiple is failing. I've seen this answer here, but it was for curl. I was wondering if someone can help me with the JS part.

    // add a document
    //var doc = { id : 1234, title_t : 'Hello', text_field_s: 'some text' };
    var data = [];

    for (var count = 0; count < documents.length ; count++){
        data.push({"add":{"doc":{id : documents[count]._id, problem : documents[count].problem, description : documents[count].description, resolution : documents[count].resolution}}})
    }

    console.log(data);

    solrClient.add(data, function(err) {
      if(err) {
        console.log('Error indexing document: ' + err);
      } else {
        console.log('Indexed a document.');
        solrClient.commit(function(err) {
          if(err) {
            console.log('Error committing change: ' + err);
          } else {
            console.log('Successfully commited changes.');
            callback(documents);
          }
        });
      }
    });

I get this:

[ { add: { doc: [Object] } },
  { add: { doc: [Object] } },
  { add: { doc: [Object] } },
  { add: { doc: [Object] } },
  { add: { doc: [Object] } },
  { add: { doc: [Object] } } ]

However, I think I need this: [ add: {doc: [Object] }, add : {doc: [Object] }, ...

When I try to remove the { in front of the add, I get an error (missing ) after argument list) and it won't run.

user269964
  • 159
  • 11
  • Well, `[ add: { doc: ...` wouldn't be valid anything. I don't know the API; does it want a collection of `{ add: { doc: ...}}` or does it want a collection to add like `{ add: [ { doc: ...}, { doc: ...} ]}`? What do the docs say? – Dave Newton Aug 31 '16 at 13:49
  • The example has: { "add" : { "doc" : { "id" : 1, "author" : "brenckman,m.", "bibliography" : "j. ae. scs. 25, 1958, 324.", "body" : "exnt .", "title" : "experimental investigation of the aerodynamics of a wing in a slipstream ." } }, "add" : { "doc" : { "id" : 2, "author" : "ting-yili", "bibliography" : "department of aeronautical engineering, rensselaer polytechnic institute troy, n.y.", "body" : "simple flow .", "title" : "simple s incompressible fluid ." } }, – user269964 Aug 31 '16 at 15:13

3 Answers3

0

Looking at the test code, I think that you need to use a simple array of document objects:

var data = [];

for (var count = 0; count < documents.length ; count++){
  data.push({ id : documents[count]._id, problem : documents[count].problem, description : documents[count].description, resolution : documents[count].resolution})
}
robertklep
  • 198,204
  • 35
  • 394
  • 381
0
[ 
    add: {doc: [Object] },
    add : {doc: [Object] },
    //...
    add : {doc: [Object] }
]

is a associative array, which is translated in JS as a object.
And a object can't have multiple property named the same.

What you probably need however, is a simple arra:

[
    {doc: [Object] },
    {doc: [Object] },
    //...
    {doc: [Object] }
]
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
0

[ add: {doc: [Object] }, add : {doc: [Object] }, ... is invalid because the keys are not unique.

And your code is correct I think. Just change like this callback(null, documents);

I think callback is for your upper method.

So you don't have callback in you question sample code.

You do not need to call commit. Just adding is enough.

solrClient.add(data, function(err, added) {
      if(err) {
        console.log('Error indexing document: ' + err);
      } else {
        console.log('Indexed a document.');  
        console.log(added);      
      }
    });
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • I made the change, but I still get the same error. Also, the keys I'm getting are from a Mongoose database, so they are unique. – user269964 Aug 31 '16 at 16:05