Random Record as a Solr Search

615 views
Skip to first unread message

Hema Chaudhry

unread,
Feb 25, 2012, 12:58:27 PM2/25/12
to SolrNet
Hi,

I have few categories under which I have products. Now I need to fetch
one random product of any category on every page refresh. How can get
this type of data back from SolrNet query call through my c#(.net)
program.However I know that SOLR is a search engine and not a rules
engine but any way by which I can get the data as mentioned above.

Please let me know for more details around the same.

Any response will be much appreciated.

Thanks in advance.

Thanks,
Hema Chaudhry

Mauricio Scheffer

unread,
Feb 25, 2012, 4:38:49 PM2/25/12
to sol...@googlegroups.com
Perhaps a random sort can help:  http://code.google.com/p/solrnet/wiki/Querying#Sorting 

Cheers,
Mauricio




--
You received this message because you are subscribed to the Google Groups "SolrNet" group.
To post to this group, send email to sol...@googlegroups.com.
To unsubscribe from this group, send email to solrnet+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/solrnet?hl=en.


Vince

unread,
Feb 26, 2012, 3:06:29 PM2/26/12
to SolrNet
Do it in 2 calls, your first returns your first 10 records that the
user matches. Your second will call your random search from solr (i
would add a search stem in the solr config file), introduce a random
field on your index, an call one record from it. Once you have that
records add it to your List<T> or IEnumerable<T>, this if done
properly happens so fast the delay on your request to your user would
be very minimal (milliseconds) and my guess won't be very difficult to
code.

Vince

Hema Chaudhry

unread,
Feb 27, 2012, 12:29:45 AM2/27/12
to SolrNet
Thanks for your response, below is my understanding from your response
please validate:

1. Ist Solr call will fetch 10 records
2. I need to take one random record of these 10 record
3. IInd solr call will get the details for the selected random record
from second step.

Now my question is in Step 1 how will 1 choose those random 10 records
to get one out of them?
Will it be feasible to fetch all the records for the first step to get
one random record?

Please correct if I am wrong.

Thanks,
Hema Chaudhry
> > Hema Chaudhry- Hide quoted text -
>
> - Show quoted text -

Vince

unread,
Feb 28, 2012, 3:03:28 PM2/28/12
to SolrNet
Hi,

sorry for delay had some major fires to put out.

once the user performs the search you cold use

List<string> example = solr(..return..); //this is where solrnet
returns the records

Random random = new Random(); //create a random
int randomNumber = random.Next(1, example.count()); // this would
return a random number between 1 and the total records in your return,
from the List<> get the record of that random number

example.Skip(randomNumber).Take(1).First(); //get the random record
from the search

The above would be if you want to get a random record from a search
you have already performed.

the second solution is the add a random field in solr, that is also
really easy.

in your solr schema.xml file add

<dynamicField name="random*" type="random" indexed="true"
stored="false" />

and see http://lucene.apache.org/solr/api/org/apache/solr/schema/RandomSortField.html

or have SolrNet do it for you, Mauricio pasted the link above for it.

gswartz

unread,
Dec 17, 2012, 3:07:25 PM12/17/12
to sol...@googlegroups.com
If you add the dynamicField to your schema.xml do you then have to recreate your index or will it work right away?  Thanks.

Paige Cook

unread,
Dec 18, 2012, 7:58:45 AM12/18/12
to sol...@googlegroups.com
You will need to recreate your index to have the dynamicField present for all items in the index.The dynamicField will only appear for index items that are added/created after the field was added to the schema (and Solr was restarted).

gswartz

unread,
Dec 18, 2012, 8:32:29 AM12/18/12
to sol...@googlegroups.com
Thank you Paige, that worked great!
Reply all
Reply to author
Forward
0 new messages