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.