Search code examples
c#solrsolrnetquerying

How to perform indexing/search on multiple tables in SolrNet


I am using SolrNet to index/search data on set of tables.

I have two tables Category and Item.

Both the tables have same fields, Hence i have a base mapping class form which i derive.

[Serializable]
    [XmlRoot(ElementName = "SolrSearchEntity", Namespace = "")]
    [DataContract(Name = "SolrSearchEntity", Namespace = "")]
    public class SolrSearchEntity
    {
        [XmlElement(ElementName = "Id")]
        [DataMember(Name = "Id", IsRequired = true)]
        [SolrUniqueKey("id")]
        public string Id { get; set; }

        [XmlElement(ElementName = "Name")]
        [DataMember(Name = "Name", IsRequired = true)]
        [SolrField("name")]
        public string Name { get; set; }
    }

public class Category : SolrSearchEntity
    {        
    }

    public class Item : SolrSearchEntity
    {
    }

code block for indexing Category data

using (SolrBaseRepository.Instance<Category> repository = new SolrBaseRepository.Instance<Category>())
            {
                var output = ItemStoreDataManager.GetAllCategoryNames(dataAdapter);
                repository.Start();
                var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Category>>();
                solr.AddRange(output);
                solr.Commit();
            }

code block for indexing Item data

using (SolrBaseRepository.Instance<Item> repository = new SolrBaseRepository.Instance<Item>())
                {
                    var output = ItemStoreDataManager.GetAllItemNames(dataAdapter);
                    repository.Start();
                    var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Item>>();
                    solr.AddRange(output);
                    solr.Commit();
                }

My Schema.xml has

 <fields>
    <!-- declare fields of entity class -->
    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
    <field name="name" type="text_general" indexed="true" stored="true" omitNorms="true"/>

    <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>
    <field name="_version_" type="long" indexed="true" stored="true"/>

  </fields>

Querying for data in Category

  var entities = ItemStoreManager<Category>.Search(keyword); which will internally execute this.

  new SolrBaseRepository.Instance<T>().Start();
  var solr = ServiceLocator.Current.GetInstance<ISolrOperations<T>>();
  var results = solr.Query(keyword);

Strangely, I am getting the records from the Item table.

How would i tell the SolrNet (search engine) to look within the type i have specified.

Or i am indexing correctly in the first place?

Please help.

Thanks,


Solution

  • In addition to the suggestion mentioned by Cook,

    Add GUID field in SolrSearchEntity

     [XmlElement(ElementName = "UId")]
     [DataMember(Name = "UId", IsRequired = true)]
     [SolrUniqueKey("uid")]
     public Guid UId { get; set; }
    

    Initialise Uid in the constructor

    public Item()
    {
       Type = "Item";
       UId = Guid.NewGuid();
    }
    

    Changes in schema.xml

    <fieldType name="uuid" class="solr.UUIDField" indexed="true" />
    
     <!-- unique field -->
    <field name="uid" type="uuid" indexed="true" stored="true" />
    
    <uniqueKey>uid</uniqueKey>
    

    Now the index will not overlap or inconsistent and the search will be narrowed down to the type specified.