将Solr xml文件解析为SolrInputDocument [英] parse Solr xml files to SolrInputDocument

查看:148
本文介绍了将Solr xml文件解析为SolrInputDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有预期Solr格式的单个文件(每个文件只有一个文档):

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>

是否有办法轻松将该文件封送到SolrInputDocument中?我是否必须自己进行解析?

Is not there a way to easily marshal that file into a SolrInputDocument? Do I have to do the parsing myself?

编辑:我需要在java pojo中使用它因为我想在使用SolrJ索引它之前修改一些字段...

I need it in java pojo cause I want to modify some fields before indexing it with SolrJ...

推荐答案

最好以编程方式完成。我知道您正在寻找Java解决方案,但我个人建议使用groovy。

This is best done programmatically. I know you're looking for a Java solution, but I'd personally recommend groovy.

以下脚本处理当前目录中的XML文件。

The following script processes XML files found in the current directory.

//
// 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(it.@name.text(), it.text())
            }

            server.add(doc)
        }
    }

}

server.commit()

这篇关于将Solr xml文件解析为SolrInputDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