Always Get Better

Posts Tagged ‘Google’

How to Create Full Text Search Using mySQL

Thursday, February 14th, 2008

Search is one of the most basic features visitors expect when they come to a web site. This is especially true in e-commerce where your ability to make a sale is directly related to your customer’s ability to find the product they’re looking for.

Using Third-Party Solutions

Many first-time site owner choose to go with Google Custom Search because of its easy setup and because of Google’s incredible indexing reach and results. I don’t like the standard edition of the Custom Search because of the branding – your search results advertise Google and provide links to competing content. For an e-commerce site to lose control of such a critical function, the results can be costly.

Don’t Give Up Control

Especially in e-commerce, it is best to never give up control over any content. Advanced users may choose to ignore your site’s search tool and use Google to get at your content anyway (via the site: directive) but in the general case there is great potential to keep selling useful products to your potential customers even while they search your site for other items.

Incorporating a decent search tool into a web site using PHP is dead simple. All it involves is a database table with 3 or more rows and a little bit of an eye for layout. Even if you don’t consider yourself a designer, having a look around other search engines will give you a feel for how results should display.

Creating the Search Tables

Let’s get started. Our simplistic database table (PRODUCTS) will consist of the following columns:

Column Name Data Type Description
intID int Product ID and Primary Key
vcrName varchar(25) Product Name
txtDescription text Product Description
vcrPhoto varchar(40) Path (URL) to product photo

Obviously this is just a simplified example, but the product ID, name, description and photo should be enough for the purposes of our demonstration.

The SQL to create the table looks like this:
[source:sql]
CREATE TABLE PRODUCTS
(
intID int auto_increment,
vcrName varchar(25),
txtDescription text,
vcrPhoto varchar(40),
CONSTRAINT PRODUCTS_pk PRIMARY KEY ( intID )
);
[/source]

Add the Full-Text Index

In this example, we’re interested in searching the name and description fields of our products. In order to add the full-text index to our table, we use the ALTER TABLE command:

[source:sql]
ALTER TABLE PRODUCTS ADD FULLTEXT( vcrName, txtDescription );
[/source]

Alternatively, we could have created the index along with the table in our original CREATE statement like this:

[source:sql]
CREATE TABLE PRODUCTS
(
intID int auto_increment,
vcrName varchar(25),
txtDescription text,
vcrPhoto varchar(40),
CONSTRAINT PRODUCTS_pk PRIMARY KEY ( intID ),
FULLTEXT( vcrName, txtDescription )
);
[/source]

Searching For Text

Now that the index has been created, we can go ahead and search the database. To activate full-text search, we use the MATCH () AGAINST () syntax like this:

[source:sql]
SELECT intID, vcrName, txtDescription, vcrPhoto
FROM PRODUCTS
WHERE MATCH( vcrName, txtDescription ) AGAINST ( ‘search terms here’ );
[/source]

That’s all there is to it! Anyone with access to a mySQL database should be able to incorporate search into their sites without too much difficulty. Of course this is a very basic introduction, but should be more the sufficient to get going with.

WordPress Source Code Syntax Highlighting

Saturday, January 19th, 2008

Because the program code I’ll be posting on this blog is hard to read when in the form of long colourless unformatted lines, I’ve looked around at various plugins to help make my work more readable.

WordPress suggests using a port of the syntaxhighlighter Google Code proejct by Alex Gorbatchev (http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/) but I found this plug-in to be too robust.

For my purposes, I plan to demonstrate HTML, C#, C++ and some Java, so my goal was to have a very scaled-back method of posting code.

A Google search brought me to Sean Deasy’s blog (http://www.seandeasy.com/code-syntax-highlighting-in-wordpress/) which pushed me in the right direction.

The WP-dp.SyntaxHighlighter:source code syntax highlighting plugin supports a good breadth of languages and has a minimalistic look – perfect for my needs. It can be found at:

WP-dp.SyntaxHighlighter:source code syntax highlighting plugin

I recommend this plugin to anyone who, like me, needs a decent syntax highlighter without a lot of the bells and whistles found in other packages.

[Edit: The plugin file for this tool has 2 lines of whitespace at the end, after the “?>”. If you install this tool, you will want to remove these extra characters (you can do this from right inside WordPress’ editor page after uploading the file to your server).]

[Edit2-Feb8, 2008: I had problems getting the originally suggested Highlighter working the way I wanted. I eventually settled on this one from OpenSourceBrain: http://erik.range-it.de/wordpress/plugins/syntaxhighlighter/ … it’s the same idea but implemented much more cleanly]

Blank Screen Loading GMail

Wednesday, January 16th, 2008

I noticed lately that I’ve been having issues accessing my GMail account.  These issues have come and gone in the past but in the last several days it’s turned into an occurrence every time I log into my account.

For anyone else sharing in this problem, here is my solution:

  1. Try clearing the URL (e.g. if your address bar reads http://mail.google.com/mail/#, change it to simply http://mail.google.com).  Sometimes this will “trick” your browser into re-loading the page.
  2. Go into your Internet Options and clear your browser’s cache.  On Firefox this works nearly 100% of the time; for Internet Explorer continue to step 3.
  3. Close all running copies of your web browser (after clearing the cache) and then re-load and log in again.  This is normally my last step and hasn’t failed yet.