A simple and lightweight query .NET library for Solr, in a controlled, buildable and fail fast way.
If you want to use Solr 4.9+.
Just install SolrExpress.Solr4 package using below command.
Install-Package SolrExpress.Solr4
If you want to use Solr 5.3+.
Just install SolrExpress.Solr5 package using below command.
Install-Package SolrExpress.Solr5
.Net Framework 4.5 or higher
Core library with main logic and generic implementations
Solr 4 implementation, full compatibility with the Solr 4.9+.
Implementation using default query handler and mechanism provided by request parameters.
Solr 5 implementation, full compatibility with the Solr 5.3+.
Implementation using default query handler and mechanism provided by JSON Request API.
Allows send parameters to Sorl in a controlled and buildable way.
Create a facet field type parameter using the informed field name and sort type.
new FacetFieldParameter<MyDocument>(q => q.Id, SolrFacetSortType.CountDesc);
Create a facet query type parameter using the informed field alias, query class and sort type.
new FacetQueryParameter("Alias", new QueryAll(), SolrFacetSortType.CountDesc);
Create a facet range type parameter using the informed field name, query class and sort type.
new FacetRangeParameter<MyDocument>("X", q => q.Price, "1", "10", "20", SolrFacetSortType.CountDesc);
Create a fields parameter (field list in Solr 4) using the informed field list.
- One by one
new FieldListParameter<MyDocument>(q => q.Id);
new FieldListParameter<MyDocument>(q => q.Score);
- All in the same moment
new FieldListParameter<MyDocument>(q => q.Id, q => q.Score);
Create a fields parameter (filter query in Solr 4) using the informed query class.
new FilterParameter(new SingleValue<MyDocument>(q => q.Id, "XPTO"));
Create a limit parameter (rows in Solr 4) using the informed number.
new LimitParameter(50);
Create a minimum should match parameter using the informed expression.
new MinimumShouldMatchParameter("75%");
Create a offset parameter (start in Solr 4) using the informed number.
new OffsetParameter(50);
Create a query field parameter using the informed expression.
new QueryFieldParameter("Id^10 Name^5~2");
Create a query parameter using the informed query class.
new QueryParameter(new SingleValue<MyDocument>(q => q.Id, "XPTO"));
Create a sort parameter using the informed expression and ascending type.
new SortParameter(q => q.Id, true);
Create a spatial filter parameter using the informed spatial function, expression, geo coordinate of origin and distance from origin point.
// Using Geofilt function
new SpatialFilterParameter<MyDocument>(SolrSpatialFunctionType.Geofilt, q => q.Spatial, new GeoCoordinate(-1.1M, -2.2M), 5.5M);
// Using Bbox function
new SpatialFilterParameter<MyDocument>(SolrSpatialFunctionType.Bbox, q => q.Spatial, new GeoCoordinate(-1.1M, -2.2M), 5.5M);
Allows create simple or complex queries in a controlled, buildable and testable way.
Create a query to return all documents.
// Create a query like "*:*"
new QueryAll();
Create a free value query, this is weakest query class, because allows everything what you want.
Use very carefully.
new FreeValue("Id:10");
Create a range query.
// Create a query like "Price:[1.5 TO 10.5]"
new RangeValue<MyDocument, decimal>(q => q.Price, 1.5M, 10.5M)
Create a single value query, this is the easier way to create queries.
// Create a query like City:"New York"
new SingleValue<MyDocument>(q => q.City, "New York");
Create a container to complex queries using AND or OR operators.
// Create a query like Price:[1.5 TO 10.5] AND City:"New York"
new MultiValue(SolrQueryConditionType.And, new RangeValue<MyDocument, decimal>(q => q.Price, 1.5M, 10.5M), new SingleValue<MyDocument>(q => q.City, "New York"));
// Create a query like (Price:[1.5 TO 10.5] AND City:"New York") OR Id:"XPTO"
new MultiValue(SolrQueryConditionType.Or,
new MultiValue(SolrQueryConditionType.And, new RangeValue<MyDocument, decimal>(q => q.Price, 1.5M, 10.5M), new SingleValue<MyDocument>(q => q.City, "New York")),
new SingleValue<MyDocument>(q => q.Id, "XPTO"));
Create a container to negate the queries.
// Create a query like -(Id:"Xpto")
new NegativeValue(new SingleValue<MyDocument>(q => q.Id, "XPTO"));
Allows parse Sorl result in a controlled and buildable way.
Parse the "documents" part of the Solr result in a list of MyDocument class.
new DocumentBuilder<MyDocument>();
Parse the "facet.field" part of the Solr result in a list of FacetKeyValue class.
new FacetFieldResultBuilder();
Parse the "facet.query" part of the Solr result in a instance of Dictionary<string, long>.
new FacetQueryResultBuilder();
Parse the "facet.range" part of the Solr result in a instance of List<FacetKeyValue>.
new FacetQueryResultBuilder();
Parse the "statistic" part of the Solr result in a several properties with Solr statistics.
new StatisticResultBuilder();
Allows use of fluent API to make the life easier and a beautiful code. To exemplify this, see the code below without and with the fluent api.
// Source without fluent API
using (var ctx = new SolrContext())
{
List<TechProduct> documents;
ctx.TechProducts
.Parameter(new QueryParameter(new QueryAll()))
.Parameter(new LimitParameter(3));
var result = ctx.TechProducts.Execute();
documents = result.Get(new DocumentBuilder<TechProduct>()).Data;
}
// Source with fluent API
using (var ctx = new SolrContext())
{
List<TechProduct> documents;
ctx.TechProducts
.Query(new QueryAll())
.Limit(3);
var result = ctx.TechProducts.Execute();
result.Document<TechProduct>(out documents);
}
Allows use of SolrFieldAttribute attribute and control "from-to" field name between Solr document and POCO class.
public class MyDocument : IDocument
{
[SolrFieldAttribute("Field_With_A_Name_Hosted_In_Solr_Document")]
public GeoCoordinate StoredAt { get; set; }
}
Allows throws exceptions in some cases and make unit tests easier to be created.
To do this, use the SolrFieldAttribute attribute in properties of the POCO than represents the Solr document.
public class MyDocument : IDocument
{
[SolrFieldAttribute("StoredAt", Indexed = true, Stored = true, OmitNorms = true)]
public GeoCoordinate StoredAt { get; set; }
}
Each property of the attribute is validate in different moments. For example, indexed=false throws exception if the referenced property was used in FieldsParameter.
To all use cases, see official wiki
To deactivate the fail fast feature (not recommended), when created the SolrQueryable, pass a configuration object in the constructor like the below code:
var provider = new Provider("http://localhost:8983/solr/techproducts");
var config = new SolrQueryConfiguration
{
FailFast = false
};
this.TechProducts = new SolrQueryable<TechProduct>(provider, config);
Step to step to use the framework:
- Create a class and implement the IDocument interface
public class MyDocument : IDocument
- Create a instance of SolrQueryable class. Set the Provider instance.
var myProvider = new Provider("http://localhost:8983/solr/mycollection");
var myDocuments = new SolrQueryable<MyDocument>(myProvider);
- Use parameters
// This will create a query like http://localhost:8983/solr/mycollection/query?q=*:*
myDocuments.Parameter(new QueryParameter(new QueryAll()));
- Execute the query
var queryResult = myDocuments.Execute();
- And get results
var documents = queryResult.Get(new DocumentBuilder<MyDocument>()).Data;
Tan dam!! Done!
All sorces of this example is available here
A fully implemented example is available here
This software is licensed in MIT License (MIT)