0

I am trying to work with solr with c#. I installed a bitnami apache + solr stack and changed the schema.xml file. I tried the following example: http://www.chrisumbel.com/article/solrnet_solr_net

schema file

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

here is the code:

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; }
}

main 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 is running and I can connect. also the three articles are added. If I start a query, the following exception appears:

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:

I downloaded the latest build from solrnet. any suggestions?

thanks, tro

tro
  • 914
  • 2
  • 10
  • 23

2 Answers2

4

If you want to use a multiValued field, you will need to map that to an ICollection in your Article class.

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

For more information and examples, please see the Mapping section of the SolrNet project page.

Paige Cook
  • 22,415
  • 3
  • 57
  • 68
  • it also works with the list [SolrField("tag")] public List Tags { get; set; } – tro Mar 27 '13 at 09:45
  • It is true that it works with List, but it is a best practice to use an interface over a concrete type - http://stackoverflow.com/questions/12241287/what-is-the-advantage-of-return-icollection-over-list – Paige Cook Mar 27 '13 at 11:38
0

got it. I had wrong data types in the schema file:

   <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

tro
  • 914
  • 2
  • 10
  • 23