boolean search "hack" for GalleryCoreSearch

hummle

Joined: 2007-06-01
Posts: 3
Posted: Mon, 2007-06-18 21:30

hello everybody,

As we needed some simple boolean search functionality for a photo project using gallery 2.x and I could not find any existing script I did my own little modification. I thought it might be interesting for some of you. Here it goes:

In the gallery/modules/core/classes/ folder make a backup of the file GalleryCoreSearch.class. Then open it with a simple text editor and replace following code:

/* original code

if (isset($options[$key])) {

$whereList[] = "$column LIKE ?";
$whereData[] = '%' . $criteria . '%';
$selectMap[$column] = $columnNumber++;;
$selectList[] = $column;
}
*/

/* Replace it with this part
* IT ADDS A BASIC BOOLEAN SEARCH
* START NEW CODE
*/

if (isset($options[$key])) {

$criteria_array = explode(" ", $criteria);
$total_items = count($criteria_array);
$loop_index = 0;
$countWhere = "("; // to group for AND start

while ($loop_index < $total_items) {

$criteria_item =
trim($criteria_array[$loop_index]);

if (strlen($criteria_item) > 0)
{

if (($total_items > 1) and ($loop_index > 0))
{ // more words so AND or OR needed

/* checks if advanced search parameters are entered
* such as + for AND, - for AND NOT and translates them in SQL
* then removes the used special characters for search
* @author Francois Voisard <francois.voisard<at>gmx.ch>
*/

if (preg_match("/\+/",$criteria_array[$loop_index]))
{
$criteria_item = ltrim($criteria_array[$loop_index],"+");
$countWhere .= "AND $column LIKE ?";
}
elseif (preg_match("/\-/",$criteria_array[$loop_index]))
{
$criteria_item = ltrim($criteria_array[$loop_index],"-");
$countWhere .= "AND $column NOT LIKE ?";
}
else
{
$countWhere .= "OR $column LIKE ?";
}

}
else
{

if (preg_match("/\+/",$criteria_array[$loop_index]))
{
$criteria_item = ltrim($criteria_array[$loop_index],"+");
$countWhere .= " $column LIKE ?";
}
elseif (preg_match("/\-/",$criteria_array[$loop_index]))
{
$criteria_item = ltrim($criteria_array[$loop_index],"-");
$countWhere .= " $column LIKE ?";
}
else
{
$countWhere .= " $column LIKE ?";
}
}

//make each question mark to inject data later
$whereData[] = '%' . $criteria_item . '%';

}
$loop_index++;

} // end loop

$selectMap[$column] = $columnNumber++;
$selectList[] = $column;
$countWhere .= ") "; // to group
$whereList[] = $countWhere;
} // end isset

/*
* END NEW BOOLEAN CODE
*/

Save the modified file as new GalleryCoreSearch.class. Now you should be able to do following boolean searches:
- Searching for word1 word2 is equivalent to "result LIKE word1 OR LIKE word2"
- Searching for +WORD1 +WORD2 is equivalent to "result LIKE word1 AND LIKE word2"
- Searching for -WORD1 +WORD2 is equivalent to "result NOT LIKE word1 AND LIKE word2"

As I'm not a genuine programmer, I'm sure there is lot of potenial for improvements in my code, but for a first try it's ok and should do until boolean search is included in gallery (don't forget to vote for the boolean search feature :) http://sourceforge.net/tracker/index.php?func=detail&aid=1221234&group_id=7130&atid=357130)

The code is provided as is and it's based on the unofficial update "Enhancement to search for each word" by kid1270 (https://sourceforge.net/tracker/index.php?func=detail&aid=1243397&group_id=7130&atid=357130)

If you find it usefull and like it, you may drop me a line to tell me...

enjoy
François

 
GiovannaS

Joined: 2007-06-24
Posts: 3
Posted: Sun, 2007-06-24 05:31

Sweet! I'm wondering though, how might one go about editing this so that the + is always assumed? My gallery is rather large and the OR is useless; I'd rather people just type in the words and have it always be searching as AND. Is this possible?

 
rachima
rachima's picture

Joined: 2007-06-21
Posts: 4
Posted: Sun, 2007-06-24 19:25

Hi GiovannaS,

GiovannaS wrote:
Sweet! I'm wondering though, how might one go about editing this so that the + is always assumed? My gallery is rather large and the OR is useless; I'd rather people just type in the words and have it always be searching as AND. Is this possible?

I think, for this, you need one of the shortest modifications I've ever seen ;-)

You only have to change in this section:

else
{
$countWhere .= "OR $column LIKE ?";
}

