2013-03-26 93 views
0

我正在嘗試使用c#編寫solrnet。我安裝了一個bitnami apache + solr棧並更改了schema.xml文件。我想下面的例子: http://www.chrisumbel.com/article/solrnet_solr_netsolrnet查詢失敗

模式文件

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
    <field name="title" type="string" indexed="true" stored="true" required="true"/> 
    <field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/> 
    <field name="name" type="text_general" indexed="true" stored="true"/> 
    <field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/> 
    <field name="cat" type="string" indexed="true" stored="true" multiValued="true"/> 
    <field name="features" type="text_general" indexed="true" stored="true" multiValued="true"/> 
    <field name="includes" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true" /> 

    <field name="weight" type="float" indexed="true" stored="true"/> 
    <field name="price" type="float" indexed="true" stored="true"/> 
    <field name="popularity" type="int" indexed="true" stored="true" /> 
    <field name="inStock" type="boolean" indexed="true" stored="true" /> 

    <field name="store" type="location" indexed="true" stored="true"/> 

    <!-- Common metadata fields, named specifically to match up with 
    SolrCell metadata when parsing rich documents such as Word, PDF. 
    Some fields are multiValued only because Tika currently may return 
    multiple values for them. Some metadata is parsed from the documents, 
    but there are some which come from the client context: 
     "content_type": From the HTTP headers of incoming stream 
     "resourcename": From SolrCell request param resource.name 
    --> 
    <!-- <field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/> --> 
    <field name="tag" type="text_general" indexed="true" stored="true" multiValued="true"/> 
    <field name="subject" type="text_general" indexed="true" stored="true"/> 
    <field name="description" type="text_general" indexed="true" stored="true"/> 
    <field name="comments" type="text_general" indexed="true" stored="true"/> 
    <field name="author" type="text_general" indexed="true" stored="true"/> 
    <field name="keywords" type="text_general" indexed="true" stored="true"/> 
    <field name="category" type="text_general" indexed="true" stored="true"/> 
    <field name="resourcename" type="text_general" indexed="true" stored="true"/> 
    <field name="url" type="text_general" indexed="true" stored="true"/> 
    <field name="content_type" type="string" indexed="true" stored="true" multiValued="true"/> 
    <field name="last_modified" type="date" indexed="true" stored="true"/> 
    <field name="links" type="string" indexed="true" stored="true" multiValued="true"/> 

    <!-- 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"/> 


    <!-- catchall field, containing all other searchable text fields (implemented 
     via copyField further on in this schema --> 
    <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/> 

    <!-- catchall text field that indexes tokens both normally and in reverse for efficient 
     leading wildcard queries. --> 
    <field name="text_rev" type="text_general_rev" indexed="true" stored="false" multiValued="true"/> 

    <!-- non-tokenized version of manufacturer to make it easier to sort or group 
     results by manufacturer. copied from "manu" via copyField --> 
    <field name="manu_exact" type="string" indexed="true" stored="false"/> 

    <field name="payloads" type="payloads" indexed="true" stored="true"/> 

    <field name="_version_" type="long" indexed="true" stored="true"/> 

這裏是代碼:

article.cs

using System; 
using System.Collections.Generic; 
using SolrNet; 
using SolrNet.Attributes; 
using SolrNet.Commands.Parameters; 
using Microsoft.Practices.ServiceLocation; 

class Article 
{ 
    [SolrUniqueKey("id")] 
    public int id { get; set; } 

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

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

    //[SolrField("tag")] 
    //public List<string> Tags { get; set; } 
} 

主要PROGRAMM:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Microsoft.Practices.ServiceLocation; 
using SolrNet; 

namespace SolrTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // find the service 
      Startup.Init<Article>("http://berserkerpc:444/solr"); 
      ISolrOperations<Article> solr = 
    ServiceLocator.Current.GetInstance<ISolrOperations<Article>>(); 


      //AddArticles(solr); 

      Query(solr); 

      Console.ReadLine(); 

     } 
     private static ISolrOperations<Article> AddArticles(ISolrOperations<Article> solr) 
     { 
      // make some articles 
      solr.Add(new Article() 
      { 
       id = 1, 
       Title = "my laptop", 
       Content = "my laptop is a portable power station", 
       //  Tags = new List<string>() { 
       // "laptop", 
       // "computer", 
       // "device" 
       //} 
      }); 

      solr.Add(new Article() 
      { 
       id = 2, 
       Title = "my iphone", 
       Content = "my iphone consumes power", 
       //  Tags = new List<string>() { 
       // "phone", 
       // "apple", 
       // "device" 
       //} 
      }); 

      solr.Add(new Article() 
      { 
       id = 3, 
       Title = "your blackberry", 
       Content = "your blackberry has an alt key", 
       //  Tags = new List<string>() { 
       // "phone", 
       // "rim", 
       // "device" 
       //} 
      }); 

      // commit to the index 
      solr.Commit(); 
      return solr; 
     } 
     private static void Query(ISolrOperations<Article> solr) 
     { 

      // fulltext "power" search 
      Console.WriteLine("POWER ARTICLES:"); 

      SolrQueryResults<Article> powerArticles = solr.Query(new SolrQuery("power")); 

      foreach (Article article in powerArticles) 
      { 
       Console.WriteLine(string.Format("{0}: {1}", article.id, article.Title)); 
      } 

      Console.WriteLine(); 


      //// tag search for "phone" 
      //Console.WriteLine("PHONE TAGGED ARTICLES:"); 
      //SolrQueryResults<Article> phoneTaggedArticles = solr.Query(new SolrQuery("tag:phone")); 

      //foreach (Article article in phoneTaggedArticles) 
      //{ 
      // Console.WriteLine(string.Format("{0}: {1}", article.id, article.Title)); 
      //} 
     } 
    } 
} 

