Question

I am currently trying to integrate Solrnet with a project I am working on and I am unable to even get Solrnet to produce a document from a populated POCO. Below is an example of the POCOs i'm using

public class Person : ICustomInterface
{
    [SolrField("text")]
    public string ContactNumber { get; set; }

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

    [SolrField("text")]
    public string ContactEmail { get; set; }
    [SolrField("text")]
    public string FamilyName { get; set; }
    [SolrField("text")]
    public string GivenName { get; set; }
    [SolrField("text")]
    public string MiddleName { get; set; }
    [SolrField("text")]
    public string Title { get; set; }
    [SolrField("text")]
    public string Gender { get; set; }
    [SolrField("text")]
    public string PlaceOfBirth { get; set; }
    [SolrField("text")]
    public string CountryOfBirth { get; set; }

    [SolrUniqueKey("id")]
    public string Id { get; set; }
}

I am calling the init function before I try and index the above using the following code.

Startup.Init<Person>("http://localhost:8080/solr-4.1.0");

I am then calling the following to try and index the object.

var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Person>>();
solr.Add(mypoco);
solr.Commit();

The error on 'solr.Add(person)' is: "Document is missing mandatory uniqueKey field: id". Looking at the http request being sent, this makes sense, the body of the request is : <add><doc /></add>.

It seems that no fields are getting picked up. Looking into it further, 'AttributesMappingManager' is return 0 results. Method is below and commented results I am seeing from 'GetPropertiesWithAttribute'.

var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); //returns properties correctly
var kvAttrs = props.Select(prop => new KeyValuePair<PropertyInfo, T[]>(prop, type.GetCustomAttributes<T>())); // correct number of keys of properties with Solrnet attributes, but values are empty
var propsAttrs = kvAttrs.Where(kv => kv.Value.Length > 0); // 0 results
return propsAttrs; // 0 results

I have tried breaking this out into a test project and still get the same problems. I have tried with and without an interface and still no luck. I am probably missing something really simple, but would love to know what it is.

Note: I am using the latest version of Solrnet from the github repository

Update Tested the 'SampleSolrApp' solution, fixed up some references (couldn't find SolrNet.DSL in 'HomeController.cs', SolrNet.DSL ref seemed to be missing, added reference from compiled github repo), sample seems to initialize correctly, posts 'exampledocs' via the AddInitialDocuments() method on Application_Start. That works, however, shortly after I get the error "The given key was not present in the dictionary." within 'HtmlHelperMapperExtensions.cs'. The 'Product' object does have the correct attributes, with the field name the mapper is looking for, but can't find any fields('cat' in this case). This matches my problem I am having as it is not constructing a valid http request for solr because it can't seem to find the SolrField attributes.

Going to test the sample on another PC to see if this problem is something to do with my dev environment. Any advice or suggestions would be appreciated.

Update 2 Tested on another dev environment and I am getting the same error. So it seems that the GetCustomAttributes extension methods are not returning the attributes on the pocos even in the sample. I am running the samples on VS 2012 (.net 4.5, targeting 3.5 in the sample project), IIS Express on a Win7 machine. Hope someone might be able to point me in the right direction for further investigation.

Was it helpful?

Solution 2

The (user) error has been resolved, a simple mistake with odd consequences. I'm still not exactly sure what was root cause of the problem, but the locally compiled dlls I was using for both the sample app and my own application were built from VS solutions didn't work. I had some trouble (crashed as soon as I ran it, problem with the local machine, not the build script) running the build.bat file when I first downloaded it, hence why I tried building it from the solution/projects directly.

Moving to another PC I built all the libs from the build.bat file, the sample app now works and so does my other application.

TLDR

Build libs from Build.bat

OTHER TIPS

First off, not sure why you are getting the "Document missing mandatory uniqueKey field: id" error, because you appear to have decorated your Id property appropriately. Since you did not show how you are creating your mypoco object, I would check that you are setting the Id property (as the Id needs to be supplied (and unique) as is not auto-assigned by Solr). Also, I noticed that that you are mapping all of the other fields to the text field in your schema. This is not the standard practice for accomplishing this behavior. You typically would be mapping each property on your POCO to a separate field in your schema (see Mapping on the SolrNet Project Page for an example) and using the copyField directive in the schema to move all of the individual fields into the common text field. That allows you to do a search on either the combined text field or the individual fields along with enabling options such as highlighting and faceting on the individual fields.

Please check out Documents, Fields and Schema Design for more details on setting up your own custom fields and using copyField. Additionally, since you have the SolrNet source from GitHub, I would recommend running and examining the code in the SampleSolrApp directory to get a good working understanding of SolrNet. You can start the sample via runsample.bat in the source parent directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top