Do Simple Query in SolR Index

SolrNetLight allows you to make queries with all your devices on your SolR server instance in very simple way.

Solr Index

Create a new solr index called "customer"
Update its "schema.xml" file with the following properties :

<!-- id--> 
<field name="id" type="long" indexed="true" stored="true" required="true" multiValued="false" />
<!-- firstName--> 
<field name="firstName" type="string" indexed="true" stored="true"/>
<!-- lastName --> 
<field name="lastName" type="string" indexed="true" stored="true"/> 
<!-- roles --> 
<field name="roles" type="string" indexed="true" stored="true" multiValued="true"/> 
<!-- phone_* --> 
<dynamicField name="phone_*" type="string" indexed="true" stored="true" />
	 


Restart your Solr instance to and make sure everything works.

Sample data


{
  "responseHeader": {
    "status": 0,
    "QTime": 0,
    "params": {
      "spellcheck": "true",
      "indent": "true",
      "q": "*:*",
      "_": "1404822864286",
      "wt": "json"
    }
  },
  "response": {
    "numFound": 4,
    "start": 0,
    "docs": [
      {
        "id": 1104,
        "lastName": "Lamande",
        "firstName": "Dorian",
        "roles": [
          ".NET"
        ],
        "phone_mobile": "0123456789",
        "phone_home": "0123456789",
        "_version_": 1473063496451096600
      },
      {
        "id": 1105,
        "lastName": "Fabrizi",
        "firstName": "Guillaume",
        "roles": [
          ".NET"
        ],
        "phone_mobile": "0123456789",
        "phone_home": "0123456789",
        "_version_": 1473063497375940600
      }
   ]
  }
}

Console App project

Create a new Console app project Add a new class "CustomerIndex" similar to below :

using Newtonsoft.Json;
using SolrNetLight.Attributes;
using SolrNetLight.Impl.FieldSerializers;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace SolrNetLight.ConsoleApp
{
    [DataContract]
    public class CustomerIndex
    {
        public const string INDEX_NAME = "customer";
        [SolrUniqueKey("id")]
        [DataMember(Name="id")]
        public long Id { get; set; }
        [DataMember(Name="lastName")]
        public string LastName { get; set; }
        [DataMember(Name="firstName")]
        public string FirstName { get; set; }
        [DataMember(Name = "roles")]
        public List Roles { get; set; }
        [DataMember(Name = "phone_")]
        [JsonConverter(typeof(DictionaryFieldJsonConverter))]
        public IDictionary PhoneNumber { get; set; }
        public CustomerIndex()
        {
            PhoneNumber = new Dictionary();
	     Roles = new List();
        }
    }
}


Link your class properties with SolrIndex schema's properties with DataMember attribute. Create a "CustomerService" class in order to create our Solr query methods.
                    
    public class CustomerService
    {
        private ISolrOperations _customerSolRInstance;
        public CustomerService()
        {
            _customerSolRInstance = ServiceLocator.Current.GetInstance>();
        }
        /// 
        /// Get Customer by firstName
        /// 
        /// 
        /// 
        public async Task GetCustomerByName(string firstName)
        {
            CustomerIndex result = null;
            try
            {
                var results = await _customerSolRInstance.Query(new SolrQueryByField("firstName", firstName));
                if (results != null && results.Count > 0)
                {
                    result = results[0];
                }
            }
            catch (SolrNetException ex)
            {
                
            }
            return result;
        }
        /// 
        /// Add new customers
        /// 
        public async Task AddCustomers()
        {
            try
            {
                var dlamande = new CustomerIndex
                {
                    Id = 1000,
                    FirstName = "Dorian",
                    LastName = "Lamande",
                    Roles = new List { ".NET" },
                    PhoneNumber = { new KeyValuePair("mobile", "0123456789"), new KeyValuePair("home", "0123456789") }
                };
                var gfabrizi = new CustomerIndex
                {
                    Id = 1001,
                    FirstName = "Guillaume",
                    LastName = "Fabrizi",
                    Roles = new List { ".NET" },
                    PhoneNumber = { new KeyValuePair("mobile", "0123456789"), new KeyValuePair("home", "0123456789") }
                };
                await _customerSolRInstance.Add(dlamande);
                await _customerSolRInstance.Add(gfabrizi);
              
                await _customerSolRInstance.Commit();
            }
            catch (Exception ex)
            {
            }
        }
    }
 

In Program.cs, you can add these following lines and try to get data

               class Program
    {
        private static CustomerService _customerService;
        static void Main(string[] args)
        {
            //Initialize Solr Customer Index with current Solr local instance
            Startup.Init(String.Format("{0}{1}", "http://localhost:8983/solr/", CustomerIndex.INDEX_NAME));
            //Instanciate customer service
            _customerService = new CustomerService();
            DoRequest().Wait();
            Console.ReadLine();
        }
        private static async Task DoRequest()
        {
            //Add query
            await _customerService.AddCustomers();
            //Make simple queries
            CustomerIndex dlamande = await _customerService.GetCustomerByName("Dorian");
            CustomerIndex gfabrizi = await _customerService.GetCustomerByName("Guillaume");
            //Display results
            Console.WriteLine(String.Concat(dlamande.FirstName, dlamande.LastName));
            Console.WriteLine(String.Concat(gfabrizi.FirstName, gfabrizi.LastName));
        }
    }
    

Result :