将 Solr xml 文件解析为 SolrInputDocument
如果我有预期 Solr 格式的单个文件(每个文件只有一个文档):
<add>
<doc>
<field name="id">GB18030TEST</field>
<field name="name">Test with some GB18030 encoded characters</field>
<field name="features">No accents here</field>
<field name="features">ÕâÊÇÒ»¸ö¹¦ÄÜ</field>
<field name="price">0</field>
</doc>
</add>
是否有一种方法可以轻松将该文件编组到 SolrInputDocument 中?我必须自己进行解析吗?
编辑:我在 java pojo 中需要它,因为我想在使用 SolrJ 对其进行索引之前修改一些字段...
If I have individual files in the expected Solr format (having just ONE doc per file):
<add>
<doc>
<field name="id">GB18030TEST</field>
<field name="name">Test with some GB18030 encoded characters</field>
<field name="features">No accents here</field>
<field name="features">ÕâÊÇÒ»¸ö¹¦ÄÜ</field>
<field name="price">0</field>
</doc>
</add>
Is not there a way to easily marshal that file into a SolrInputDocument? Do I have to do the parsing myself?
EDIT: I need it in java pojo cause I want to modify some fields before indexing it with SolrJ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
痴梦一场2025-01-02 05:11:41
在 Java 中你可以做到这一点。
private void populateIndexFromXmlFile(String fileName) throws Exception {
UpdateRequest update = new UpdateRequest();
update.add(getSolrInputDocumentListFromXmlFile(fileName));
update.process(server);
server.commit();
}
private List<SolrInputDocument> getSolrInputDocumentListFromXmlFile(
String fileName) throws Exception {
ArrayList<SolrInputDocument> solrDocList = new ArrayList<SolrInputDocument>();
File fXmlFile = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList docList = doc.getElementsByTagName("doc");
for (int docIdx = 0; docIdx < docList.getLength(); docIdx++) {
Node docNode = docList.item(docIdx);
if (docNode.getNodeType() == Node.ELEMENT_NODE) {
SolrInputDocument solrInputDoc = new SolrInputDocument();
Element docElement = (Element) docNode;
NodeList fieldsList = docElement.getChildNodes();
for (int fieldIdx = 0; fieldIdx < fieldsList.getLength(); fieldIdx++) {
Node fieldNode = fieldsList.item(fieldIdx);
if (fieldNode.getNodeType() == Node.ELEMENT_NODE) {
Element fieldElement = (Element) fieldNode;
String fieldName = fieldElement.getAttribute("name");
String fieldValue = fieldElement.getTextContent();
solrInputDoc.addField(fieldName, fieldValue);
}
}
solrDocList.add(solrInputDoc);
}
}
return solrDocList;
}
画骨成沙2025-01-02 05:11:41
这最好以编程方式完成。我知道您正在寻找 Java 解决方案,但我个人推荐 groovy。
以下脚本处理在当前目录中找到的 XML 文件。
//
// Dependencies
// ============
import org.apache.solr.client.solrj.SolrServer
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer
import org.apache.solr.common.SolrInputDocument
@Grapes([
@Grab(group='org.apache.solr', module='solr-solrj', version='3.5.0'),
])
//
// Main
// =====
SolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");
new File(".").eachFileMatch(~/.*\.xml/) {
it.withReader { reader ->
def xml = new XmlSlurper().parse(reader)
xml.doc.each {
SolrInputDocument doc = new SolrInputDocument();
it.field.each {
doc.addField([email protected](), it.text())
}
server.add(doc)
}
}
}
server.commit()
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
编辑:为了将 XML 转换为 POJO,请参考之前的 SO 问题 - 是否有一个库可以将 Java POJO 与 JSON 和 XML 相互转换?
由于您已经拥有了预期格式的文档,你可以使用post.jar 或 post.sh 脚本文件,如 Solr 教程 - 索引中所示数据 都接受 xml 文件作为输入。
另外,还有一个 toSolrInputDocument() SolrJ ClientUtils 库中的方法可能对 你。当然,您需要将文件编组到 SolrDocument 类中才能使用
toSolrInputDocument()
方法。EDIT: In order to convert XML to POJO, please reference this previous SO Question - Is there a library to convert Java POJOs to and from JSON and XML?
Since you already have your documents in the expected format, you can just use the post.jar or post.sh script file as shown in the Solr Tutorial - Indexing Data that both accept xml files as input.
Also, there is a toSolrInputDocument() method in the SolrJ ClientUtils library that may be useful for you. Granted you would need to marshal the files into the SolrDocument class in order to use the
toSolrInputDocument()
method.