【发布时间】: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