PDA

View Full Version : Search Examples?



mobile207
4 May 2011, 8:42 AM
I've been looking for examples (preferably a tutorial) on building a search form that queries a mysql database.

I'd like to build a search box at the top of one of my panels, submit the search query to a php script when a button is pressed, return appropriate json and insert the records into a list.

Thanks

Jack9
4 May 2011, 4:58 PM
For the frontend, search is just a textbox/textarea for a search query.
In the backend, well... "Searching" is too general an idea to have a general implementation in the backend. Search is a complex topic. This is more of a computer science question than a Sencha/Javascript question.*

It can be done in a very primitive and slow manner using extra columns and MySQL's like %cat% - This of very limited use as it does not scale and requires some horrible code/schemas to support it. Stay away from this, in my opinion. I tried it when I first started out and it was never something to be proud of.

* -
When searching for "cat" do you want to query integer, boolean columns and how do you do that?
When searching for "dog" do you want to search the password fields of user accounts?
What happens when your dataset gets very large and it takes 5 minute to read the entire database to search for 1 term?

Just no.

There are many concerns that need to be addressed, resulting in some sort of middle tier to implement search. Engines like Solr (very popular), create searchable indexes to query with a standard syntax.

Solr can provide multi-column fields alongside native columns (search for name across Fname, Lname, Fname+" "+Lname). Solr can return results in json. Solr usually builds indexes on a scheduled cron or manual run or can be done as a trigger on certain database platforms.

mobile207
4 May 2011, 5:59 PM
Thanks Jack.

I need a pretty basic search - I will have a maximum of 300 records in one table that I will be searching. No blobs or long pages of text. Just want to match against short fields like city, business name, region and maybe category.

Should I query the MySQL DB directly from the app each time a search is performed or preload the JSON results from the DB when it loads? (The big JSON load is used for a nested list on another panel) Maybe store it somehow and search through that?

Jack9
4 May 2011, 7:53 PM
For 300 rows just do a query every time.

$_POST['searchfield']

"select * from table where
city like %searchfield% or
region like %searchfield% or
category like %searchfield%"

You have to worry about injection, like is extremely hard on the DB. Also this is 1 query on 1 denormalized table. Like I said, it's hell to maintain. Definitely don't do it in your javascript.

mobile207
4 May 2011, 8:05 PM
I think I have the php/mysql part of it down. How do I build the actual search form and process the results in Sencha Touch though? I want to send off the query and put the JSON response into a list, like how Google live search works. I think an example would set me on the right path.

efilips
5 May 2011, 2:39 AM
I recently did a search and did not want a search button so I put a keyup listner on the searchfield

listeners: {
keyup: function(thisField, e) {
result.setLoading(true,true);
searchTerm = thisField.getValue();
if (searchTerm.length > 4) {
setTimeout('doSearch(searchTerm)', 1000);
}
}
}

My doSearch function makes an Ajax request to my servlet.

function doSearch(query) {
Ext.Ajax.request({
url: 'search',
params: {
query: query
},
success: function(response, opt) {
searchStore.loadData(Ext.util.JSON.decode(response.responseText),false);
result.setLoading(false,false);
},
failure: function(response, opts) {
Ext.Msg.alert('Fel inträffat vid kontakt med Servern ' + response.status);
}
});
}

My store is a memory store :

var searchStore = new Ext.data.Store({
autoLoad: true,
model: 'maindocs',
proxy: {
type: 'memory',
reader: {
type: 'json',
data: {doc: []}
}
}
});

I don´t think this is a 'best-practise' way of doing it but it seams to work fine in our environment.