Jump to content

Dynamic Search Suggestions


Fmdpa

Recommended Posts

I am considering what I want in the search engine for my site, and I came to search suggestions. I could just insert the searched values into the database, and retrieve them via AJAX, but that would cause any queries to be inserted into the database, including nonsense. I could create a table to hold the user's ip and query, then retrieve the values if multiple people (maybe 10-15) had entered that same query. But that still wouldn't fix the issue of nonsense queries entirely, like 'asdf'. What would be the best way to do this?

Link to comment
Share on other sites

I'm sure i can remember downloading a free dictionary text file from somewhere. if you can do this, you then can compare nonsense queries values, with the dictionary file, and then if the word exist, add it to table, if not, ignore.
I just downloaded the file, and the zipped archive alone was 3.5MB. Don't you think opening and searching this file each time a search is preformed would bog down the server?
Link to comment
Share on other sites

Of course it will, you're searching through all words in the English language, right? Something like that isn't instantaneous, of course it will slow things down. There are faster ways than using a text file though, like adding all of those words to a database and then searching through the database. You could set up one table per first letter, or break it down even further to make the search set smaller.

Link to comment
Share on other sites

You must have downloaded the full dictionary listing, mine was commonly used words of just about 1mb.The adding to table is a good idea, as it is what i did in the end, as it gives more control, and obviously be quicker. from what i do remember when adding to table, you have to remove carriage return ("\r") and 'new line' ("\n") characters from word before inserting into the table.

Link to comment
Share on other sites

I actually decided to do it a different way. Since the search function is going to be for either blogs or photos that I'll individually post/upload, I'll just place a textarea on that page, and I'll keyword the blog post when I post it. Those keywords will be inserted into the database with the blog, but I also used the explode function to turn the keywords into an array. Then the array of keywords is sent through a foreach loop that check if that keyword (the current array element) exists in the suggested_keywords table. If not, it is inserted, otherwise it just continues. Then I used an AJAX request on keyup to search and display any keywords below the search box. If the person clicks on the keyword, it will automatically search for it. Here's where you can help me. I know this is crossing over into JS, but I just described the whole thing here. :) My search results are displayed in an unordered list (<ul>), so they really don't have anything to do with the form, except for a little JS/jQuery that I wrote to make the keyword be inserted and the form submitted. What I would like to do is for the user to be able to navigate up and down the list with the arrow keys (up and down), and use the enter button to insert the suggestion. How would I go about this?

Link to comment
Share on other sites

this does not seem all that complicated, unless i'm getting it totally wrong.you just restrict height of ul to fixed height, assign overflow-y:scroll;, then use onclick to take the innerHTML of anchor link and insert in to form field.but you could get the same result by just using a form select/option instead.

Link to comment
Share on other sites

Yes, I already have it so that the user can click on it, and it will perform that search, but I want the user to be able to navigate up or down the list with the up and down arrows on their keyboard. Yeah, I guess I could use a select menu, but it just doesn't look as good.To see what I mean, go to bing and just type in one letter. Notice how you can use the "down" arrow key to select it. The source code for that page isn't any help. :)

Link to comment
Share on other sites

it looks as though, the list is shown when the input has focus, the keypress() is used to detect if key 38 (up) or 40 (down) is pressed similar to$('#target').keypress(function(event) { if (event.keyCode == '13') {...} });where 13 equals the enter key on down arrow press the highlight goes from input straight to the ul lists first value, then every time the keypress highlights a new value it is transfered to the input which does not seem to lose focus.Soo good luck with this, its going to be a challenge.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...