MongoDB + Node.js + AJAX solution for finding autocomplete

I am looking to implement a head / autocomplete type search for fun. I have several attributes in my schema in mongoDB, but I want to be able to search only by category, title, view or date.

This is my mongoDB schema for one article (I use mongoose as ORM):

{ title: { type: String, required: true} , preview: { type: String, required: true} , body: { type: String, required: true} , category: {type: String} , created_at: { type: Date, default: Date.now } } 

Every time I create, update or destroy, I have to re-index so that the search is updated. The search will be performed automatically, for example, when I have two articles called "Welcome to stackoverflow" and "How to avoid stackoverflow", respectively, and the user enters the key 't' , then I would show both articles using AJAX, since both have 't' in their names. I would also like to highlight each 't' ; 't' in 'to' , 't' in s 't' ackoverflow, which indicates that the request is missing something. (I expect this to be similar to when we look for specific β€œtags” here on stackoverflow.com)

Now the question is, should I use a different scheme for indexing, or just stick with my existing scheme? It seems that I will not use the "body" attribute, which contains the full article and contains thousands of words, as I am not looking for a full text search right now.

  • Heading attributes probably only have ~ 45 characters and 3 or 4 words on average.
  • Mostly categories are just 1 word with average 9-15 characters.
  • The preview will be the largest data set with ~ 150 characters and 20 words on average.

I would like to implement this using trie data structures. In my opinion, I would say that one way to do this is to make an AJAX request for each keystroke that will be directed to the node.js handler, and then from there make a request to mongoDB that will return every entry that has words, which have a letter that corresponds to a keystroke entered by the user as a JSON file. Then I will parse this JSON file and show each entry.

The question is, how would I put the trie algorithm in my plan? Another thing is that I need to rebuild the index every time I perform a CRUD operation.

I will thank any suggestions / pointers in the right direction or any articles that will help me do this. (I'm looking to do a best practice / performing way) Thank you. Let me know if a question needs to be clarified.

+8
javascript ajax mongodb trie
source share
1 answer

I do not think that the trick will work. Usually Trie works from the beginning of the line. Therefore, if you used trie to index the headers, the user typing 't' could only search for three headers starting with t. I believe the best way to use mongodb if you don't have a lot of text is to simply use regular expressions in combination with the $ or operator.

In the change event in the input text box, you need to make an AJAX request, as you said, to your node server, which issues a mongodb request and returns the results in a JSON array.

Regular expressions in mongo: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

$ or operator: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24or

Demonstration of how jQuery user interface handles autocompletion (for help on AJAX request and filling in values): http://jqueryui.com/demos/autocomplete/

+8
source share

All Articles