使用Apache Solr为pdf文件内容编制索引


Index pdf file content using Apache Solr

我正在使用Solr的php扩展与Apache Solr进行交互。我正在为数据库中的数据编制索引。我也想索引外部文件(如PDF、PPTX)的内容。

索引的逻辑是:假设schema.xml定义了以下字段:

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="created" type="tlong" indexed="true" stored="true" />
<field name="name" type="text_general" indexed="true" stored="true"/>
<field name="filepath" type="text_general" indexed="false" stored="true"/>
<field name="filecontent" type="text_general" indexed="false" stored="true"/>

单个数据库条目可能/可能没有存储文件。

因此,以下是我的索引代码:

$post = stdclass object having the database content
$doc = new SolrInputDocument();
$doc->addField('id', $post->id);
$doc->addField('name', $post->name);
....
....
$res = $client->addDocument($doc);
$client->commit();

接下来,我想将PDF文件的内容添加到与上面相同的solr文档中。

这是curl代码:

$ch = curl_init('
http://localhost:8010/solr/update/extract?');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, array('myfile'=>'@'.$post->filepath));
$result= curl_exec ($ch);

但是,我想我错过了什么。我阅读了文档,但我不知道如何检索文件的内容,然后将其添加到field: filecontent 中现有的solr文档中

编辑#1:如果我尝试在curl请求中设置literal.id=xyz,它将创建一个具有id=xyz的新solr文档。我不希望创建新的solr文档。我希望pdf的内容被编入索引,并作为字段存储在之前创建的solr文档中。

$doc = new SolrInputDocument();//Solr document is created
$doc->addField('id', 98765);//The solr document created above is assigned an id=`98765`
....
....
$ch = curl_init('
http://localhost:8010/solr/update/extract?literal.id=1&literal.name=Name&commit=true');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, array('myfile'=>'@'.$post->filepath));
$result= curl_exec ($ch);

我希望上面的solr文档(id = 98765)具有一个字段,在该字段中pdf的内容被索引&存储。

但是cURL请求(如上所述)创建了另一个新文档(具有id = 1)。我不想那样。

使用Apache Tika的Solr处理提取富文档的内容并将其添加回Solr文档。

文件:-

您可能会注意到,尽管您可以在示例文档,当检索文档。这仅仅是因为"内容"字段Tika生成的Solr字段被映射到名为"text"的Solr域,即索引但未存储。这是通过/solrconfig.xml中的更新/提取处理程序,可以很容易地更改或被覆盖。例如为了存储和查看所有元数据和内容,执行以下操作:

默认schema.xml:-

<!-- Main body of document extracted by SolrCell.
    NOTE: This field is not indexed by default, since it is also copied to "text"
    using copyField below. This is to save space. Use this field for returning and
    highlighting document content. Use the "text" field to search the content. -->
<field name="content" type="text_general" indexed="false" stored="true" multiValued="true"/>

如果要定义不同的属性来维护文件内容,请在solrconfig.xml本身中使用fmap.content=filecontent覆盖默认值。

fmap.content=attr_content参数覆盖默认值fmap.content=导致将内容添加到attr_content的文本字段。

如果您想在单个文档中对其进行索引,请使用文字前缀,例如具有属性的literal.id=1&literal.name=Name

$ch = curl_init('
http://localhost:8010/solr/update/extract?literal.id=1&literal.name=Name&commit=true');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, array('myfile'=>'@'.$post->filepath));
$result= curl_exec ($ch);