未在Solr中的文档中定义时设置last_modified字段

时间:2013-12-23 14:54:45

标签: apache solr lucene metadata apache-tika

我正在使用Solr 4.6示例的SimplePostTool将文件从文件系统导入Solr。一切都没问题,但只有当原始文档包含元数据时才会填充字段last_modified。如果该字段不存在,Solr提取器将该字段留空。

我尝试使用文件系统修改日期修改SimplePostTool来设置此字段,但是当我尝试从元数据导入已经具有last_modified字段的文件时,我收到此错误:

430584 [qtp1214238505-16] ERROR org.apache.solr.core.SolrCore  – 
  org.apache.solr.common.SolrException: ERROR: 
  [doc=4861976] multiple values encountered for non multiValued field 
  last_modified: [2013-12-22T14:03:10.000Z, 2013-07-02T11:29:20.000Z]

我正在考虑将自定义字段用于文件系统日期,但在我的情况下,元数据日期如果可用则更好。有没有办法在导入时合并它们?

谢谢!

3 个答案:

答案 0 :(得分:0)

您可以在架构中设置默认值。这样的事情应该有效:

<field name="my_date" type="date" indexed="true" stored="true" multiValued="false" default="NOW" />

字段类型定义:

<fieldType name="date"     class="solr.TrieDateField" sortMissingLast="true" omitNorms="true"/>

答案 1 :(得分:0)

在创建文档时,solr将所有输入作为文本进行处理,然后根据给定的数据类型进行验证。因此,接受任何形式的有效日期格式都可以与solr一起使用。 目前的时间 任何默认值

问候

拉​​杰特

答案 2 :(得分:0)

我终于解决了创建自定义更新请求处理器的问题,如下所述:http://wiki.apache.org/solr/UpdateRequestProcessor

我的处理器如下:

package com.mycompany.solr;

import java.io.IOException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.AddUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.update.processor.UpdateRequestProcessorFactory;

public class LastModifiedMergeProcessorFactory 
   extends UpdateRequestProcessorFactory {

  @Override
  public UpdateRequestProcessor getInstance(SolrQueryRequest req, 
       SolrQueryResponse rsp, UpdateRequestProcessor next) {
    return new LastModifiedMergeProcessor(next);
  }
} 

class LastModifiedMergeProcessor extends UpdateRequestProcessor {

  public LastModifiedMergeProcessor(UpdateRequestProcessor next) {
    super(next);
  }

  @Override
  public void processAdd(AddUpdateCommand cmd) throws IOException {
    SolrInputDocument doc = cmd.getSolrInputDocument();

    Object metaDate = doc.getFieldValue( "last_modified" );
    Object fileDate = doc.getFieldValue( "file_date" );
    if( metaDate == null && fileDate != null) {
        doc.addField( "last_modified", fileDate );
    }

      // pass it up the chain
      super.processAdd(cmd);
    }   
  }

其中file_date是我在导入时使用文件修改日期设置的字段。