CodeIgniter Forums

Full Version: Simple search engine (POST or URI?)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mvdg27[/eluser]
Hi there,

On my website I'd like to implement a simple search engine. All very basic. Creating the controller to search the database and display the results, shouldn't be a problem.

I do however would like some input on how to pass the search query to my controller. As far as I can see I have two different options:

1) Use post data, which off course will be be available as de search query in my controller
2) Append the search query to my url, as such: "domains.com/search/my search query"

The first options probably is the easiest, the second one looks very interesting as the query is part of the URL, so it can be bookmarked, copy pasted etc.

The second option does cause some problems with disallowed charachters. Several characters (+ signs, accents, quotes, ampersands, commas etc.) are not allowed by default, but could very well be part of a user's search query ..

Does anyone have any advice on how to handle this?

Thanks! -Michiel

El Forum

[eluser]Bramme[/eluser]
I personally prefer the second method, for the same reasons you mentioned: bookmarking, c/p...

In my app, I created a method that handled the actual search action (the post), i processed the query with url_title and then redirected to a new uri with that string attached to it. This provides a nice clean url and you have everything you need: the search words.

You can then decide if you want to search word by word (explode the string with the - ) or use the entire string to perform your search (replacing - with " ").

Mind you, there's probably some downsides to my method, but it's all I needed.

El Forum

[eluser]Thorpe Obazee[/eluser]
I usually go with option 2.

controller/method/keyword/page

El Forum

[eluser]mvdg27[/eluser]
I think that's a nice idea, using the post variable to do the actual searching, and redirect to a results-page with the search words included in the url. Best of both worlds, I guess! I'll go with that option!

As I was working on the actual search controller, I came across the following issue. If someone enters multiple keywords, would you search for:

1) the complete, exact search query
2) any record that has either keywords
3) any record that has all keywords

I'm in doubt of returning option 2 or option 3 .. what makes more sense, you guys think?

Thanks for the advice, so far!

Cheers, Michiel

El Forum

[eluser]Bramme[/eluser]
I'd go for option three, as it would generally be the best.

El Forum

[eluser]ehicks727[/eluser]
Quote:As I was working on the actual search controller, I came across the following issue. If someone enters multiple keywords, would you search for:

1) the complete, exact search query
2) any record that has either keywords
3) any record that has all keywords

Ideally, you'd want to rank your search results in order of relevance. You'll have to define relevance yourself because that could mean...

1. The result with the most occurrence of the keyword(s)
2. The result with the most occurrence + proximity ("keyword1 keyword2" vs. "keyword1 blah blah keyword2")


Have you tried searching for an already coded search library? Do you know that you can customize a Google search just for your domain? Why re-invent the wheel?