SolrNet advanced usage (paging, Facet query, any group), solrnetfacet

Source: Internet
Author: User

SolrNet advanced usage (paging, Facet query, any group), solrnetfacet
Preface

If Solr is used in the system, you will certainly encounter the need to reverse push data from Solr. After the index is produced based on the database data, the Solr index data is relatively accurate, in e-commerce demands, you will often encounter menus, navigation categories (such as computers, PCs, there will be many brands), second-hand car navigation will have a car brand. Custom query conditions are also made up based on the price range. Common advanced usage:

1. Back-push data based on basic data for navigation (Computer brand, mobile phone brand, vehicle brand ).

2. Paging is required for large data volumes.

3. Custom price ranges.

4. Group time segments.

5. highlight.

The following lists the three actual problems I encountered to demonstrate the actual usage of SolrNet.


Advanced query scenario 1 (Facet query)

If you select a city on a car-selling e-commerce website, you must only want to query Find the existing vehicle source brands in the city. In this way, the query conditions selected by the user are valid. The Facet Syntax of Solr is used here. I understand Facet as a shard or category query. In the returned FacetFields Field, the result of the Facet Field query lists the IDs of all brands and returns a KeyValue, in the result, all brands and the number of vehicle sources are returned. If the vehicle source quantity is greater than 0, the brand data corresponding to the region is used.



ISolrOperations<SolrQuotePriceCore> solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrQuotePriceCore>>();

            / / Find the city
            ISolrQuery q1 = new SolrQueryByField("provid", "540000"); //Province ID
            ISolrQuery q2 = new SolrQueryByField("cityid", "542400"); // City ID
            ISolrQuery q3 = new SolrQueryByField("selled", "1"); //Sold status

            IList<ISolrQuery> filter=new List<ISolrQuery>();
            filter.Add(q1);
            filter.Add(q2);
            filter.Add(q3);
            //grouping
            Var facet = new FacetParameters()
            {
                Queries = new[]
                {
                    New SolrFacetFieldQuery("brandid"),
                }
            };

            QueryOptions options=new QueryOptions();
            options.Facet = facet;

            SolrMultipleCriteriaQuery qtbo = new SolrMultipleCriteriaQuery(filter,"AND");

            SolrQueryResults<SolrQuotePriceCore> results = solr.Query(qtbo,options);

            Foreach (var f in results.FacetFields["brandId"])
            {
                If (f.Value > 0)
                    Console.WriteLine("{0}: {1}", f.Key, f.Value);
            }

 

Advanced query scenario 2 (any group)

The above is the car source brand, and the price is the same. You need to use the task group to query the car source data according to the specified price range. Query results of any group are traversed in the returned FacetQueries to change the keyvalue value.



ISolrOperations<SolrQuotePriceCore> solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrQuotePriceCore>>();

            ISolrQuery r1 = new SolrQueryByRange<decimal>("price", 0, 3m);
            ISolrQuery r2 = new SolrQueryByRange<decimal>("price", 3.01m, 5m);
            ISolrQuery r3 = new SolrQueryByRange<decimal>("price", 5.01m, 8m);
            ISolrQuery r4 = new SolrQueryByRange<decimal>("price", 8.01m, 10m);
            ISolrQuery r5 = new SolrQueryByRange<decimal>("price", 10.01m, 15m);
            ISolrQuery r6 = new SolrQueryByRange<decimal>("price", 15.01m, 20m);
            ISolrQuery r7 = new SolrQueryByRange<decimal>("price", 20.01m, 30m);
            ISolrQuery r8 = new SolrQueryByRange<decimal>("price", 30.01m, 50m);
            ISolrQuery r9 = new SolrQueryByRange<decimal>("price", 50.01m, 1000m);

            var facet = new FacetParameters()
            {
                Queries = new[]
                {
                    new SolrFacetQuery(r1),
                    new SolrFacetQuery(r2),
                    new SolrFacetQuery(r3),
                    new SolrFacetQuery(r4),
                    new SolrFacetQuery(r5),
                    new SolrFacetQuery(r6),
                    new SolrFacetQuery(r7),
                    new SolrFacetQuery(r8),
                    new SolrFacetQuery(r9)
                }
            };

            ISolrQuery q1 = new SolrQueryByField("provid", "540000");
            ISolrQuery q2 = new SolrQueryByField("cityid", "542400");
            ISolrQuery q3 = new SolrQueryByField("selled", "1");

            IList<ISolrQuery> filter = new List<ISolrQuery>();
            filter.Add(q1);
            filter.Add(q2);
            filter.Add(q3);

            QueryOptions options=new QueryOptions();
            options.Facet = facet;

            var qTBO = new SolrMultipleCriteriaQuery(filter, "AND");
            SolrQueryResults<SolrQuotePriceCore> results = solr.Query(qTBO, options);


            foreach (var f in results.FacetQueries)
            {
                Console.WriteLine("{0}: {1}", f.Key, f.Value);
            }

 

Advanced query scenario 3 (paging)
QueryOptions options = new QueryOptions();
//page parameter
options.Rows = pageNum; //Number of data
options.Start = start; //start item
/ / Stitch related query conditions

  //Execute the query
  SolrQueryResults<Product> results = solr.Query(qTBO, options);

/ / Get the total number of returned data and total and total number of pages for paging display,
Var total = results.NumFound;
Var pageCount = total / pageNum + 1; 

 

Summary

The interfaces AND classes ISolrQuery AND SolrQueryByField used for queries. Multiple query conditions require the construction of a SolrMultipleCriteriaQuery class AND the logical relationship (and or) between Query conditions ). Similar Facet, Sort, OrderBy, Highlight, and SpellCheck are all in QueryOptions.

 

References

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


Related Article

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.