GithubHelp home page GithubHelp logo

asears / solr-express Goto Github PK

View Code? Open in Web Editor NEW

This project forked from solr-express/solr-express

0.0 2.0 0.0 2.53 MB

A simple and lightweight query .NET library for Solr, in a controlled, buildable and fail fast way.

License: MIT License

C# 99.59% Batchfile 0.41%

solr-express's Introduction

Solr Express

A simple and lightweight query .NET library for Solr, in a controlled, buildable and fail fast way.

Available at NuGet

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

Index

  1. Compatibility
  2. Packages
  3. Features
    1. Parameters
    2. Queries
    3. Builders
    4. Fluent API
    5. Friendly Field Name
    6. Fail Fast
  4. Examples
    1. Basic use
    2. SearchUI
  5. License

Compatibility

.Net Framework 4.5 or higher

Packages

SolrExpress.Core

Core library with main logic and generic implementations

SolrExpress.Solr4

Solr 4 implementation, full compatibility with the Solr 4.9+.

Implementation using default query handler and mechanism provided by request parameters.

SolrExpress.Solr5

Solr 5 implementation, full compatibility with the Solr 5.3+.

Implementation using default query handler and mechanism provided by JSON Request API.

Features

1. Parameters

Allows send parameters to Sorl in a controlled and buildable way.

1.1. FacetFieldParameter

Create a facet field type parameter using the informed field name and sort type.

new FacetFieldParameter<MyDocument>(q => q.Id, SolrFacetSortType.CountDesc);

1.2. FacetQueryParameter

Create a facet query type parameter using the informed field alias, query class and sort type.

new FacetQueryParameter("Alias", new QueryAll(), SolrFacetSortType.CountDesc);

1.3. FacetRangeParameter

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);

1.4. FieldsParameter

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);

1.5. FilterParameter

Create a fields parameter (filter query in Solr 4) using the informed query class.

new FilterParameter(new SingleValue<MyDocument>(q => q.Id, "XPTO"));

1.6. LimitParameter

Create a limit parameter (rows in Solr 4) using the informed number.

new LimitParameter(50);

1.7. MinimumShouldMatchParameter

Create a minimum should match parameter using the informed expression.

new MinimumShouldMatchParameter("75%");

1.8. OffsetParameter

Create a offset parameter (start in Solr 4) using the informed number.

new OffsetParameter(50);

1.9. QueryFieldParameter

Create a query field parameter using the informed expression.

new QueryFieldParameter("Id^10 Name^5~2");

1.10. QueryParameter

Create a query parameter using the informed query class.

new QueryParameter(new SingleValue<MyDocument>(q => q.Id, "XPTO"));

1.11. SortParameter

Create a sort parameter using the informed expression and ascending type.

new SortParameter(q => q.Id, true);

1.12. SpatialFilterParameter

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);

2. Queries

Allows create simple or complex queries in a controlled, buildable and testable way.

2.1. QueryAll

Create a query to return all documents.

// Create a query like "*:*"
new QueryAll();

2.2. FreeValue

Create a free value query, this is weakest query class, because allows everything what you want.

Use very carefully.

new FreeValue("Id:10");

2.3. RangeValue

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)

2.4. SingleValue

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");

2.5. MultiValue

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"));

2.6. NegativeValue

Create a container to negate the queries.

// Create a query like -(Id:"Xpto")
new NegativeValue(new SingleValue<MyDocument>(q => q.Id, "XPTO"));

3. Builders

Allows parse Sorl result in a controlled and buildable way.

3.1. DocumentBuilder

Parse the "documents" part of the Solr result in a list of MyDocument class.

new DocumentBuilder<MyDocument>();

3.2. FacetFieldResultBuilder

Parse the "facet.field" part of the Solr result in a list of FacetKeyValue class.

new FacetFieldResultBuilder();

3.3. FacetQueryResultBuilder

Parse the "facet.query" part of the Solr result in a instance of Dictionary<string, long>.

new FacetQueryResultBuilder();

3.4. FacetRangeResultBuilder

Parse the "facet.range" part of the Solr result in a instance of List<FacetKeyValue>.

new FacetQueryResultBuilder();

3.5. StatisticResultBuilder

Parse the "statistic" part of the Solr result in a several properties with Solr statistics.

new StatisticResultBuilder();

4. Fluent API

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);
	}

5. Friendly field name

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; }
	}

6. Fail fast

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);

Examples

Basic use

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

SearchUI

A fully implemented example is available here

License

This software is licensed in MIT License (MIT)

solr-express's People

Contributors

diegobrum avatar lgtelles avatar

Watchers

Andrew Sears avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.