"[[SyntaxError:意外令牌<]""当我尝试通过Node/Express应用将文档添加到Solr索引中时 [英] "[SyntaxError: Unexpected token <]" when I try to add documents to my Solr index with my Node/Express app
问题描述
我正在尝试将Solr集成到Node/Express应用程序中,而我正在使用此用于Solr集成的节点程序包.我能够正确查询索引,但是按照软件包GitHub上的说明,我无法将文档添加到索引.我不断收到此错误:
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 <]
在Solr控制台上,该错误更为详细:
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);
}
});
我的直觉是,这与期望XML时接收JSON有关,但是我一生无法弄清楚如何发送XML或使Solr接受JSON.有什么建议吗?
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?
推荐答案
我看到了solr-node-client的代码,并且看到为了添加文档,他们使用了此函数Client.prototype.添加,此函数最终使用此处理程序
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';
这意味着,如果使用的Solr版本大于或等于4,则Solrconfig.xml文件中将存在一个名为update的请求处理程序.
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>
单个统一更新请求处理程序支持XML,CSV,JSON和 javabin更新请求,委托给适当的 基于ContentStream的Content-Type的ContentStreamLoader.
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.
Node包中的代码发送json,因此Solr将委托给适当的ContentStreamLoader.这是Node包中的代码
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',
'content-type' : 'application/json; charset=utf-8',
如果您使用的Solr版本低于4,则在solrconfig.xml中应该有一个这样的请求处理程序
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>
因此,长话短说,请确保如果使用Solr> = 4,则必须在solrconfig.xml中具有更新请求处理程序
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>
,如果您使用Solr< 4然后确保您有一个像这样的更新处理程序
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>
希望这会有所帮助
这篇关于"[[SyntaxError:意外令牌<]""当我尝试通过Node/Express应用将文档添加到Solr索引中时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!