My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Facets  
Faceted search
Updated Dec 27, 2009 by mauricio...@gmail.com

Facets

SolrNet supports faceted searching.

There are basically three kinds of facet queries:

  1. querying by field
  2. date facets
  3. arbitrary facet queries

Facet queries are issued through the FacetQueries property of QueryOptions. Then the QueryOptions instance is passed to the server instance.

Querying by field

Querying by field is handled by the SolrFacetFieldQuery class. Results are available through the FacetFields property.

Example: print all categories sorted by popularity.

ISolrOperations<Document> solr = ...
var r = solr.Query(SolrQuery.All, new QueryOptions {
    Rows = 0,
    Facet = new FacetParameters {
        Queries = new[] {new SolrFacetFieldQuery("category")}
    }
});
foreach (var facet in r.FacetFields["category"]) {
  Console.WriteLine("{0}: {1}", facet.Key, facet.Value);
}

Date facets

Date facet queries create facets from date ranges. Sample code:

ISolrOperations<Product> solr = ...
ISolrQueryResults<Product> results = solr.Query(SolrQuery.All, new QueryOptions {
    Facet = new FacetParameters {
        Queries = new[] {
            new SolrFacetDateQuery("timestamp", new DateTime(2001, 1, 1).AddDays(-1) /* range start */, new DateTime(2001, 1, 1).AddMonths(1) /* range end */, "+1DAY" /* gap */) {
                HardEnd = true,
                Other = new[] {FacetDateOther.After, FacetDateOther.Before}
            },
        }
    }
});
DateFacetingResult dateFacetResult = results.FacetDates["timestamp"];
foreach (KeyValuePair<DateTime, int> dr in dateFacetResult.DateResults) {
    Console.WriteLine(dr.Key);
    Console.WriteLine(dr.Value);
}

Arbitrary facet queries

Arbitrary facet queries are handled by the SolrFacetQuery class. Results are available through the FacetQueries property.

Example: segregate items by price (less than $500 - more than $500)

ISolrOperations<Document> solr = ...
var lessThan500 = new SolrQueryByRange<decimal>("price", 0m, 500m);
var moreThan500 = new SolrQueryByRange<string>("price", "500", "*");
var r = solr.Query(SolrQuery.All, new QueryOptions {
    Rows = 0,
    Facet = new FacetParameters {
        Queries = new[] {new SolrFacetQuery(lessThan500), new SolrFacetQuery(moreThan500)}
    }
});
foreach (var facet in r.FacetQueries) {
  Console.WriteLine("{0}: {1}", facet.Key, facet.Value);
}

For an overview of faceted search with Solr, see SolrFacetingOverview.

Powered by Google Project Hosting