0

我正在尝试将 solr 与 c# 一起使用。我安装了一个 bitnami apache + solr 堆栈并更改了 schema.xml 文件。我尝试了以下示例: http: //www.chrisumbel.com/article/solrnet_solr_net

架构文件

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

这是代码:

文章.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; }
}

主程序:

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 下载了最新版本。有什么建议么?

谢谢,特罗

4

2 回答 2

4

如果你想使用一个多值字段,你需要将它映射到ICollection你的文章类中。

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

有关更多信息和示例,请参阅 SolrNet 项目页面的映射部分。

于 2013-03-26T15:39:43.100 回答
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"/>

特罗

于 2013-03-26T15:31:27.117 回答