当我尝试使用 Node/Express 应用程序将文档添加到 Solr 索引时,出现“[SyntaxError: Unexpected token <]”

2024-01-31

我正在尝试将 Solr 集成到 Node/Express 应用程序中,当我使用时this https://github.com/lbdremy/solr-node-client用于 Solr 集成的节点包。我能够正确查询索引,但按照包的 GitHub 上的说明,我无法将文档添加到索引。我不断收到此错误:

[SyntaxError: Unexpected token <]

在 Solr 控制台上,错误更详细一些:

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

这是我用来尝试使其正常工作的一个小型测试应用程序:

// 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');

不起作用的具体部分是我尝试将文档添加到此处的索引的地方:

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

我的预感是,这与在需要 XML 时接收 JSON 有关,但我一生都无法弄清楚如何发送 XML 或使 Solr 接受 JSON。有什么建议吗?


我看过一点 solr-node-client 的代码,我发现为了添加文档,他们使用了这个函数 Client.prototype.add 并且这个函数最终使用了这个处理程序

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

这意味着,如果您使用大于或等于 4 的 Solr 版本,则在 Solrconfig.xml 文件中存在一个名为 update 的请求处理程序。

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

  </requestHandler>

单个统一的更新请求处理程序支持 XML、CSV、JSON 和 javabin更新请求,委托给适当的 ContentStreamLoader 基于ContentStream 的Content-Type。

看看这里https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers#UploadingDatawithIndexHandlers-JSONFormattedIndexUpdates https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers#UploadingDatawithIndexHandlers-JSONFormattedIndexUpdates

Node包中的代码发送json,因此Solr将委托给适当的ContentStreamLoader。这是Node包中的代码

'内容类型':'应用程序/json;字符集=utf-8',

如果您使用低于版本 4 的 Solr,那么在 solrconfig.xml 中您应该有一个像这样的请求处理程序

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

因此,长话短说,请确保如果您使用 Solr >=4,那么在 solrconfig.xml 中您必须有一个更新请求处理程序

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

如果您使用 Solr

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

希望这可以帮助

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我尝试使用 Node/Express 应用程序将文档添加到 Solr 索引时,出现“[SyntaxError: Unexpected token <]” 的相关文章

随机推荐