Take the 2-minute tour ×

If I have a list like : List<NewsItems>, and there are 10 news items in this list, is there some way I can get a set of 4 different news items each time, for instance, say the 1st, 4th, 8th and 10th item in the list.

I am currently using Pageable Collections and breaking up the display of news items to show 4 news items ( max ) per page. So, in this way, I can randomly change the page to display ( thus ultimately changing the news at every refresh) , but it is not random per news item ( for eg : 1-4 items are always together , as are 5-8 ).

final HippoResultSetBean resultSet = featurednewsBean.getResultSet();
  if (resultSet != null && resultSet.getCount() > 0) {

    final HippoDocumentIterator<NewsDocument> facetIt = resultSet.getDocumentIterator(NewsDocument.class);
    final int facetCount = featurednewsBean.getCount().intValue();
     * @ Javadoc - Show news items(max 4) randomly at every page refresh.
     * FacetCount - No. of news items.
     * pageRandom - A randomly generated number for the current page, so that we display a different page containing news each time.
    int pageRandom = (int) (Math.random() * facetCount);
    //if(pageRandom<(facetCount/4)+1) {
    featurednews = new PageableCollection<NewsDocument>(facetIt, facetCount, DEFAULT_HEADLINES_MAX,pageRandom);

Is there a way that I can still use Pageable Collections and randomly get the news feed ? OR some other way? Any help is greatly appreciated.

share|improve this question
add comment

2 Answers

Hippo Facets are always ordered. Either on a property you define or on (Lucene) score. A PageableCollection gives you a List of documents given the page size and the offset. If your page size is 5 and you set the offset to 0, you get the first 5 items. Using a random offset will still give you a result set in the order they came out of the facet selection.

You can make it a bit more random when you increase the page size a little to 10 items and pick 4 "random" of them from featuredNews.getItems() (this is a java.util.List). Don't set the page size too high, this will decrease the performance.

Another option is to create 4 PageableCollection's with a page size of 1 and 4 different offsets if you want to choose from a larger result set.

share|improve this answer
add comment

Thank you for your answer Jasha ! Solr integration makes it much easier to sort randomly.

What I ended up doing(in the meanwhile, works fine) was, I took the list of news Items, randomly sorted them and picked 4 unique of the localised list, and rendered it to the JSP.

share|improve this answer
Yes the Solr integration will eventually solve your issue, but at the moment it's not yet in a released version. –  Jasha Jun 29 '12 at 19:07
add comment

Your Answer


