【问题标题】:How to index pdf's content with SolrJ?如何使用 Solr 索引 pdf 内容?
【发布时间】:2011-04-17 13:06:44
【问题描述】:

我正在尝试使用 SolrJ 索引一些 pdf 文档,如 http://wiki.apache.org/solr/ContentStreamUpdateRequestExample 所述,下面是代码:

import static org.apache.solr.handler.extraction.ExtractingParams.LITERALS_PREFIX;
import static org.apache.solr.handler.extraction.ExtractingParams.MAP_PREFIX;
import static org.apache.solr.handler.extraction.ExtractingParams.UNKNOWN_FIELD_PREFIX;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.common.util.NamedList;
...
public static void indexFilesSolrCell(String fileName) throws IOException, SolrServerException {

  String urlString = "http://localhost:8080/solr"; 
  SolrServer server = new CommonsHttpSolrServer(urlString);

  ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
  up.addFile(new File(fileName));
  String id = fileName.substring(fileName.lastIndexOf('/')+1);
  System.out.println(id);

  up.setParam(LITERALS_PREFIX + "id", id);
  up.setParam(LITERALS_PREFIX + "location", fileName); // this field doesn't exists in schema.xml, it'll be created as attr_location
  up.setParam(UNKNOWN_FIELD_PREFIX, "attr_");
  up.setParam(MAP_PREFIX + "content", "attr_content");
  up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);

  NamedList<Object> request = server.request(up);
  for(Entry<String, Object> entry : request){
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
  }
}

不幸的是,在查询 *:* 时,我得到了索引文档列表,但内容字段为空。如何更改上面的代码以提取文档的内容?

下面是描述this document的xml框架:

<doc>
  <arr name="attr_content">
    <str>            </str>
  </arr>
  <arr name="attr_location">
    <str>/home/alex/Documents/lsp.pdf</str>
  </arr>
  <arr name="attr_meta">
    <str>stream_size</str>
    <str>31203</str>
    <str>Content-Type</str>
    <str>application/pdf</str>
  </arr>
  <arr name="attr_stream_size">
    <str>31203</str>
  </arr>
  <arr name="content_type">
    <str>application/pdf</str>
  </arr>
  <str name="id">lsp.pdf</str>
</doc>

我认为这个问题与 Apache Tika 的错误安装无关,因为之前我有一些 ServerException 但现在我已经在正确的路径中安装了所需的 jar。此外,我尝试使用同一类索引 txt 文件,但 attr_content 字段始终为空。

【问题讨论】:

    标签: java solr solr-cell


    【解决方案1】:

    在 schema.xml 文件中,你是否在内容字段中设置了“stored=true”,这是我的 schema.xml 文件的一个示例,用于存储 pdf 和其他二进制文件的内容。

    <field name="text" type="textgen" indexed="true" stored="true" required="false" multiValued="true"/>

    对你有帮助吗?

    赫克托

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2017-10-30
      • 2011-10-05
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多