0
votes

I'm using Solr and Solrnet for the first time.

I have managed to create a schema, which has amongst them the following fields:

created_date DateTime
parent_id int
categories string multi-valued

I've been able to get a basic search working against the parent_id field, as so:

var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrNode>>();
            ICollection<SolrNode> results = solr.Query(new SolrQueryByField("parent_id", currentNode.Id.ToString()));

I've been able to figure out (kind of) how to return facets for all of my results, as so:

var solrFacet = ServiceLocator.Current.GetInstance<ISolrOperations<SolrNode>>();
            var r = solrFacet.Query(SolrQuery.All, new QueryOptions
            {
                Rows = 0,
                Facet = new FacetParameters
                {
                    Queries = new[] { 
                        //new SolrFacetDateQuery("created_date", DateTime.Now /* range start */, DateTime.Now.AddMonths(-6) /* range end */, "+1DAY" /* gap */) {
                        //    HardEnd = true,
                        //    Other = new[] {FacetDateOther.After, FacetDateOther.Before}
                        //}
                        new SolrFacetDateQuery("created_date", new DateTime(2011, 1, 1).AddDays(-1) /* range start */, new DateTime(2014, 1, 1).AddMonths(1) /* range end */, "+1MONTH" /* gap */) {
                            HardEnd = true,
                            Other = new[] {FacetDateOther.After, FacetDateOther.Before}
                        }
                        //,
                        //new SolrFacetFieldQuery("categories")
                    }, 
                }
            });
            //foreach (var facet in r.FacetFields["categories"])
            //{
            //    this.DebugLiteral.Text += string.Format("{0}: {1}{2}", facet.Key, facet.Value, "<br />");
            //}

            DateFacetingResult dateFacetResult = r.FacetDates["created_date"];
            foreach (KeyValuePair<DateTime, int> dr in dateFacetResult.DateResults)
            {
                this.DebugLiteral.Text += string.Format("{0}: {1}{2}", dr.Key, dr.Value, "<br />");
            }

But what I'm not able to figure out is how to plumb it all together. My requirements are as follows:

Page loads - show all search results where parent_id matches N. Query facets of search results and show tick boxes for the facets like so:

Categories

  • Category 1
  • Category 2

Within

  • Last week
  • Last month
  • Last 3 months
  • Last 6 months
  • All time

User clicks on relevant tick boxes and then code executes another solr query, passing in both the parent_id criteria, along with the facets the user has selected.

I realise in my description I have simplified the process, and perhaps it is quite a big question to ask on StackOverflow, so I'm of course not expecting a working example (although if you're bored pls feel free ;-)) but could anyone provide any pointers, or examples? SolrNet does have an MVC sample app, but I'm using WebForms and not particularly comfortable with MVC just yet.

Any help would be greatly appreciated.

Thanks in advance Al

2

2 Answers

0
votes

You can club the Query and add facets as Query Opertations

ISolrQueryResults<TestDocument> r = solr.Query("product_id:XXXXX", new QueryOptions {
    FacetQueries = new ISolrFacetQuery[] {
        new SolrFacetFieldQuery("category")
    }
});
0
votes

I create a 'fq' string of the facets selected. For instance, if the user selects these facets:

 united states

 california

 almond growers

and the facet field is 'content_type', I generate this query string:

(content_type:"united states" AND content_type:california  AND content_type:"almond growers")

Note the quotes and the open and close parenthesis...important! I store this in the variable named finalFacet. I then submit it to Solr like this, where sQuery is the text the user is searching on and finalFacet is the facet query string as shown above:

articles = solr.Query(sQuery, new QueryOptions
{
   ExtraParams = new Dictionary<string, string> {
   {"fq", finalFacet},
},
Facet = new FacetParameters
{
   Queries = new[] { new SolrFacetFieldQuery("content_type")}
},
Rows = 10
Start = 1,
});