I am the author of 16 books on artificial intelligence and the semantic web. I use this blog to share ideas and code snippets using my favorite languages: Clojure, Java, Haskell, Common Lisp, and Ruby.

Wednesday, November 11, 2009

MongoDB has good support for indexing and search, including prefix matching for AJAX completion lists

I have been spoiled by great support for indexing and search in relational databases (e.g., Sphinx, native search in PostgreSQL and MySQL, etc.)

I was pleased to discover, after a little bit of hacking this morning, how easy it is to do indexing and search using the MongoDB document-centered database. I have two common use cases for search, and MongoDB seems to handle both of them fairly well:

Search for words inside of text fields

Efficient word prefix search to support AJAX "suggest" style lists

My approach does require combining search results for multiple search terms in application code, but that is OK. Assuming the use of MongoRecord, here is a code snippet:

Please let me point you to an alternative that MongoDB might serve as well. As you restrict completion to prefixes of words you could keep all prefixes and their definite completions as hash. Using definite completions allows ajax completion to start efficiently from the first character with fast response times and a minimum of data to transfer. By the way you get rid of the input field, like http://bu4.taipudex.com/pinyin.htm , that implements a dictionary as dynamic menu, more http://taipudex.com

Hello Zawuni. I downloaded the Trie type TCL software in your links. Looks interesting enough but solves a very different problem that a MongoDB data store. Your Trie based system seems to be hand crafted, for a topic domain, for completion suggestions.

BTW, I usually delete user comments that mostly serve as an advertising link to the user's business. I am going to leave your comment in place right now, but if at some time in the future I delete your comment, you will know why.