Solrnet Advanced Usage (pagination, facet query, arbitrary grouping)

Source: Internet
Author: User
Tags solr

Objective

If you use SOLR in your system, then you will certainly encounter the need to reverse the data from SOLR, and after the production index based on the database data, the SOLR index data is relatively accurate, often encountered in the e-commerce needs of the menu, navigation classification (such as computer, PC will have many brands), New cars used car navigation will have a car brand. Custom query criteria are also composed according to the free combination of price ranges. Common advanced usage is as follows:

1, according to the basic data anti-push data classification for navigation (computer brand, mobile phone brand, car brand).

2, the amount of data is large to be paged.

3, custom price range.

4, time period group.

5, highlight.

The following is a list of three practical questions I have encountered to illustrate the actual usage of the solrnet.

Advanced Query Scenario One (Facet query)

In the selling car e-commerce website, you choose the designated city, then you must only want to query the conditions of the city has a car source brand , so that the user selected query conditions are valid. Here we use the facet syntax of SOLR, which I understand as a shard or a categorical query. The result of using the Facet field query is that in the Facetfields field of the return value, he lists all the brand's IDs and returns a keyvalue that returns all the brands and the corresponding number of vehicle sources. The number of vehicles with a quantity greater than 0 is the corresponding brand data in the region.

isolroperations<solrquotepricecore> SOLR = servicelocator.current.getinstance<isolroperations< Solrquotepricecore>>(); //Find a cityIsolrquery q1 =NewSolrquerybyfield ("Provid","540000"); Province ID isolrquery Q2=NewSolrquerybyfield ("Cityid","542400"); City ID isolrquery Q3=NewSolrquerybyfield ("selled","1"); State of the sale IList<ISolrQuery> filter=NewList<isolrquery>(); Filter.            ADD (Q1); Filter.            ADD (Q2); Filter. ADD (Q3);            //Grouping            varFacet =Newfacetparameters () {Queries=New[]                {                    NewSolrfacetfieldquery ("Brandid"),                    }            }; Queryoptions Options=Newqueryoptions (); Options. Facet=facet; Solrmultiplecriteriaquery Qtbo=NewSolrmultiplecriteriaquery (Filter, "and"); Solrqueryresults<SolrQuotePriceCore> results =SOLR.            Query (qtbo,options); foreach(varFinchResults. facetfields["Brandid"])            {                if(F.value >0) Console.WriteLine ("{0}: {1}", F.key, F.value); }

Advanced Query Scenario two (any grouping)

The above is the car source brand, the same price, you want to search according to the specified price range of vehicle source data will be used to task groups. Any grouped query results in the return value of the facetqueries, the traversal of the keyvalue value.

isolroperations<solrquotepricecore> SOLR = servicelocator.current.getinstance<isolroperations< Solrquotepricecore>>(); Isolrquery R1=Newsolrquerybyrange<decimal> (" Price",0, 3m); Isolrquery R2=Newsolrquerybyrange<decimal> (" Price",3.01m, 5m); Isolrquery R3=Newsolrquerybyrange<decimal> (" Price",5.01m, 8m); Isolrquery R4=Newsolrquerybyrange<decimal> (" Price",8.01m, 10m); Isolrquery R5=Newsolrquerybyrange<decimal> (" Price",10.01m, 15m); Isolrquery R6=Newsolrquerybyrange<decimal> (" Price",15.01m, 20m); Isolrquery R7=Newsolrquerybyrange<decimal> (" Price",20.01m, 30m); Isolrquery R8=Newsolrquerybyrange<decimal> (" Price",30.01m, 50m); Isolrquery R9=Newsolrquerybyrange<decimal> (" Price",50.01m, 1000m); varFacet =Newfacetparameters () {Queries=New[]                {                    NewSolrfacetquery (R1),Newsolrfacetquery (R2),NewSolrfacetquery (R3),Newsolrfacetquery (R4),NewSolrfacetquery (R5),NewSolrfacetquery (R6),NewSolrfacetquery (R7),NewSolrfacetquery (R8),Newsolrfacetquery (R9)}}; Isolrquery Q1=NewSolrquerybyfield ("Provid","540000"); Isolrquery Q2=NewSolrquerybyfield ("Cityid","542400"); Isolrquery Q3=NewSolrquerybyfield ("selled","1"); IList<ISolrQuery> filter =NewList<isolrquery>(); Filter.            ADD (Q1); Filter.            ADD (Q2); Filter.            ADD (Q3); Queryoptions Options=Newqueryoptions (); Options. Facet=facet; varQtbo =NewSolrmultiplecriteriaquery (Filter," and"); Solrqueryresults<SolrQuotePriceCore> results =SOLR.            Query (Qtbo, Options); foreach(varFinchresults. Facetqueries) {Console.WriteLine ("{0}: {1}", F.key, F.value); }

Advanced query Scenario three (paging)
Queryoptions options =Newqueryoptions ();//Paging ParametersOptions. Rows = Pagenum;//number of data barsOptions. start = start;//Start Item//stitching related query conditions//Execute Querysolrqueryresults<product> results =SOLR. Query (Qtbo, options);//get the total number of data returned and total and page count for pagination display,varTotal =results. Numfound;varPageCount = Total/pagenum +1;

Summarize

The interfaces and classes Isolrquery, Solrquerybyfield, and multiple query criteria required to construct a Solrmultiplecriteriaquery class and the logical relationship between the query criteria (and, or). Similar facets, Sort, Highlight, and spellcheck are all in queryoptions.

Resources

Http://www.cnblogs.com/zhangweizhong/p/5075277.html?utm_source=tuicool&utm_medium=referral

Solrnet Advanced Usage (pagination, facet query, arbitrary grouping)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.