the OR to AND ! ($countWhere .= "AND $column LIKE ?";)

That's all.

Rachima

 
helaku

Joined: 2007-04-29
Posts: 51
Posted: Mon, 2007-06-25 20:50

Hi François & Rachima

Just what I've been looking for! Like GiovannaS I prefer the AND search as a default. You've made my day, thank you :-)

 
hummle

Joined: 2007-06-01
Posts: 3
Posted: Tue, 2007-06-26 06:51

Hi all,

helaku wrote:
Just what I've been looking for! Like GiovannaS I prefer the AND search as a default. You've made my day, thank you :-)

It's always a good feeling to save one's day =;0) As Rachima already wrote you can just replace the general OR with an AND, but as my code was designed to perform the OR-search in first place you may prefer to replace the original snippet with a simpler and shorter patch doing just the AND-search without the other two possibilities (AND NOT, OR). You'll find it here: https://sourceforge.net/tracker/index.php?func=detail&aid=1243397&group_id=7130&atid=357130 (by kid1270)

have a nice day,
François :)

 
balu123

Joined: 2007-06-26
Posts: 1
Posted: Tue, 2007-06-26 13:30

Hi Everyone

Great. It works fine for common functions like Search descriptions or Search keywords. But I cannot use it for Search Tags. I think that Tags search engine uses diferent code for search in tables of database. Is it possible that this solution could be implement into tags search engine?
Thank you.

Balu

 
GiovannaS

Joined: 2007-06-24
Posts: 3
Posted: Thu, 2007-06-28 13:14
rachima wrote:
I think, for this, you need one of the shortest modifications I've ever seen ;-)

Wow! I'd say 'Well that was stupid of me.', but I'm completely unfamiliar with the code. This will be perfect! Thanks. :)

 
ron@adobewerks.com
ron@adobewerks.com's picture

Joined: 2006-11-09
Posts: 66
Posted: Sat, 2007-08-18 15:54

This is so wonderful! i have been waiting for this for a long time. My site is large and heavily tagged.. I am using this code with the OR option. if you would like to see how sweet it is: www.adobewerks.com thank you François!

 
mriffey

Joined: 2006-06-22
Posts: 8
Posted: Tue, 2007-08-21 18:41

Hummle,

thanks very much for this. Works great. You can see it at http://www.wickerbydesign.com/gallery

Mark

 
tuz83

Joined: 2007-08-29
Posts: 3
Posted: Fri, 2007-08-31 10:49

thanks very much for this hummle! great extension!

I for myself do only need the AND search - so I tried the "short and simple" extesion you recommended ("Enhancement to search for each word"), but this one didn't work for me.
I just got a blank screen when I entered the search words, no error messages or so - just a blank page.

I tried your extension and it worked immediately - great!

But becuase I do only need the AND seach is it possible that you write us your extension but only with the AND function - to keep the code short and clean.

that would be really great!
thanks, Tom

 
tonneti

Joined: 2006-10-07
Posts: 1
Posted: Wed, 2007-09-26 23:03

this works great, is just what we were looking for our project, thanks!!

greets

 
hummle

Joined: 2007-06-01
Posts: 3
Posted: Wed, 2007-09-26 23:17

Hi Tom,

I'm very busy at the moment (as you probably guessed for the lack of an answer ;) ), but I'll do the AND-only modification as soon as possible and post it here.

cu around,
François

 
lerone

Joined: 2008-09-22
Posts: 16
Posted: Wed, 2008-09-24 15:15

hello,

a thank you note from another thankful person you saved the day. it works swift! grreat!

still I would have a question. it seemes the "AND"-search looks only over the same fields. i.e. either title, or keywords, or description. meaning: if you put in "x" "y" it will find images in which "x" AND "y" are in the same field. so if "x" is in the title and "y" in the descriptions it won´t find the image. this seems strange as the standard search looks across all fields.

so I am not a coder and it might be an inherent problem. but maybe there is a swift way to make this swift hack even cooler. would be nice! – thanks already for all hints!

 
dranium

Joined: 2008-11-22
Posts: 18
Posted: Fri, 2009-06-19 02:52

I am using this hack for my search and it works great. I want to change it up a bit and add a drop down menu.

I added this to my search page...

<select name="{g->formVar var="form[searchOpt]"}">
<OPTION value="1" > Any matches )
<OPTION value="2" > Exact Match
</select>

Only problem is I do not know how to pass the variable to GalleryCoreSearch.class... I tried using $testing = $_POST["g2_form[searchOpt]"]; but this does not pick up the value from the search. I tried to figure out how $criteria is passed on to the class but cannot seem to figure out. Any help would be greatly appreciated.

