Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"[SyntaxError: Unexpected token <]" when I try to add documents to my Solr index with my Node/Express app

I'm trying to integrate Solr into a Node/Express app and while I am using this Node package for the Solr integration. I am able to properly query the index, but following the instructions on the package's GitHub, I am unable to add documents to the index. I keep getting this error:

[SyntaxError: Unexpected token <]

On the Solr console, the error is a little more detailed:

SEVERE: Error processing "legacy" update command:com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '[' (code 91) in prolog; expected '<'

Here is a small test app I'm using to try and get this working:

// set variables for environment
var express = require('express');
var path = require('path');
var solr = require('solr-client');

// Create the Express app
var app = express();
module.exports = app;

// Initialize the Vash view engine
app.set("views", path.join( __dirname, "/views") );
app.set("view engine", "vash");

// set routes
app.get('/', function(req, res) {
    // Create a client
    var client = solr.createClient();

    var query = 'q=fencing';
    client.get('select', query, function(err, obj) {
        if (err) {
            console.log(err);
        } else {
            console.log(obj);
        }
    });

    // Switch on "auto commit", by default `client.autoCommit = false`
    client.autoCommit = true;

    var docs = [];
    for(var i = 0; i <= 10 ; i++){
        var doc = {
            id : 12345 + i,
            title_t : "Title "+ i,
            description_t : "Text"+ i + "Alice"
        };
        docs.push(doc);
    }

    // Add documents
    client.add(docs,function(err,obj){
        if(err){
            console.log(err);
        }else{
            console.log(obj);
        }
    });

    res.render('index');
});

// Set server port
app.listen(4000);
console.log('server is running on localhost:4000');

The specific part that doesn't work is where I attempt to add a document to the index here:

// Add documents
client.add(docs,function(err,obj){
    if(err){
        console.log(err);
    }else{
        console.log(obj);
    }
});

My hunch is that this has something to do with receiving JSON when it's expecting XML, but I cannot for the life of me figure out how to either send XML or to enable Solr to accept JSON. Any advice?

like image 670
Captain Stack Avatar asked Oct 18 '22 06:10

Captain Stack


1 Answers

I have seen a little bit the code of the solr-node-client and I see that in order to add a document they use this function Client.prototype.add and this function finally uses this handler

this.UPDATE_JSON_HANDLER = 
(versionUtils.version(this.options.solrVersion) >= versionUtils.Solr4_0) 
? 'update' : 'update/json';

What this means is that if you use Solr version greater or equal to 4 then in the Solrconfig.xml file there exists a request handler named update.

  <requestHandler name="/update" class="solr.UpdateRequestHandler">

  </requestHandler>

A single unified update request handler supports XML, CSV, JSON, and javabin update requests, delegating to the appropriate ContentStreamLoader based on the Content-Type of the ContentStream.

Have a look here https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers#UploadingDatawithIndexHandlers-JSONFormattedIndexUpdates

The code in Node package sends json, so Solr would delegate to the appropriate ContentStreamLoader. This is the code in Node package

'content-type' : 'application/json; charset=utf-8',

If you use Solr less than version 4 then in your solrconfig.xml you should have a request handler like this

<requestHandler name="/update/json" class="solr.JsonUpdateRequestHandler">
        <lst name="defaults">
         <str name="stream.contentType">application/json</str>
       </lst>
  </requestHandler> 

So, to make a long story short, make sure that if you use Solr >=4 then in solrconfig.xml you have to have an update request handler

 <requestHandler name="/update" class="solr.UpdateRequestHandler">
      </requestHandler>

and if you use Solr < 4 then make sure you have a update handler like this one

<requestHandler name="/update/json" class="solr.JsonUpdateRequestHandler">
            <lst name="defaults">
             <str name="stream.contentType">application/json</str>
           </lst>
      </requestHandler> 

Hope this helps

like image 87
Periklis Douvitsas Avatar answered Oct 22 '22 02:10

Periklis Douvitsas