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 :