【问题标题】:How to add a new field to the document in a custom Solr Filter如何在自定义 Solr 过滤器中向文档添加新字段
【发布时间】:2013-11-24 07:21:51
【问题描述】:

我正在 Solr 中编写一个自定义过滤器,以将令牌发布到 Apache Stanbol 以进行增强并将响应索引到同一文档中的不同字段。

在下面的测试代码中,我得到了 Stanbol 响应并将其作为新文档添加到 Solr。我的要求是将 stanbolResponse 作为字段值添加到被索引的同一文档中。 我认为如果我可以从过滤器中的 TokenStream 中检索文档 ID,就可以做到这一点。

谁能帮助我提供示例代码/示例或有关如何实现此目的的链接?

public boolean incrementToken() throws IOException {
    if (!input.incrementToken()) {
      return false;
    }

    int length = charTermAttr.length();
    char[] buffer = charTermAttr.buffer();
    String content = new String(buffer);
    Client client = Client.create();
    WebResource webResource = client.resource(stanbol_endpoint + "enhancer");
    ClientResponse response = webResource
        .type(MediaType.TEXT_PLAIN)
        .accept(new MediaType("application", "rdf+xml"))
        .entity(content2,MediaType.TEXT_PLAIN)
        .post(ClientResponse.class);

    int status = response.getStatus();
    if (status != 200 && status != 201 && status != 202) {
        throw new RuntimeException("Failed : HTTP error code : "
             + response.getStatus());
    }

    String output = response.getEntity(String.class);
    charTermAttr.setEmpty();
    char[] newBuffer = output.toCharArray();
    charTermAttr.copyBuffer(newBuffer, 0, newBuffer.length);

    SolrInputDocument doc1 = new SolrInputDocument();
    doc1.addField( "id", "id1", 1.0f );
    doc1.addField("stanbolResponse", output);
    try {
        server.add(doc1);
        server.commit();
    } catch (SolrServerException e) {
        System.out.println("error while indexing response to solr");
        e.printStackTrace();
    }
    return true;
}

【问题讨论】:

  • 也许我误解了你的情况,但你不能简单地创建一个UpdateRequestProcessor先验分析吗?你可以在你的处理器中做任何你想做的事情,然后将结果添加到文档中并通过正常的分析链传递它。
  • 是的 Lexk,这个用例已通过编写 UpdateRequestProcessor 成功覆盖。

标签: solr filter solrj apache-stanbol


【解决方案1】:

通过编写自定义 UpdateRequestProcessor 并配置 /update 请求处理程序以在 update.chain 中使用我的自定义处理器,成功覆盖了此用例。

我能够在索引之前处理并向文档添加新字段。 下面是我如何使用自定义处理器配置 /update 请求处理程序。

用于 stanbol 进程的 RequestProcessor:

<updateRequestProcessorChain name="stanbolInterceptor">
    <processor class="com.solr.stanbol.processor.StanbolContentProcessorFactory"/>
    <processor class="solr.RunUpdateProcessorFactory" />
</updateRequestProcessorChain>

为 update.chain 配置上述链的请求处理程序:

<requestHandler name="/update" class="solr.UpdateRequestHandler">
       <lst name="defaults">
         <str name="update.chain">stanbolInterceptor</str>
       </lst>
</requestHandler>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多