понедельник, 23 июля 2012 г.

Simple technique for randomizing search results



 There have been some posts recently (like this one) where efficient and elegant solution is possible with a bit of help from probabilistic theory.
I tried to remember if I ever used it for solving tasks I faced with. Here is the one I'd like to tell you about.


Suppose you are writing the dating service and want different persons to be shown for subsequent calls of you main function - 'search for partners that suits me best'.

Imagine a man who looks for a girl and dislikes the ones presented on the first page. Then he scrolls to another chunk of data service provides. He can also launch this function on visiting the service next time.
There is a sence to present different results to him each time.

It is certainly not a big challenge when you have some kind of a session and can keep track of people shown and you system is not very large. As the system grows you can't afford for a number of reasons to store large amount of data per each request/person and|or the number of searching people is huge and processing time is important (records should be fetched from some permanent storage) and|or the search criteria can be changed by users then recording search items shown for each particular user can have a dramatic impact on performance. Tracking implies a lot of coding also.
Why not just keep the latest page number clicked by a user? Tha data in our storage system can be in some arranged form (as it was in my case), for example sorted by a second name, but we need samples from the entire set.

Formally, my task was the following:
There is a huge amount of data to search for. The search is performed with a set of criteria to match. This criteria are generally different for each call. No need for ranging the results. The ususal number of results per each call is hundreds to tens of thousands. The resulted items to be presented in chunks of a size of 20-50. The total number of items is known. The search process was performed by Lucene library with the custom results collector/listener - upon each matching criteria document its id was passed to that custom listener.


The main idea was to randomly substitute the items after filling in the needed 20-50 chunk with the first results.
Let N be total number of data items;
n - is a number of items which match search citeria;
h  - is a chunk size.
Then the probability of each item from n-matching set to be in the result chunk is h/n (h<n). If n were known it would be possible to solve the task in the following way:
1. Fill the result set with first h results.
2. Upon each new match replace one of h-results with probability of h/n (How to choose which element to subsitute is described later).

Unfortunately, we don't know the number n while searching. It is only known after search engine has walked over the entire index. One possible solution is to walk two times - the first walk is to calc n number. But having two index passes might be too expensive solution from performance point of view.
Another solution I developed was to estimate n based on the current rate of results.
If the engine has walked over D (out of N) items and there are d matches one can estimate n from a simple proportion d/D ~ n/N. n~d*N/D.
So n is dynamically changing over the search process. The probability of each new match to be presented in the resulted chunk is h/n=h*D/(d*N). If the matching data start to be densely located in the index - the estimated number of total results rises quickly and it tends to replace the items in the result chunk more often and vice versa.
This certainly works best for uniformly distributed data. If the data distribution in an index is very skew the probability of appearing in the result chunk for some items will also be skewed. For dating service it means some persons will have higher probabilty to be presented than the others.
The error of probabilty estimation will be higher for initial part of the index. The more we move to the end of the index the less the estimation error.  In practice, the data distribution appeared to be good enough to follow this way.
Another question is how to choose the item to substitute from the filled list of results.
The first, naive thought, was to choose index of element for replacement by generating uniformly distributed value from 0 to h each time replacement is needed.
In this case some items may be replaced multiple times and this contradicts to the estimation of probability to be in the list =n/N. In fact, if you do this the wrongly great part of the results will be from the first h matches.
The possible silution is to pick random value from [0, h-1] values without repetition. In this case each item will have equal probability to be replaced. If it runs out of index values for replacement - just start again as this won't harm the logic - the average number of replaces is n*h/n=h.
How to pick random value from [0, h-1] values without repetition? This is one of well-known "Google interview"-like questions and one can easily find how to solve it.












Комментариев нет:

Отправить комментарий