SolrNet异常:文档缺少必需的uniqueKey字段:id

时间:2015-01-24 08:32:59

标签: c# asp.net-mvc solr lucene solrnet

我正在开发一个使用SolrNet(版本0.4.0.2002)实现Solr(最新版本solr-4.10.3)的MVC应用程序。我是Solr / SolrNet的新手,并使用http://crazorsharp.blogspot.in/2010/01/full-text-search-using-solr-lucene-and.html教程进行练习。在使用SolrNet.dll索引数据库表数据以进行索引时,我收到了以下错误。

An exception of type 'SolrNet.Exceptions.SolrConnectionException' occurred in SolrNet.dll but was not handled in user code

Additional information: <?xml version="1.0" encoding="UTF-8"?>

<response>
<lst name="responseHeader">
   <int name="status">400</int>
   <int name="QTime">51</int>
</lst>
<lst name="error">
    <str name="msg">Document is missing mandatory uniqueKey field: id</str>
    <int name="code">400</int>
</lst>
</response>

schema.xml中

<?xml version="1.0" encoding="UTF-8" ?>
<schema name="nopAccelerate" version="1.7">
  <types>
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" />
    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0" omitNorms="true"/>
    <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="date" class="solr.TrieDateField" precisionStep="6" positionIncrementGap="0"/>
    <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <charFilter class="solr.HTMLStripCharFilterFactory"/>
        <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.PorterStemFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.PorterStemFilterFactory"/>
      </analyzer>
    </fieldType>
    <fieldType name="textSpell" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="nGramtext" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <charFilter class="solr.HTMLStripCharFilterFactory"/>
        <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.EdgeNGramFilterFactory" minGramSize="3" maxGramSize="15" side="front"/>
        <filter class="solr.EdgeNGramFilterFactory" minGramSize="3" maxGramSize="15" side="back"/>
        <filter class="solr.PorterStemFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.PorterStemFilterFactory"/>
      </analyzer>
    </fieldType>
  </types>
  <fields>  
    <field name="Id" type="int" indexed="true" stored="true" required="true" />
    <field name="FirstName" type="string" indexed="true" stored="true" required="false" />
    <field name="MiddleName" type="string" indexed="true" stored="false" required="false" />
    <field name="LastName" type="string" indexed="true" stored="false" required="false" />
    <field name="EmailId" type="string" indexed="true" stored="false" required="false" />
    <field name="MobileNumber" type="string" indexed="true" stored="false" required="false" /> 
    <field name="_version_" type="long" indexed="true" stored="true"/>
  </fields>
  <uniqueKey>Id</uniqueKey>
  <defaultSearchField>FirstName</defaultSearchField>
  <solrQueryParser defaultOperator="OR"/>
</schema>

core.properties

#Written by CorePropertiesLocator
#Wed Jan 21 10:30:51 IST 2015
name=mytestcore
config=D\:\\SolrTest\\mytestcore\\conf\\solrconfig.xml
schema=D\:\\SolrTest\\mytestcore\\conf\\schema.xml
dataDir=D\:\\SolrTest\\mytestcore\\data

EmpSolr.cs Solr,SQL&amp ;;之间的Mapper类SolrNet

public class EmpSolr
{
    [SolrUniqueKey("Id")]
    public int Id { get; set; }

    [SolrField("FirstName")]
    public string FirstName { get; set; }

    [SolrField("MiddleName")]
    public string MiddleName { get; set; }

    [SolrField("LastName")]
    public string LastName { get; set; }

    [SolrField("EmailId")]
    public string EmailId { get; set; }

    [SolrField("MobileNumber")]
    public string MobileNumber { get; set; }
}

Indexer Class

public void IndexFiles()
{
    //Startup.Init<EmpSolr>(this.solrUrl);
    Startup.Init<EmpSolr>("http://localhost:8888/solr/#/mytestcore"); //chagned default port during installations
    var solrWorker = ServiceLocator.Current.GetInstance<ISolrOperations<EmpSolr>>();

    var employees = new SolrRepository(this.connectionString).GetEmployees();
    solrWorker.Add(employees);
    solrWorker.Commit();
}

错误日志:

org.apache.solr.common.SolrException: Document is missing mandatory uniqueKey field: id
    at org.apache.solr.update.AddUpdateCommand.getIndexedId(AddUpdateCommand.java:93)
    at org.apache.solr.update.processor.DistributedUpdateProcessor.versionAdd(DistributedUpdateProcessor.java:939)
    at org.apache.solr.update.processor.DistributedUpdateProcessor.processAdd(DistributedUpdateProcessor.java:692)
    at org.apache.solr.update.processor.LogUpdateProcessor.processAdd(LogUpdateProcessorFactory.java:100)
    at org.apache.solr.handler.loader.XMLLoader.processUpdate(XMLLoader.java:247)
    at org.apache.solr.handler.loader.XMLLoader.load(XMLLoader.java:174)
    at org.apache.solr.handler.UpdateRequestHandler$1.load(UpdateRequestHandler.java:99)
    at org.apache.solr.handler.ContentStreamHandlerBase.handleRequestBody(ContentStreamHandlerBase.java:74)
    at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:135)
    at org.apache.solr.core.SolrCore.execute(SolrCore.java:1976)
    at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:777)
    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:418)
    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:207)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

这是我的错。我已经指定了错误的URL来映射核心。所以,我改变了

Startup.Init<EmpSolr>("http://localhost:8888/solr/#/mytestcore");

Startup.Init<EmpSolr>("http://localhost:8888/solr/mytestcore");

在网址中删除了额外的#