Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:59:14+00:00 2026-05-29T05:59:14+00:00

I’m looking to implement a typeahead/autocomplete search for fun. I have a few attributes

  • 0

I’m looking to implement a typeahead/autocomplete search for fun. I have a few attributes within my schema in mongoDB, but I want to be able to search only by category, title, preview, or date.

This is my mongoDB schema for a single article (I’m using 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 }
}

Each time I create, update, or destroy, I have to re-index so the search gets updated. The search will be autocompleted, such that for example, when I have two articles titled “Welcome to stackoverflow” and “How to avoid stackoverflow” respectively and the user types in a key 't' then I’d display both articles using AJAX since both have character 't' in their titles. i’d also like to highlight every single 't'; the 't' in 'to', 't' in s't'ackoverflow, indicating that the query hits something. (I expect that it will look similar to when we search for particular ‘tags’ here at stackoverflow.com)

The question now is should I use a different schema for indexing, or just stick to my existing schema? It seems that I wont be using ‘body’ attribute which contain the full article and has thousands of words in it since I’m not looking to do full text search right now.

  • Title attributes probably only have ~45 characters and 3 or 4 words in average.
  • Category mostly only 1 word with average 9-15 characters.
  • Preview would be the largest datasets with ~150 characters and 20 words in average.

I’d probably like to implement this using trie data structures. On top of my head, I would probably say that one way of doing this is by making AJAX request every keystroke that will be routed to node.js handler, and then from there making query to mongoDB that will return every entry that has words that has a letter that matches the keystroke typed in by the user as a JSON file. I will then parse that JSON file and display each entry.

The question then is how would I fit the trie algorithm into my plan? The other thing is that I need to rebuild the index each time i do CRUD operation.

Would appreciate any suggestion/pointers to the right direction or any articles that would help me do this. (I’m looking to do the best practice/performant way) Thanks. Let me know if the question needs to be clarified.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:59:15+00:00Added an answer on May 29, 2026 at 5:59 am

    I don’t think a trie will work. Trie’s usually operate from the beginning of a string. So if you used a trie to index your headlines, a user typing ‘t’ would only be able to search a trie for headlines that started with t. I think the best bet for using mongodb, unless you’ve got huge amounts of text, is simply to use regular expressions in conjunction with the $or operator.

    On the change event in a text input box, you’ll want to make an AJAX request, as you said, to your node server which will issue the query to mongodb and return 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

    A demo of how jQuery UI handles auto complete (for reference on the AJAX request and filling in values):
    http://jqueryui.com/demos/autocomplete/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.