Apache SOLR facet grouping Query

Source: Internet
Author: User
Tags apache solr solr

This section describes how to query SOLR groups:

Public static Map <string, integer> querybygroup (string qstr, string groupfield, string sortfield, Boolean ASC, integer pagesize, integer pagenum ){
Map <string, integer> RMAP = new linkedhashmap <string, integer> ();
Try {
Solrserver Server = getsolrserver (); // The getsolrserver () method returns a commonshttpsolrserver
Solrquery query = new solrquery ();
If (qstr! = NULL & qstr. Length ()> 0)
Query. setquery (qstr );
Else
Query. setquery ("*: *"); // if no query statement exists, it must be written in this way. Otherwise, an exception is reported.
Query. setshortdescore (false); // determines whether to sort by the number of groups.
Query. setfacet (true); // whether to query by group
Query. setrows (0); // you can specify the number of returned results. If you query results by group, set the value to 0.
Query. addfacetfield (groupfield); // Add a group field
Query. setfacetsort (true); // whether the group is sorted
Query. setfacetlimit (pagesize); // limit the number of results returned each time
Query. setsortfield (sortfield, ASC? Solrquery. Order. ASC: solrquery. Order. DESC); // group sorting Field
Query. Set (facetparams. facet_offset, (pageNum-1) * pagesize); // the starting position of the current result
Queryresponse RSp = server. Query (query );

List <count> countlist = RSP. getfacetfield (groupfield). getvalues ();
List <count> returnlist = new arraylist <count> ();
If (pagenum * pagesize <countlist. Size ())
Returnlist = countlist. sublist (pageNum-1) * pagesize, pagenum * pagesize );
Else
Returnlist = countlist. sublist (pageNum-1) * pagesize, countlist. Size ()-1 );

For (count: returnlist ){
If (count. getcount ()> 0)
RMAP. Put (count. getname (), (INT) count. getcount ());
}
} Catch (exception e ){
E. printstacktrace ();
}
Return RMAP;
}

 

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.