Thank you!

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Fri, 2009-06-19 03:50

Try looking to the search module
namely SearchScan.inc

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
dranium

Joined: 2008-11-22
Posts: 18
Posted: Mon, 2009-06-29 19:36

Thank you suprsidr. Works great!

 
TomekM

Joined: 2010-05-05
Posts: 1
Posted: Wed, 2010-05-05 20:20

God bless you Hummle! U have saved me tons of work!

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Mon, 2010-05-24 14:47

This hack works fine but it has trouble searching across multiple fields as Lerone explained above. I took endstille's version from gallery.menalto.com/node/53069 and had it corrected it for my site. You'll need to replace the function search code around line 58 in GalleryCoreSearch.class with the following...

Quote:
/**
* @see GallerySearchInterface_1_0.search() --- Modified hack from gallery.menalto.com/node/53069
*/
function search($options, $criteria, $offset=0, $count=-1) {
global $gallery;

$column_array = array(
array('titles', '[GalleryItem::title]'),
array('descriptions', '[GalleryItem::description]'),
array('keywords', '[GalleryItem::keywords]'),
array('summaries', '[GalleryItem::summary]')
);

$criteria_array = explode(" ", $criteria);

$whereList = array();
$whereData = array();
$columnNumber = 0;

for ($j = 0; $j < count($column_array); $j++)
{
$column = $column_array[$j][1];
$key = $column_array[$j][0];

if (isset($options[$key]))
{
$selectList[] = $column_array[$j][1];
$selectMap[$column] = $columnNumber++;;
}
}

for($i = 0; $i < count($criteria_array); $i++)
{
$crit = $criteria_array[$i];
$countWhere = "(";
$countMatches = 0;

for ($j = 0; $j < count($column_array); $j++)
{
$column = $column_array[$j][1];
$key = $column_array[$j][0];

if (isset($options[$key])) {
if ($countMatches)
{
$countWhere .= ' OR ';
}
$countMatches++;
$countWhere .= "$column LIKE ?";

$ca = preg_split('//', $criteria_array[$i], -1, PREG_SPLIT_NO_EMPTY);

if ($ca[0] == "-")
{
$whereData[] = '%' . substr($crit, 1) . '%';
}
elseif ($ca[0] == "+")
{
$whereData[] = '%' . substr($crit, 1) . '%';
}
else
{
$whereData[] = '%' . $crit . '%';
}
}
}

$countWhere .= ")";

if ($i + 1 < count($criteria_array))
{
$ca = preg_split('//', $criteria_array[$i+1], -1, PREG_SPLIT_NO_EMPTY);

if ($ca[0] == "-")
{
$countWhere .= " AND NOT ";
}
else
{
$countWhere .= " AND ";
}
}

$whereList[] = $countWhere;
}
/* End of NEW boolean hack */

 
bdam

Joined: 2010-08-17
Posts: 1
Posted: Tue, 2010-08-17 15:50

Another_Matt, to get yours working (G2) with multiple words I had to remove the OR from the implode lines of the function. This was based on a suggestion on the bottom of the thread you linked to. However, doing this seems to have removed all OR logic so every query became an AND query. Is that intentional or am I doing it wrong?

Also, any thoughts on searching for an exact phrase? For instance, if I have a keyword 'Keyword 8' how would I find where that phrase occurs exactly versus finding images that have both of them somewhere. Typically this is done by surrounding the phrase with single or double quotes but that doesn't seem to work.

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Wed, 2010-08-18 16:53

Yeah, I intentionally removed the OR because it never seemed to work correctly. I would love for someone to add the ability to search exact phrases with quotes. Maybe a bona fide programmer will tire of G3 coding and fix up this G2 search hack with google-like abilities for old time's sake.

 
madsbarkman

Joined: 2010-11-01
Posts: 5
Posted: Mon, 2010-11-01 14:06

Hey guys
For some reason I cannot get this to work. When I replace the code in GalleryCoreSearch.class with Hummels, I just get a blank screen when searching in my gallery. Anyone has an idea what I'm doing wrong?
Thx alot in advance

 
nivekiam
nivekiam's picture

Joined: 2002-12-10
Posts: 16504
Posted: Mon, 2010-11-01 17:19

I'm sure there's a syntax error. Please zip and attach your GalleryCoreSearch.class
____________________________________________
Like Gallery? Like the support? Donate now!!! See G2 live here

 
madsbarkman

Joined: 2010-11-01
Posts: 5
Posted: Tue, 2010-11-02 08:31

Hey Nivekiam
Thx for your fast reply. I've attached my GalleryCoreSearch.class file where the new code is pasted into.