Sunday, January 24, 2021

Connect .Net Core 5.0 Console App to Solr 8 using SolrExpress

 For Console or any One Page Web APP

Task
Connect to Solr http://IP:8983/solr/CoreName/ and get fields (title, author) where title=laser,
PageSize=10 and get the results from page 2
Use author as Facet field 

Use Package manager to get the next Packages

 Install-Package SolrExpress -Version 5.5.0  
 Install-Package SolrExpress.Solr5 -Version 5.5.0  
 Install-Package SolrExpress.DI.CoreClr -Version 5.5.0 



Code:

 using Microsoft.Extensions.DependencyInjection;  
 using Newtonsoft.Json;  
 using SolrExpress;  
 using SolrExpress.Configuration;  
 using SolrExpress.DI.CoreClr;  
 using SolrExpress.Options;  
 using SolrExpress.Search.Extension;  
 using SolrExpress.Search.Parameter.Extension;  
 using SolrExpress.Search.Result.Extension;  
 using SolrExpress.Solr5.Extension;  
 using SolrExpress.Solr5.Update;  


Model

   public class SolrModel : Document  
   {  
     [SolrField("title")]  
     public string title { get; set; }  
     [SolrField("author")]  
     public string author { get; set; }  
   }  



       var services = new ServiceCollection()  
           .AddSolrExpress<SolrModel>(builder => builder  
           .UseOptions(q => q.HasHostAddress("http://IP:8983/solr/CoreName/"))  
           .UseSolr5());  
       var serviceProvider = services.BuildServiceProvider();  
       DocumentCollection<SolrModel> _solrModel = serviceProvider.GetRequiredService<DocumentCollection<SolrModel>>();  
       _solrModel  
         .Select()  
         .Fields(d => d.title, d => d.author)  
         .FacetField(d => d.author)  
         .Filter(d => d.title, "laser")  
         .Limit(10) //.Limit(itemsPerPage)  
         .Offset(2) //.Offset(page)  
         .Execute()  
         .Information(out var information)  
         .Document(out var documents)  
         .Facets(out var facets);  
       Console.WriteLine(JsonConvert.SerializeObject(information, Formatting.Indented));
       Console.WriteLine(JsonConvert.SerializeObject(documents, Formatting.Indented));
Console.WriteLine(JsonConvert.SerializeObject(facets, Formatting.Indented));


Sample  Result

         ** For Informations  
         {  
            "PageSize": 10,  
            "PageNumber": 1,  
            "PageCount": 1,  
            "HasPreviousPage": false,  
            "HasNextPage": false,  
            "IsFirstPage": true,  
            "IsLastPage": true,  
            "DocumentCount": 1,  
            "ElapsedTime": "00:00:00.0080000",  
            "NextCursorMark": null  
           }  
         
       ** For documents  
       [  
        {  
         "title": "laser",  
         "author": "Bashir, Lubna Z."
        }  
       ]  
       
       ** For facets  
       [  
         {  
         "Name": "title",  
         "FacetType": 0,  
         "Tag": null,  
         "Values": [  
           {  
           "Key": "University admission system using machine learning /  ",  
           "Quantity": 1,  
           "Facets": null  
           }  
         ]  
         }  
       ]  
        


Here are list Filters that can use for query 


Use case

How to

Solr Query generated

Query to find all informed values (conditional AND)

query.Field(f => f.Categories).All("category1", "category2")

cat:("category1" AND "category2")

Query to find some of informed values (conditional OR)

query.Field(f => f.Categories).Any("category1", "category2")

cat:("category1" OR "category2")

Query to find something starts with informed value

query.Field(f => f.Categories).StartsWith("c")

cat:"c*"

Query to find exact informed value

query.Field(f => f.Categories).EqualsTo("category1")

cat:"category1"

Query to find negate informed value

query.Field(f => f.Categories).NotEqualsTo("category1")

NOT(cat:"category1")

Query to find someting in informed range

query.Field(f => f.Price).InRange(1, 10)

price:[1 TO 10]

Query to find someting greater than informed value

query.Field(f => f.Price).GreaterThan(1)

price:[1 TO *]

Query to find someting less than informed value                

query.Field(f => f.Price).LessThan(1)    

price:[* TO 10] 

Query expression isolating in a groupquery.Group(price=> price.Filed(f => f.Price).InRange(1, 10).Or(popularity => popularity.Field(f => f.Popularity).GreaterThan(5)))(price:[1 TO 10] OR  popularity:[5 TO *])





No comments: