A behemoth for handling huge searches
=====================================
This behemoth handles extensive search and sorts on relevance.
simon[at]casa-laguna[dot]net
##Features
git clone https://{your username}@github.com/{your username}/silverstripe-searcher.git
.FulltextSearchable::enable();
to your _config in mysite.www.example.com/dev/build
to rebuild the database.DataObject::add_extension('ObjectName', 'FulltextSearchable('Title,Content,Extrafield,etc.'));
.This module is given "as is" and I am not responsible for any damage it might do to your brain, dog, cat, house, computer or website.
This module is published under BSD 2-clause license, although these are not in the actual classes, the license does apply:
http://www.opensource.org/licenses/BSD-2-Clause
Copyright (c) 2012, Simon "Sphere" Erkelens
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Great thanks to DasPlan for finding and rudimentary implementing the query in the very first version we ever build.
SELECT *,
CASE WHEN ClassName LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN URLSegment LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN Title LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS titlematch,
CASE WHEN MenuTitle LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN Content LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN MetaTitle LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN MetaDescription LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN MetaKeywords LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN ExtraMeta LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS searchmatch,
CASE WHEN SearchKeywords LIKE '%{searchquery}%' THEN 1 ELSE 0 END AS keywordmatch,
MATCH (Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords,SearchKeywords) AGAINST ('{searchquery}') AS relevance
FROM SiteTree
WHERE (ShowInSearch = 1) AND (Status = 'Published') AND (ClassName LIKE '%{searchquery}%' OR URLSegment LIKE '%{searchquery}%' OR Title LIKE '%{searchquery}%' OR MenuTitle LIKE '%{searchquery}%' OR Content LIKE '%{searchquery}%' OR MetaTitle LIKE '%{searchquery}%' OR MetaDescription LIKE '%{searchquery}%' OR MetaKeywords LIKE '%{searchquery}%' OR ExtraMeta LIKE '%{searchquery}%' OR SearchKeywords LIKE '%{searchquery}%' OR MATCH(Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords,SearchKeywords) AGAINST ('{searchquery}' IN BOOLEAN MODE))
HAVING ( searchmatch > 0 OR keywordmatch > 0 OR titlematch > 0 OR relevance > 0 )
ORDER BY searchmatch DESC, keywordmatch DESC, titlematch DESC, relevance DESC