【问题标题】:parse Solr xml files to SolrInputDocument将 Solr xml 文件解析为 SolrInputDocument
【发布时间】:2012-01-12 17:19:57
【问题描述】:

如果我有预期 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 索引之前修改一些字段...

【问题讨论】:

  • 谁编写单个 xml 文件?你?别人?
  • 太糟糕了!我想知道,因为您可以直接使用带有solrj annotations的java bean@

标签: java solr xml-parsing


【解决方案1】:

编辑:为了将 XML 转换为 POJO,请参考之前的 SO Question - Is there a library to convert Java POJOs to and from JSON and XML?

由于您已经拥有预期格式的文档,您可以只使用Solr Tutorial - Indexing Data 中所示的 post.jar 或 post.sh 脚本文件,它们都接受 xml 文件作为输入。

此外,SolrJ ClientUtils 库中有一个 toSolrInputDocument() 方法可能对您有用。当然,您需要将文件编组到 SolrDocument 类中才能使用 toSolrInputDocument() 方法。

【讨论】:

    【解决方案2】:

    在 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;
    
    }
    

    【讨论】:

      【解决方案3】:

      最好以编程方式完成。我知道您正在寻找 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(it.@name.text(), it.text())
                  }
      
                  server.add(doc)
              }
          }
      
      }
      
      server.commit()
      

      【讨论】:

      • 将每一个添加为文本文档。有趣
      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多