Anne van Kesteren gave me some code to make intelligent searches inside WordPress and I wrapped it up in a nice install tutorial. Some of the features of this hack includes
-text based “fuzzy” searches within WordPress
-a nice Google like interface which is very customizeable
-search results with decreasing relevance to search topic
-trap for words which are less than four letters long, the search then gets routed through the default WP search
-“Next results” button which shows the next 10 (or less) results in relevance
-very simple installation with no effect on future upgrades
Test install is on this blog. Simply search for something from the form on the right.
Please post suggestions/bugs.
Download the zip file here: http://weblogtoolscollection.com/b2-img/search.zip
Does this search hack search the text of the excerpt, or just the main contents of the post?
Right now it only works with the post title and post content and not the excerpt.
I wrapped my site design around the code and made a new search.php file. The search results don’t span from the left to the right within the div, as I would like them to. Complete newbie question:
How do I get the search results to go completely across the center content div?
FYI: You should change line 149 of search.php from “search-beta.php” to “search.php”.
I will make that change.
As for the CSS, take a look at the source of my search page (or any single post page). The menu is suppressed.
Yes, I thought I had that, too. Take a look at this page on my site. You can see how the content isn’t filling up the center div. What did I do wrong? You can view the code I have in the search.php file here.
Apparently, the href’s got stripped out of that last comment. Here they are:
This plugin is great 🙂
However, I might be asking much, but is there ANY way to include PostMeta tags in the search page?
I’ve sort of included them, but the values don’t “spit out”
http://www.noranja.com/search.php?q=air
(that page is still much under construction) 😉
BTW – don’t hate me ’cause I use tables 😛
And, is there any reason why it wont show any results if I search “Nike”, but it will if I search “Air” keeping in mind that I have to posts with the words “Nike Air” in their titles… ?
the script is great. but it doesn’t highlight the search keywords in the results?
There is no search word highlighting because words are not searched for as a whole, there is some fuzzy matching going on.
Just installed the search engine. I get this error:
Database error: [Can’t find FULLTEXT index matching the column list]
SELECT ID,post_title,post_content,post_excerpt,post_date, MATCH(post_title,post_content) AGAINST (‘united forest’) AS score FROM wp_posts WHERE MATCH (post_title,post_content) AGAINST (‘united forest’) LIMIT 0,11
No results.
Did you run search_upgrade.php that is included in the zip file?
I’ve used this before, and it worked great, but this time something’s wrong. I get no results for searches over three characters – four or more, it tells me I have no search results. COuld this be a result of using WordPress 1.2.1?
Thanks
I am not sure where it is breaking because your site seems to be searching just fine.
Thisis a test
I just tried out this search on a local install of WP 1.2.2, but decided that it wasn’t what I want and went back to using the normal search (i.e., I changed the searchform action back to the default and deleted the files from my directory).
I was able to create a new post after the change, but it wouldn’t let me edit it (changes wouldn’t be saved), and when I tried to edit another post, I got the following error messages on top of my header image:
Database error: [Can't open file: 'wp_posts.MYD'. (errno: 145)]
SELECT DISTINCT * FROM wp_posts WHERE 1=1 AND ID = 24 AND post_date_gmt < = '2005-01-14 15:54:59' AND (post_status = "publish") GROUP BY wp_posts.ID ORDER BY post_date DESC Database error: [Can't open file: 'wp_posts.MYD'. (errno: 145)] SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM wp_posts WHERE post_date < '2005-01-14 16:54:26' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC
and a "Sorry, no posts matched your criteria." message in the content area. Everything worked fine before the search-upgrade, and I really hope you can help me with this.
Sounds like your system is caching some items. try rebooting the server or restarting.
I tried both. Same error message. I *think* I didn’t do anything apart from running the upgrade_search.php, but I can’t rule out mistakes on my part. Any ideas? Don’t know if you need it, but here’s the full error message again, it got truncated in the previous post:
Database error: [Can't open file: 'wp_posts.MYD'. (errno: 145)]
SELECT DISTINCT * FROM wp_posts WHERE 1=1 AND post_date_gmt < = '2005-01-14 20:25:59' AND (post_status = "publish") GROUP BY wp_posts.ID ORDER BY post_date DESC LIMIT 1
It got truncated again.
Okay, I managed to fix the wp_posts table with the help of the MySQL REPAIR TABLE command and everything *seems* to work again now. But it’s doing the thing again where it doesn’t save changes to already-published posts, which has never happened before. I’ll try a restart tomorrow (after some sleep).
Sorry for spamming your blog, I hope this counts as (welcome) bug-reporting.
Nice mod but it’s a bit too rigorous since it displays private, drafts etc I would rather it didn’t. Can you provide and example of how to limit the search to published posts only?
Many thanks
Replace line 108 in search.php with the following line. Make sure there are no line breaks, all of this goes on one line.
$sql = "SELECT ID,post_title,post_content,post_excerpt,post_date, MATCH(post_title,post_content) AGAINST ('".$_GET['q']."') AS score FROM ".$table_prefix."posts WHERE MATCH (post_title,post_content) AGAINST ('".$_GET['q']."') WHERE post_status= 'publish' LIMIT ".$_GET['next_value'].",".(intval($_GET['next_value'])+$max_results+1);
For the most part any input is really welcome, so no problem. I hope you got your issue fixed.
I wonder why no one knows about WHY THE TABLES CRACH AND WHY THIS ERRNO: 145 COMES UP. We all have been discussing the remedy for the identified problem… we we still don’t know WHAT ACTUALLY BREAKS THESE TABLES TO CAUSE THIS ERRNO 145!
I’m facing this problem damn frequently…. and so I want to indentify the root cause of this problem…. please anyone let me know if you know….
Thanks in Advance.
James,
this probably comes from a corrupted Mysql table and the only answer I have for you is to backup before you install any upgrade/plugin that modifies your tables. In the meantime, http://www.karakas-online.de/f......php?t=332 might help.
This is a great improvement over WP’s default search — thank you!
One question. How do I get the results to sort by date as opposed to relevance?
It looks really strange on sort by date, but if thats what you want do the following:
Line 103 in search.php, add the following:
$sql .= ' ORDER BY post_date ASC ';
Replace line 108 with the following:
$sql = "SELECT ID,post_title,post_content,post_excerpt,post_date, MATCH(post_title,post_content) AGAINST ('".$_GET['q']."') AS score FROM ".$table_prefix."posts WHERE MATCH (post_title,post_content) AGAINST ('".$_GET['q']."') ORDER BY post_date DESC LIMIT ".$_GET['next_value'].",".(intval($_GET['next_value'])+$max_results+1);
Now this is considering you want the date sorted descending, if not replace DESC above with ASC.
Thank you very much. I am not sure why it “looks really strange” but it works well for our site (a political blog where we want the latest “news” on a given subject available first, or at least make it easier to find older posts).
I have updated the search plugin to work with 1.5 of wordpress. Could I send it to you and have you announce it?
you can email me at the above.
Hi:
I’ve just installed your search script using my own style sheet. Doesn’t see anything with my mod rewrite rules of %postname%. When I go back to the default permalink structure on a WP 1.2.2 it works just fine.
Any ideas?
Thanks
RD
Does anybody know how I can adjust the code so that it will also search through the category titles? Can’t seem to find any plugin that can do this…
I tryed to run the search_upgrade.php after copying bith scripts to the root of my blog an it gaves me the following error: No Database Selected. What should I do?
Thanks!
I got the same error.
Is it possible to change the URL structure?
I prefer “http://www.example.com/search/?q=the+keyword” and have a search page under /search/ without any query to link to if I want someone to search.
I would be very happy if it was possible to translate /search/ to /sok/ (Swedish) in the URL above.
Thanks for a great “plugin”!