PDA

View Full Version : Complex search with DB Search component of DA


msummers194171
05-26-2009, 10:09 AM
I want to be able to do a keyword search across multiple fields. For example, I want the client to be able to search for -

red widgets

in fields A, B, and C.

I have a form with a single text field called 'keywords'. If I add more than one item to that field ('red widgets'), I get a query like this -

SELECT * FROM items WHERE (((item_primary LIKE '%red%') OR (item_secondary LIKE '%red%') OR (item_content LIKE '%red%')) OR ((item_primary LIKE '%widgets%') OR (item_secondary LIKE '%widgets%') OR (item_content LIKE '%widgets%'))) ORDER BY item_id ASC

And what I really want is a query like this -

SELECT * FROM items WHERE (((LOWER(item_primary) LIKE LOWER('%red%')) OR (LOWER(item_secondary) LIKE LOWER('%red%')) OR (item_content LIKE '%red%')) AND ((item_primary LIKE '%widgets%') OR (item_secondary LIKE '%widgets%') OR (item_content LIKE '%widgets%'))) ORDER BY item_id ASC

(I didn't propagate the LOWER() usages for simplicity)

How can I get that?

Thanks,

Murray

Ray Borduin
05-26-2009, 11:46 AM
Your first one is easy. Just use the wizards "keyword" search option which allows you to choose multiple columns and automatically creates the where clause as you have specified.

The second one is more difficult and would require hand code. You can't just pass in LOWER() around columns because there is SQL Injection protection built into the code that won't allow you to call functions from within it.

You could remove that and pass it in... another option is to create a VIEW in your database with Lower() versions of the fields. Then you wouldn't need to specify it in the query, you could also convert the value of the field to lower automatically in the server code so that it wouldn't need to be in the SQL itself.

The final option is to bring in the field AS a column in the query itself and using HAVING instead of WHERE. This can slow things down and requires hand code, so I would suggest the creating a view technique if at all possible.

msummers194171
05-26-2009, 01:11 PM
Thank you, Ray! I'll give those things a shot.... 8)

msummers194171
05-26-2009, 02:02 PM
Ray - I think that's what I have already used to create this page in the first place. The code I pasted into my initial post shows the select statement that resulted from that.

But - I started a new page, added a recordset to it. Then I inserted DataAssist > Database Search Wizard. Next, I chose a search page and a form name. Then I Add a search element that is Advanced Text Search. I select the fields I want to search, and specify a New Form Field (text) that I have called "Keywords". I make no further changes by clicking through the next step. The summary shows me -

Creating Search Page: test3-search.php

Creating Form: form1
Creating form element: Keywords

Applying to Recordset: rsData2

Search Arguments:
Advanced Text Search in item_primary, item_secondary, item_content from Keywords

This new search results page looks identical to my original post - it has:

//comparison list additions
$WADbSearch1->keywordComparison($KeyArr0,"".((isset($_POST["Keywords"]))?$_POST["Keywords"]:"") ."","AND","Includes",",%20","%20","%22","%22",0);

So I added

setQueryBuilderSource($query_rsData2,$WADbSearch1, false);
exit($query_rsData2);

the exit command, and uploaded.

When I fill in and submit the form, I'm getting exactly what I posted before -

SELECT * FROM items WHERE ((((item_primary LIKE '%Remington%') OR (item_secondary LIKE '%Remington%') OR (item_content LIKE '%Remington%')) OR ((item_primary LIKE '%military%') OR (item_secondary LIKE '%military%') OR (item_content LIKE '%military%'))) OR ((item_primary LIKE '%weapon%') OR (item_secondary LIKE '%weapon%') OR (item_content LIKE '%weapon%')))

I'm not getting the "AND" conjunction in there as you say I should be.

Ray Borduin
05-26-2009, 02:23 PM
Make sure when you applied you specify your implied AND as a space. I think it may be a comma by default. That should get it to add the AND.

msummers194171
05-28-2009, 06:44 AM
OK - that's got it. Thanks, Ray!