1

I'm looking for a basic search functionality with JavaScript.

The Scenario: The user enters a single or multiple words, and hit a button. JavaScript looks up in an array of strings for items that probably relates to the entered search sentence.

The only function I'm aware of right now is "string.search" which returns the position of the string you are searching for. This is good, but not for all cases. Here are a few examples the search function should cover

Let's assume I have the following string: "This is a good day" in my array. The following search terms should return true when tested against my search function.

  • Search Term 1: This a good day
  • Search Term 2: This day
  • Search Term 3: This was good
  • Search Term 4: good dy -the user made a typo-

So nothing particular or specific. Just a basic search functionality that predicts (at a low level, and language agnostic) if the search term related to the strings in the tested array.

Omar Abid
  • 15,753
  • 28
  • 77
  • 108
  • You mean something like that: http://nakajima.github.com/jquery-livesearch/ – PhD Jun 16 '12 at 23:17
  • 1
    Lots and lots of programming. Your "This was good" and "good dy" examples, and to a lesser degree "This day", are non-trivial. – Amadan Jun 16 '12 at 23:17
  • You may try searching for "javascript live search" – PhD Jun 16 '12 at 23:17
  • @Nupul Could you read the full question. That search plugin is even case sensitive. – Omar Abid Jun 16 '12 at 23:18
  • Why not ask Google to lend you their semantic search algorithm? `:)` – Fabrício Matté Jun 16 '12 at 23:18
  • @Amadan Yes, I'm aware of that. That's why I didn't venture to build my own script from the first while and thought about asking the community. I did some Google searches but I found nothing – Omar Abid Jun 16 '12 at 23:20
  • @AbidOmar - I asked something *like* the link...not sure if that's what you were looking for and hence the comment. Putting a '?' at the end of the URL would break it – PhD Jun 16 '12 at 23:20
  • @Sebas The case when user made a typo in their search term. – Omar Abid Jun 16 '12 at 23:21
  • @Nupul: Off topic, but a common fix to that is to leave a space, even if not typographically correct. http://example.com/ ? – Amadan Jun 16 '12 at 23:22
  • @AbidOmar - Yes I thought of that :) Anyways - for the sake of completeness and I can't edit my comment here's is probably something that you are looking for that will take in typos as well as any 'sequential' pattern that you specify - http://ejohn.org/apps/livesearch/ – PhD Jun 16 '12 at 23:24
  • So, you want to compensate for user typos (#4) and semantic verb times (#3)? I like how the title is "**Basic** search functionality" and you didn't even show any code in the question. Seriously, your best bet is going with Evert's answer (maybe using OR'ed instead of AND'ed and ordering results by relevance). – Fabrício Matté Jun 16 '12 at 23:26
  • @Nupul The script you mentioned satisfies my search criteria. You can post it as answer and I'll accept i. – Omar Abid Jun 16 '12 at 23:30
  • 1
    @FabrícioMatté Maybe we have different definition of "basic":). I'd think of taking language, history and other things into account as an advanced thing. – Omar Abid Jun 16 '12 at 23:31
  • I see. @Nupul's recommended plugin fits between our definitions of basic very well, also. – Fabrício Matté Jun 16 '12 at 23:36
  • @AbidOmar - your wish is my command :P Added the answer – PhD Jun 17 '12 at 04:16

3 Answers3

2

Was the last a typo for 'day'?

If not, you could simply split the search sentence, as well as the original string using the split() function.

Then you would iterate over the search words and for each make sure they appear in the source string. As soon as you don't find the word, you stop the search.

This is assuming that all the search words should be AND'ed, not OR'ed.

Does that help?

Evert
  • 93,428
  • 18
  • 118
  • 189
1

This is not as simple as one would think. We're talking fuzzy matching and Levenshtein distance / algorithm.

See this past question: Getting the closest string match

Community
  • 1
  • 1
Moin Zaman
  • 25,281
  • 6
  • 70
  • 74
1

I guess what you are looking for is a pattern matching based live search similar to finite-state-automata-like (FSA) searching:

This link shows an example that'll allow you to search case-insensitively:

Example: Array contains 'This is a good day'

Searching for any (or all) of the following is valid:

  • THis a Day
  • Thagd (Th is a g oo d day)
  • good dy -intended typo-

etc.

A case-sensitive (albeit not perfect FSA based) version can be found here There is also one by John Resig but I don't have a link to his demo but it'd be worth looking at - it's a javascript/jquery port of the first link I mentioned.

Hope this helps!

PhD
  • 11,202
  • 14
  • 64
  • 112