solr正在運行,我可以連接。還增加了三篇文章。如果我開始查詢時,會出現以下異常:

System.ArgumentException was unhandled 
    HResult=-2147024809 
    Message=Could not convert value 'System.Collections.ArrayList' to property 'Content' of document type Article 
    Source=SolrNet 
    StackTrace: 
     bei SolrNet.Impl.DocumentPropertyVisitors.RegularDocumentVisitor.Visit(Object doc, String fieldName, XElement field) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\DocumentPropertyVisitors\RegularDocumentVisitor.cs:Zeile 53. 
     bei SolrNet.Impl.DocumentPropertyVisitors.AggregateDocumentVisitor.Visit(Object doc, String fieldName, XElement field) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\DocumentPropertyVisitors\AggregateDocumentVisitor.cs:Zeile 37. 
     bei SolrNet.Impl.DocumentPropertyVisitors.DefaultDocumentVisitor.Visit(Object doc, String fieldName, XElement field) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\DocumentPropertyVisitors\DefaultDocumentVisitor.cs:Zeile 39. 
     bei SolrNet.Impl.SolrDocumentResponseParser`1.ParseDocument(XElement node) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\SolrDocumentResponseParser.cs:Zeile 63. 
     bei SolrNet.Impl.SolrDocumentResponseParser`1.ParseResults(XElement parentNode) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\SolrDocumentResponseParser.cs:Zeile 48. 
     bei SolrNet.Impl.ResponseParsers.ResultsResponseParser`1.Parse(XDocument xml, AbstractSolrQueryResults`1 results) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\ResponseParsers\ResultsResponseParser.cs:Zeile 53. 
     bei SolrNet.Impl.ResponseParsers.AggregateResponseParser`1.Parse(XDocument xml, AbstractSolrQueryResults`1 results) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\ResponseParsers\AggregateResponseParser.cs:Zeile 15. 
     bei SolrNet.Impl.ResponseParsers.DefaultResponseParser`1.Parse(XDocument xml, AbstractSolrQueryResults`1 results) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\ResponseParsers\DefaultResponseParser.cs:Zeile 28. 
     bei SolrNet.Impl.SolrQueryExecuter`1.Execute(ISolrQuery q, QueryOptions options) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\SolrQueryExecuter.cs:Zeile 588. 
     bei SolrNet.Impl.SolrBasicServer`1.Query(ISolrQuery query, QueryOptions options) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\SolrBasicServer.cs:Zeile 98. 
     bei SolrNet.Impl.SolrServer`1.Query(ISolrQuery query, QueryOptions options) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\SolrServer.cs:Zeile 49. 
     bei SolrNet.Impl.SolrServer`1.Query(ISolrQuery q) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\SolrServer.cs:Zeile 88. 
     bei SolrTest.Program.Query(ISolrOperations`1 solr) in c:\Users\troth\Desktop\SolrTest\SolrTest\Program.cs:Zeile 77. 
     bei SolrTest.Program.Main(String[] args) in c:\Users\troth\Desktop\SolrTest\SolrTest\Program.cs:Zeile 23. 
     bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     bei System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     bei System.Threading.ThreadHelper.ThreadStart() 
    InnerException: System.ArgumentException 
     HResult=-2147024809 
     Message=Das Objekt mit dem Typ "System.Collections.ArrayList" kann nicht in den Typ "System.String" konvertiert werden. 
     Source=mscorlib 
     StackTrace: 
      bei System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) 
      bei System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) 
      bei System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) 
      bei System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
      bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
      bei System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) 
      bei System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) 
      bei SolrNet.Impl.DocumentPropertyVisitors.RegularDocumentVisitor.Visit(Object doc, String fieldName, XElement field) in c:\Users\troth\Desktop\SolrNet-master\SolrNet\Impl\DocumentPropertyVisitors\RegularDocumentVisitor.cs:Zeile 51. 
     InnerException: 

我從solrnet下載最新版本。有什麼建議麼?

感謝, 卓

回答

0

得到它。我曾在架構文件錯誤的數據類型:

<field name="id" type="string" indexed="true" stored="true" required="true" /> 
    <field name="title" type="text_general" indexed="true" stored="true" required="true"/> 
    <field name="content" type="text_general" indexed="true" stored="true" required="true"/> 
    <field name="tag" type="string" indexed="true" stored="true" multiValued="true"/> 
    <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/> 

TRO

4

如果你想使用一個多值字段,則需要映射到一個ICollection在你的文章類。

[SolrField("content")] 
public ICollection<string> Content { get; set; } 

欲瞭解更多信息和示例,請參閱SolrNet項目頁面的Mapping部分。

+0

它也適用於列表 [SolrField(「tag」)] public List Tags {get;組; } – tro 2013-03-27 09:45:24

+0

確實,它可以與列表一起使用,但是在一個具體類型上使用接口是一個最佳實踐 - http://stackoverflow.com/questions/12241287/what-is-the-advantage-of-return -icollection環比列表 – 2013-03-27 11:38:15