Saturday, March 08, 2014
Ping
Friday, March 08, 2013
Lone traveller
I settled myself besides the window and started looking at the lights from lamp posts outside. As the train slowly pulled away from the station, the lights went dimmer and dimmer and by the time the train had picked up speed, I was staring outside at the dark emptiness. I closed my eyes and the same set of questions cropped up again in my mind – Where am I headed? Why did I even board this train? Almost everyone who cared about me had called me out of my mind when I had said that I want to leave everything behind and start afresh, and for that I needed a little time off to do some soul searching, to find out what my higher purpose in this life was – the mountains seemed to be the best place for that. There is some sense of calm about the mountains, the sound of the river streams as they come down the mountains, that you just want to lose yourself in them. By the way, I didn’t understand when people said why I want to leave everything behind, heck I had nothing unless living an aimless life and going through the motions day-in day-out just to keep you afloat is considered everything. Yes, I had a well paying job; a plush house loaded with amenities but is that all that you need? Isn’t there more to life than things that money can buy?
It wasn’t an easy decision to just let go of a life that you are used to even if it is not worthwhile, we humans have the tendency to not come out of our comfort zone, we just hate changes even if they it is good for us. I took a long hard look at my life as it had panned out over last 5-8 years and didn’t find even a single moment that I cherished, a moment which made me smile – yes, there were good times and there were bad times and really really bad times but there was not even a single moment which brought a sense of satisfaction or inner peace. I think I had become like someone who is terminally ill, you don’t have any dreams, anything to look forward to, and you just live everyday as it comes, just trying to survive one more day. I knew I had to break out of that life as I don’t think I could have sustained that lifestyle any longer. And now that I have left my old life behind, I can only hope that the soul searching would lead me somewhere. Perhaps, I will love the mountains & the new life so much that I wouldn’t even want to return back (anyway you need to have something to return back to). Sometimes I do wonder though, if this soul searching is even needed? I already know what I need, it’s just that I am not lucky enough to have what I need. There is a saying – “you don’t always get what you want”, I guess in my case “I don’t ever get what I really want” would be fitting. Perhaps, I am indeed terminally ill.
Tuesday, May 22, 2012
MongoDB Tidbits - PHP, Mongo Collections and ODM
Once I decided that RDBMS was a no-go, I started looking out for alternatives in the NoSql world. I had tried playing with Cassandra and Hadoop/HDFS earlier but felt they had a steep learning curve. Also, I feel Cassandra and Hadoop/HDFS are more suitable for applications dealing with huge amount of data, given their distributed nature and complex processing (great support for Map-Reduce). I finally evaluated MongoDB, CouchDB and Redis – found Redis to be a glorified Key-Value store and MongoDB to be closest to a RDBMS (you can have indexes, dbrefs) without being a RDBMS, making it a little easier for people with RDBMS background to learn it.
- Embedding: A document becomes a subdocument of another document. You can embed as many levels deep as you wish. Warning – Embedding generally works great if you are embedding only up to one level as Mongo currently does not have good support for querying/updating attributes which are nested multiple levels deep – more on it in a subsequent post where-in I ran into the issue with $ operator.
That's pretty much it for our ODM
Thursday, March 08, 2012
Still searching...
I'll keep waiting for answers...there's nothing else for me to do, anyway.
Monday, December 05, 2011
DEL-ORD
Funny how, at times, life moves backwards and takes you to places you've been trying to run away from all this while. Perhaps, it's just life's way of reminding you of things that you've been missing and yearning for deep inside.
Tuesday, November 01, 2011
life
Monday, September 19, 2011
Windows Phone 7–A Review
It’s been more than 3 months now that I’ve been using a WP7 and I guess now I have enough idea about what I like about WP7 and what I don’t. This post is a mini-review on the review and I would not touch upon the hardware much. Also, I haven’t upgraded to Mango (WP 7.5) so I won’t talk about Mango much, though most of the stuff that I’ll be writing about would be applicable even for Mango.
Preamble – I’ve been a Windows Mobile user since quite a while and have had Windows Mobile powered devices since Windows Mobile 2003 SE. I still own quite a few Windows Mobile powered devices from Windows Mobile 5 to Windows Mobile 6.5. I have also played with iOS 4 and Android (2.3) powered devices.
The Likes -
Firstly the good things about WP7 – the OS is actually quite snappy and live tiles is a pretty cool and refreshingly different idea than almost all the other mobile devices out in the market. The lock screen on WP7 is the best with all the important stuff right there on the lock screen itself including missed calls, email/text notifications and calendar stuff. Native integration with the cloud and social networks (facebook, twitter, linkedIn) is pretty slick too.
The Dislikes or things that need to be better -
This is going be a much bigger list than the “likes” list – not because there are more things to dislike about this OS but because some of the things are simply annoying and make you feel like pulling out your hair
- No Outlook Backup - I have been used to syncing my older WM phones with Outlook – everything on my phone was backed up including Contacts, Tasks/Calendar using Outlook. I did try backing up on the cloud using Live Mesh but somehow was more comfortable with backing up to a local computer which I use daily. The first thing that I did when I got my new WP7 phone was to connect it to the computer and heck, figured out there is no Outlook sync – so there was no way for me to get my contacts back on the phone. Quite stupid given that Outlook is still the best personal manager (email, tasks, contacts and calendar) available and I use it almost daily. I don’t think it can get any more stupid that getting your contacts synced from Facebook/Google/Hotmail takes a matter of minutes but syncing with your own personal computer is impossible. So, I had to figure out a way to sync my Outlook contacts/calendar with the cloud – in the end I had to use GO Contact Sync to sync my contacts with my Google a/c so that I can get them on my phone.
- No Text Message Backup - The text messages on my WM 6.5 were backed up using MS MyPhone. In WP7 there is no way to backup text messages so one hard-reset and all your messages are lost forever!
- Look ma no paste – I know there is copy-paste support since NoDo but what I am talking about is the paste support in the dialer app. I don’t know what the devs who disabled this support were smoking but there have been numerous instances where the phone number on a website is not recognized by the OS and the only way to currently dial such number is to write it down somewhere (or use speech) and then dial it manually. Yes, there are 3rd party apps which let you paste a number, but seriously, why can’t the native dialer app have this?
- No Tasks Support – Guess fixed in Mango but really silly that there is no way to sync your tasks and there is no native task app (with reminders) in WP 7.0.
- Text Contact Details – If you want to share a contact’s details with somebody else on WP7 then you are quite out of luck – guess sharing contacts via text messages is not considered very social any longer – you should just share them on the cloud somewhere!
- International Assist – Perhaps I should have RTFMed before making a phone call but this feature gave me headaches when I tried reaching a local 1-800 number and the dialler kept on adding a + before the 1 (i.e. making it an international call). A frantic Google search made me realize that it’s a feature! I hate it when some apps try to outsmart the user. Ideally, the International Assist should be turned off by default and not turned on.
- Mail an attachment – The only attachment that you can send from the Pocket Outlook (or whatever it’s called) is of type pictures! I know I can send Office documents using the Office hub but seriously why I can only attach pictures from the mail client is beyond me.
- Half Baked Call History – The call history on WP7 is like one endless list of calls without any support for fitlers like Incoming, Outgoing and Missed. I can perhaps live without the filters but there is no way in the Call history to tap on a number and view the call history log just for that number. The call history by number is important when you do get a missed call from an unknown number and you want to check if you have received any other calls from that number or not.
- Lack of Customization/ General UI observation – Let’s face it, users love customizing their phones – customization is what makes a phone truly yours. Unfortunately, the only changes to the appearance that you can make is altering the accent color (from a list of predefined ones) and choosing a light v/s dark theme.
The live tiles home screen wastes a bit of real estate by showing the right arrow always. I have never clicked on the arrow ever and have always swiped to get to the app list – so not sure if that arrow is needed. Perhaps, the navigation arrow can be removed all together giving more horizontal space to the tiles. Maybe it’s just me but I somehow preferred the honeycomb UI of the 6.5 for the app list instead of an endless list (with jumplist in Mango) of WP7.
- Capacitive Hardware Buttons – Maybe I am nit-picking but there should be some way of at least disabling these buttons while you are playing a game like Fruit Ninja – so many times I’ve had the Bing Search open because I moved my finger accidently on the Search Icon while playing.
- Zune Now Playing Artist Info – The Zune Now playing integration thingie (where it pulls up artist information from the marketplace) has never worked for me even though I have an US Zune a/c with US mailing & billing address. Funnily, Zune has no issues in pulling artist information from marketplace on my computer.
That’s pretty much it – looking at the “dislikes list” it might look like that I have more to bitch about WP7 than what I like but the fact is I actually find WP7 to be quite slick and if the above inconveniences are fixed than it’s going to be quite a force to reckon with in the Mobile space.
* Image courtesy – Dell.
Saturday, July 16, 2011
Another (Elegant) Pairing Function
In my last post I talked about Cantor Pairing function for uniquely pairing two integers into a single number. Unfortunately, I ran into some overflow issues with that when generating pairs for very big integers like int.Max – luckily, I don’t have to deal with such large numbers in my application!
Anyway, in case you do have to deal with very large numbers and are looking at reducing them into a single number then there’s another pairing function called Elegant Pairing Function (warning pdf), which lets you reduce 2 non-negative integers to a single pair.
I did run into some issues with rounding off errors while generating the square root due to the precision of the double data type (Math.Sqrt takes a double) causing the Math.Ceil value to be one higher. Basically, even though the square-root of 1152921506754330623 (the paired value of int.Max & int.Max-1) is 1073741824.9999999990686774262519 (which after calling the Math.Floor would return back 1073741824), the Math.Sqrt returns it as 1073741825. Once I found out that the problem was with the Math.Sqrt, the fix was easy – given that flooring the Square-root of a number might result in rounding error by 1, if you just check for that condition where Square Of Floor Of Square Root Of Number > Number, and just decrement the Floor by 1, you can successfully calculate the elegant pair and reverse them for any non-negative integers up to Int.Max. Below is the C# code for the calculation of the Elegant Pair along with the reversal -
static ulong ElegantPair(uint x, uint y)
{
if (x >= y)
{
return (ulong)x * x + x + y;
}
else
{
return (ulong)y * y + x;
}
}
static uint[] ElegantReverse(ulong z)
{
uint[] pair = new uint[2];
double preciseZ = Math.Sqrt(z);
ulong floor = (ulong)Math.Floor(preciseZ);
if (floor * floor > z)
{
floor--;
}
ulong t = z - (ulong)(floor*floor);
if (t < floor)
{
pair[0] = (uint)t;
pair[1] = (uint)floor;
}
else
{
pair[0] = (uint)floor;
pair[1] = (uint)t - (uint)floor;
}
return pair;
}
Thursday, June 09, 2011
Cantor Pairing Function and Reversal
In my last post on Dice Coefficients I talked about how a nested NxN loop for finding similarity can be changed to a reducing inner loop since Similarity Score between X,Y is reversible i.e. Sim{X,Y} = Sim{Y,X}. This means if you have 26 sets in the universe (from A-Z), you would hit the same pair twice while calculating the Dice Coefficient between them. If we can somehow, store the similarity already calculated in the first iteration between two sets (say A,B), then we don’t have to re-calculate the similarity between (B,A). Hence, our nested loop can be rewritten as-
for (int i = 0; i < universe.length; i++) { for (int j = i + 1; j < universe.length; j++) { //calc similarity between set[i] & set[j] } }
By the way, for a very large value of N (universe.length), the average complexity of the above algorithm is still ~ O(N^2) but at least we have reduced the number of iterations by nearly half.
Now that we have a similarity score between two sets, we would need a mechanism to store this score and somehow tag it with the Pair for which the similarity is calculated. Also, it would be nice if the scores can be stored in a hashtable (as retrieval is on average O(1)) and make our hashtable key uniquely identify the pair for which the score is calculated. In short, we need some way to uniquely encode two docIds into a single number – enter "Cantor Pairing Function”. Pairing functions is a reversible process to uniquely encode two natural numbers into a single number. Calculating the “Cantor Pair” is quite easy but the documentation on the reversible process is a little convoluted. Anyway, below is the C# code for generating the unique number and then reversing it to get back the original numbers (for x,y>0).
static int CantorPair(short x, short y) { return ((x + y) * (x + y + 1)) / 2 + y; } static short[] Reverse(int z) { short[] pair = new short[2]; int t = (int)Math.Floor((-1D + Math.Sqrt(1D + 8 * z))/2D); int x = t * (t + 3) / 2 - z; int y = z - t * (t + 1) / 2; pair[0] = (short)x; pair[1] = (short)y; return pair; }
As you can see the CantorPair() returns back an int for two shorts to avoid any number overflows, if you are trying to generate the pair number for two ints, you would need to use long.
Saturday, June 04, 2011
Dice Coefficient–A naive Similarity engine using Set Theory
One of the ways to keep a user engaged on your site is to build some sort of recommendation engine – wherein the application automatically recommends similar “content” based on the user browsing history. One classic example of this kind of recommendation engine would be the one at amazon, which shows you a list of recommendations based on your browsing history. Another approach of doing a recommendation engine would be to suggest the user a list of “similar” content based on the current item that he’s viewing, for e.g., if you are on a online music store, and currently viewing “Dark side of the moon” album by Pink Floyd, then perhaps the application can show you list of albums similar to this for e.g.”Wish You Were Here” by Pink Floyd & “Meddle” by Led Zeppelin etc. The similarity between two items in these cases is generally calculated based on some attributes which define the items. Obviously, similarity is a relative term – so to find which items are more similar to a given item, we need to score them – the one with the higher score is “more similar” than the one with the lower score for a given item. Hence, there is a need to calculate a similarity score between two items.
We had a similar problem to solve on our site - build a similarity engine based on the categories that the items are associated with. Categories are nothing more than “tags”; so to speak; in the current web 2.0 semantics. One very naive approach to calculate similarity score between two items X, Y; which have tags Tx & Ty respectively would be to find the number of tags which are common in both, so the similarity score between (X,Y) would be -
Sim(X,Y) = |{Tx} ∩ {Ty}|
For e.g. if
{Tx} = {“music”, “rock”, “pink floyd”, “cult”} for X=”Dark Side of the moon”, where Tx = list of tags for that album.
and
{Ty} = {"music","led zeppelin","cult","rock"} for Y="Meddle", where Ty = list of tags for Meddle.
Then Sim(X,Y) = 3.
We can similarly, calculate the similarity between all the items N in our collection (if you notice it is a O(N^2) operation- more on optimizing this later) and then easily find the Top-K most similar items for a given item as generally, you would only be showing top 10 or so similar items. Below is the sample C# code snippet -
static int Similarity(IList<string> doc1Tags, IList<string> doc2Tags)
{
HashSet<string> tx = new HashSet<string>(doc1Tags);
HashSet<string> ty = new HashSet<string>(doc2Tags);
tx.IntersectWith(ty);
return tx.Count;
}
One big drawback with our above similarity engine is that it is heavily biased towards items with more number of tags – as items with more tags are more likely to have higher number of common elements. The other drawback is that there is no correlation between the similarity scores of two different items, it can be any integer, making it impossible to set any kind of threshold or find do cross-similarity analysis for e.g. you might want to limit the top-k similar documents to only documents which are really similar to reduce the noise documents or you might want to know if Doc A is more similar to Doc B than Doc C is to Doc D.
A better way of finding similarity would be to length normalize the similarity score, this way a. The score is not biased towards document with more tags & b. The similarity score is always between 0 & 1, so it’s easier to set the thresholds if required. So how do we length normalize, our scores? Below is one way of doing it, by dividing the intersection with |Tx| and |Ty|, where |Tx| = length of Tags for X. This gives us:
Sim(X,Y) = (2*|Tx ∩ Ty|)/(|Tx|+|Ty|)
This is what the Dice Coefficient is, a way of finding similarity measure between two sets. So lets change our earlier code to find Dice Coefficient:
static float Similarity(IList<string> doc1Tags, IList<string> doc2Tags)
{
HashSet<string> tx = new HashSet<string>(doc1Tags);
HashSet<string> ty = new HashSet<string>(doc2Tags);
int lenTx = tx.Count;
int lenTy = ty.Count;
tx.IntersectWith(ty);
float diceCoeff = (float)(2*tx.Count)/(float)(lenTx + lenTy);
return diceCoeff;
}
This is pretty much what we did on our project apart from few minor adjustments and optimizing the entire stuff so that we don’t iterate N^2 times (hint – Sim(X,Y) = Sim(Y,X). Also, I believe the F# sets are better equipped for this instead of the HashSet<T> class that is part of the BCL as F# sets are immutable i.e. when you do an intersection with another set, you get a completely new set instead of modifying the first set in-place – this is important when you are calculating similarity in a loop as you don’t want the original set to be touched. The other thing is to use a Heap structure for storing the Top-K documents for a given document instead of storing all the similar documents and then only picking the top K similar documents. Also, the loop for calculating similarity for a given document with other n documents; is a great candidate for parallelization using the TPL – pity we are still on 3.5! Have fun!
Sunday, May 29, 2011
ASP.Net Cache and updating the value using the Indexer
What happens if you update an object already in the cache using the indexer, something like -
HttpRuntime.Cache[“someKey”] = value; ?
A. The cached object is overwritten with the new value while retaining the other information like expiration (be it absolute or sliding) and cache dependencies.
B. The cache object is overwritten and the other cache information are reset to defaults.
The correct answer is B, calling the indexer on the cache object internally calls Cache.Insert(object) which passes default values for the CacheDependency, and uses default expiration policy (i.e. the object never expires unless the server is low on memory). So the next time, you find that your objects in cache are not honoring the cache TTLs or not getting evicted on dependency changes, make sure that you are not updating the object somewhere in the code using the indexer. I learnt it the hard-way while debugging the issue with a “forever-cached” object on our production site!
Sunday, May 15, 2011
hearbeat...
Anyway, I've been working on couple of "different" stuff off-late. On our ASP.Net website we've be using the built in In-Proc Cache to cache our data-objects and when you have a pretty decent cluster size, you run into data-consistence issue apart from having to figure out how to purge caches which are not scattered all over your web-farm. I've been thinking of getting some centralized caching engine for a while and after giving MS Velocity (or whatever it's called now) & Memcached a trial, I've decided to go with memcached along with the enyim client library. We haven't rolled it out on production yet, but in our Dev environment things actually look quite good - no exceptions, good Cache-hit rates. I'll post the entire experience with memcached and integrating with asp.net in a separate blog.
The other thing that technically, I've been looking into is reverse-proxying our web servers. I compared both Squid & Varnish, and zeroed in on Varnish based on the online material available. We already have Varnish set-up before our solr-slaves (because it's quite easy to configure varnish to do that and you get about 40-50% reduction in response times) on production, and we haven't seen any issues with it. Our Varnish cache-hit rates have been actually quite low ~around 30%, which is something that we need to look at - cause to benefit from a reverse-proxy, you would want to have your cache-hits to be around 60-70% on an average. One drawback with Varnish is the documentation available on VCL (Varnish Configuration Language) but I guess if you are clear about your caching strategy, it's not that hard to write one.
Varnishing solr is actually a piece of cake (as you don't have to worry about anonymous v/s authenticated users, session cookies, persistent cookies etc) but when it comes to Varnishing your public web-servers, things can get quite tricky. I'll post in detail, the experiences & things that I learned while Varnishing our web front-ends in another post. By the way, we've been running Varnish before the IIS on our dev environment and things have been looking quite alright so far.
One last thing on the technical stuff - we've been monitoring the performances of our memcached & varnish using munin - more on this in some other post as usual. I know this is lot of Linux stuff for a .net shop but the fact is - I didn't find any good Windows alternative for the above stuff which was free and proved itself to be quite scalable.
Moving on - as some of you would know, photography has been a hobby & a stress-reliever for me. On the photography front - after quite a bit of contemplation I got myself the Cactus triggers so that I can move my Vivitar 285HV off-camera. Well, so far they have been working like a charm. The first shot that I tried; armed with my new off-camera flash capabilities was freezing the water splash - and after 54 shots in the darkness, I did get on keeper -
| lemonade! |
I'll post about the setup and how I went about this in another post. The other news on the photography front is that I did manage to sell on of my images on fotolia (no I am not a millionaire yet!).
Lastly, I have started playing the guitar again - it's been so many years since I last strummed so it's like I am learning anew!
Well, that pretty much sums up what I've been up to all this while, hopefully, I'll be a little bit more frequent with my posting habits.
Wednesday, August 18, 2010
View On Black flickr bookmarklet
I’ll admit, I’m a fan of flickr and upload all of my pictures over there. One thing that I don’t like about flickr though is the default white background which unfortunately can make the photos look a little dull. So, generally I would include a link in the description to http://www.bighugelabs.com/onblack.php so that the viewer have an option to view the photos on black. Unfortunately, this meant that every time I upload a photo, I had to manually copy over the link to bighugelabs into the description. Since I am a sucker for redoing same stuff over and over again, I decided to automate the entire process and ended up writing a simple javascript bookmarklet. Now all I need to do is to navigate to my photo details page and just click on the bookmarklet and voila, the link to “onblack” version is automatically added in the description. Below is the bookmarklet just in case you are also a fan of “onblack”:
The CHANGE-THIS should be changed to your flickr's NID or your flickr friendly Url i.e. whatever you see in the address bar of your flickr photostream for e.g. if your flickr photo url is http://www.flickr.com/photos/xyz you should replace CHANGE-THIS to xyz. Do keep in mind that this bookmarklet will replace whatever is there in the description field with just a link to “OnBlack” version with the text “VIew On Black”. Also, I have only tried this on Firefox and it works in almost all the scenarios that I tried.
Sunday, May 16, 2010
Contextual spelling suggestions using solr SpellCheckComponent
Assume that the user types in "chicen fayita sadwich" and you have just two documents in your index:
Doc 1 ==> chicken fajita
Doc 2==> veg sandwich
Now, since solr/lucene treat the terms in isolation what you get in spellcheck.collation is:
"chicken fajita sandwich". Unfortunately, if you have a AND search and the user does click on this "Did you mean" link, it would result in a query (chicken AND fajita AND sandwich) resulting in zero results.
So, how do you solve this? One way to solve this is to use Shingles for creating your "spelling corpus", the only trouble with Shingles is that the number of suggestions generated is bound by the "MaxShinglesSize" and thus if you set your MaxShingleSize to say 4, you only get suggestions up to 4 terms.
Another cleaner albeit slightly slower approach is to extend the solr.SpellCheckComponent, and fire another solr query on the collation itself; if the suggestions' results are greater than 0 (or some other threshold), you return the collation back else try with the second set of suggestion (or just blank out the collation in case you don't want to fire multiple queries). This is what I did (though I am not 100% sure if this is the right way of firing solr queries): extend Solr.SpellCheckComponent, & hook onto the processRequest method to get the handle of ResponseBuilder object (so that you can then get SolrRequest and SolrSearcher objects from this). Override toNamedList method and in that get the collation string, fire another solr query using SolrSearcher and check the results' count; if the suggestion.Count > originalQuery.Count * THRESHOLD, let the collation be as is else blank it out. The guts of this is the overridden toNamedList method, which is below in case somebody is interested:
protected NamedList toNamedList(SpellingResult spellingResult, String
origQuery,
boolean extendedResults, boolean collate)
{
NamedList result = super.toNamedList(spellingResult, origQuery,
extendedResults, collate);
if(collate){
String collation = (String) result.get("collation");
if(collation!=null && collation.length() > 0 && builder!=null){
//fire a query and get the results
try {
//only add spelling suggestion in case results are less than
some threshold
int hits = builder.getResults().docList.matches();
if(hits>MIN_THRESHOLD){
result.remove("collation");
//result.add("collation", "");
return result;
}
SolrIndexSearcher searcher = builder.req.getSearcher();
QParser qp = QParser.getParser(collation, "dismax",
builder.req);
NamedList params = new NamedList();
params.add("rows", 0);
params.add("omitHeader","true");
SolrParams localParams = SolrParams.toSolrParams(params);
qp.setLocalParams(localParams);
Query q = qp.getQuery();
TopDocs docs = searcher.search(q, 1);
int suggestionHits = docs.totalHits;
//try to get hits for this query
log.info("current hits:" + hits);
log.info("total number of hits:" + suggestionHits);
if(suggestionHits <= hits*MULTIPLIER){
//remove the collation
result.remove("collation");
//result.add("collation", "");
}
} catch (IOException e) {
log.error(e.toString());
}
catch (ParseException e) {
log.error(e.toString());
}
}
}
return result;
}
Tuesday, March 30, 2010
Implementing Autosuggest in ASP.Net using WCF Rest service, JQuery and solr
Part I: Setting up solr index
Since, we already use solr 1.4 and I am not big fan of making database do anything more than just persist/retrieve data, I knew I would rather write my autosuggest component utilizing solr.
As far as I know, there are 3 ways (apart from wild card search) of achieving autosuggest using solr 1.4:
1. Use EdgeNGrams
2. Use shingles and prefix query.
3. Use the new Terms component (in solr 1.4, the terms component does not have the regex support, which means it can only do a "begins with" match).
After a bit of contemplation and research, I decided to go with using NGrams. Basically, the decision was made by the process of elimination:
a. shingles & prefix query: I know AOL does this way but unfortunately there's very little documentation online around shingles and how you go about using it; I figured; I would be groping in the dark if I went ahead with this.
b. Terms component: Even after applying the regex patch from JIRA, I felt there is lot more sanitizing of input that needs to be done (things like if the user types in multiple terms like: "joan of", I need to ensure that I replace the whitespace with \s etc). I somehow felt it's easy to break this in case a user types in slightly odd search terms.
Having decided on going the EdgeNGrams route, all I needed was two fields in schema file. Here's how the solr field definition for the "suggest" field looks like:
<fieldtype name="suggest" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.EdgeNGramFilterFactory"
minGramSize="2" maxGramSize="50" />
</analyzer>
<analyzer type="query">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
The suggest field holds the NGrams, whereas the query field stores the query verbatim (apart from lowercasing it). This allows us to return the query back to the user in the suggestion list based on what he typed.
Part II: Building the index
Part I of the problem solved, now we have the solr index which would generate the NGrams based on the query that it needs to index and also store the query that would be returned as part of the suggestion list.
Now, we need to figure out how do we build this index i.e. how we get the user typed in queries into this index. Instead of indexing each and every query the moment the user types it in, I implemented a simple lazy writer (in fact two levels of lazy writer). Every time the user types in a query which return results, I store that in a cached dictionary. I hook on to CacheItemRemovedCallback and persist the aggregated queries into a database table, the db table also holds a DateModified timestamp column which is updated to getdate() for newly added items. We then have a offline task which runs on a nightly basis and indexes all the newest queries to the solr index. Part II solved, now we have the solr index built and ready to serve "suggestions".
Part III: gluing it with the presentation layer
There are two bits to the presentation layer:
a. Hooking the solr index to get back the suggestion list as JSON.
b. The presentation itself aka UX.
Since, we already use JQuery on our project I decided to go with Autocomplete plugin for UX. The plugin again lacks in-depth documentation but it wasn't very hard to get it working. Instead of calling the solr index directly from the autocomplete plugin (which would have the serious after-effect of exposing our solr endpoint to the public and also adding solr dependency to the UI layer), I decided to write a WCF REST shim around the solr index. Adding a shim/decorator on top of the solr also gave me a place to intercept the calls to the solr and sanitize the user input (basically escaping the Lucene special characters) and do some heavy duty caching. That's where I had a problem: WCF services in 3.5 don't have caching support built in, you need to install the REST Starter Kit to get the caching support but once I had it installed I was able to get caching working with my WCF REST service.
After thoughts
We haven't rolled out this implementation yet, so I don't know in terms of performance how it's gonna go but at least during my limited testing, I have been able to get quite fast response times. By the way, SOLR-1316 seems quite promising too and I've been watching that thread with quite some interest: it talks about implementing the terms component as a ternary search tree so that should be quite fast.
Tuesday, March 23, 2010
solr and indexing Chinese text
Thursday, March 11, 2010
oops, something when wrong...
Well, maybe that's what beta means these days...
Sunday, September 13, 2009
fly, fly away
Wednesday, August 19, 2009
just my imagination
Saturday, July 11, 2009
Photography tidbits
One of the first thing that bites you if you're into photography is what subject to photograph? Do you just take pictures of the same ole' flowers, monuments or do you try to be different. The problem with trying to being different is finding out all those things which haven't been captured before. So, you hunt for those places/things (either mentally or physically) and then give up and in the end take the pictures of the same subjects which have been taken umpteen times before (butterfly on a flower, a flower petal, sunsets etc). You still believe that at least your photographs offer something different, maybe, a new perspective, a new *vision* (basically, all the things that you try to convince yourself with). Then, you get into some equipment acquisition syndrome and dream of owning the best in the class (read the most expensive); Zeiss & Leica lenses, Nikon D3X, Hasselblad: if something is so freaking expensive it gotta be good, right?
I am one of the 3 people in my country (the other two haven't been vociferous about it yet) who owns a 4/3rds system (a Olympus e-510), and honestly apart from a tiny-winy viewfinder; I haven't had too much to complain about the camera (I generally take all the blame for out of focus, horrifically shaky pics). Quite a few people have asked me why I invested into a 4/3rds system; well; just like my any other investment it wasn't something that I carefully thought about but was just an impulsive buy (I didn't even try it or any other brand before buying). It's a funny thing that I always believed I don't need a dSLR and was happy with my P&S (a Canon A95) till I decided to go for a trip to Corbett National Park where I had an opportunity to capture a tiger in the wild for the first time and boy, did I manage to capture it with a 3x optical zoom of Canon! I think sometimes you just need a better equipment to bail yourself out. Anyway, has the dSLR made me a better photographer than before? No, definitely not but it does allow me to experiment a bit more and push my own creative limits…it reminds me of a saying that I read long ago:
The difference between a picture and painting is that
a picture is a frozen captured moment of true life,
whereas a painting can be anything imagined in your mind.
..... someday, all these paintings will become pictures.

