Part of the EllisLab Network
   
 
How to implement complex search?
Posted: 18 January 2010 05:42 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

Hello everyone,

I have a search module that I cannot implement properly. For a real estate site I have to implement advanced search via 15 different filters. Results have to be visible as table with pagination.

Currently I have filtering and search working and filling the table with the results. My problem is passing the search parameters in the session. I use flashdata to pass all the data page by page. But if I click on details or open another tab for other search, my data is lost.

I have 3 ideas to implement it:

1. Permit using GET queries and pass parameters in the URL. It’s totally not CI friendly indeed.
2. Save params in the session object, not flashdata. Then I don’t know how to flush the session, also I have to set different IDs for different sessions (if we open few tabs and do parallel search)
3. Create a DB table for sessions and save data there.

Is there any popular solution to use here?

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 18 January 2010 11:04 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  139
Joined  03-18-2008

I’m assuming your filters have to do with min. price, max. price, bedrooms, etc.?

You could get by using delimited values like:

http://site.com/search/index/data-to-be-delimited-by-a-dash/P0

That would get you around the GET issue, as well as give you state when returning to the search page.

Profile
 
 
Posted: 18 January 2010 11:41 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

I have undefined count of parameters and different value types (also I use special symbols in search that I have to escape), so I actually think it’s going to be hard to parse and set the params in URL, too. It would probably be easier if I knew the number of parameters I pass, but I don’t.

I think I’m going to try serializing parameters and save persist them in database with a session ID which I’m going to use. Then I’m going to flush this table once a week in order to keep it clean. Honestly I prefer another solution, but still don’t find prettier one…

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 18 January 2010 11:56 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  139
Joined  03-18-2008

Yes, a database solution using sessions is probably your best bet.

BTW, how come you don’t know the number of filter parameters? It’s not fixed for your data set? Can the data set be modified? Are you using tagging to describe the data?

Profile
 
 
Posted: 18 January 2010 12:10 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

Well, I have a large number of parameters but I am able to filter all of them or just a few. Also I have 6 different advanced search pages quering the same database. Currently I just check manually all the parameters I know and then add the constraints via Active Record, f.i.

To be clear, I have 6 types of offers with different params in a parent (and sub) tables. I do DB join and check the incoming parameters - if not empty, do a where clause to filter the results.

if(!empty($someparam)) {
$this
->db->where('column'$someparam);

That’s just an example, but that’s the point now.

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 18 January 2010 01:21 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  880
Joined  07-26-2009

I would simply save it as a multi-dimensional array in the $_SESSION[‘filter1’][‘blah’] and unset($_SESSION[‘filter1’]) when I’m done.

 Signature 

BenEdmunds.com

Contact Me:                                    My Code: 

  ben.edmunds@gmail.com            Github
  @benedmunds

Profile
 
 
Posted: 18 January 2010 03:31 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

What I’m interested is what does it mean “when I’m done”? The business case is that user can have 2-3 tabs open, one of them searching, 1 publishing offer and 1 checking statistics. At the same time. So I’m not sure when I have to free the session at this point. Using DB and serialize() method at least gives autoincrement ID that can be flushed with cron task or something.

The paging and passing parameters is the worst coded part of my project and I sincerely try to improve it, so thank you all for the efforts smile Ideas and snippets are highly appreciated.

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 18 January 2010 09:19 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

My final decision was the database driven one - I created a ‘search_sessions’ table with id, query (serialized rezult) and query_date. My search button navigates to prepare_search method that serializes all the parameters to the database and assigns the session_id to the URL. Then pagination is applied and everything is OK.

Now I have persistent search, my back button is working, few tabs are working simultaneously and I am able to pass the URL to someone else to check the data.

Thanks for the advices smile

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 19 January 2010 02:17 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  45
Joined  11-11-2009

would you share some code in some way how you did it - i need similar coplex search

how controllers and views are related

Profile
 
 
Posted: 22 January 2010 12:52 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  36
Joined  07-06-2009

I lost my post twice on post, so I’ll keep it short.

2 controllers: Search and Result, 2 views: for search form and results table, and one model for my offers.

Search is just search form with fields you have to fill for filter - nothing unusual. It redirects to result/prepare_sell (for sell offers or prepare_X for other type X)

Snippet for prepare_sell:

// get all input params (here I show just 4 for example)
            
$min_area $this->input->post('min_area'); 
            
$max_area $this->input->post('max_area'); 
            
$min_price $this->input->post('min_price');
            
$max_price $this->input->post('max_price');  

// add them to array            
            
$search_params = array( 
                                    
'min_area' => $min_area
                                    
'max_area' => $max_area
                                    
'min_price' => $min_price,
                                    
'max_price' => $max_price);
            
// serialize them in JSON format
            
$ser serialize($search_params);
            
// add them to sessions database
            
$session_record = array(
                
'query_text' => $ser,
                
'search_date' => date('Y-m-d H:i:s'
            );
            
$sess_id $this->offers->add_search_session($session_record); 

I’ve skipped all validations and most of the fields, but this is the algorithm I use.
we redirect to results/sell and pass the $sess_id as a param:

function sell($session_id ''{
            
// get session from database via the session_id we pass
            
$session $this->offers->get_search_session($session_id);
            
// set validation rules
            
            
$per_page 20;
            
$base_url site_url('results/sell/'$session_id);
            
$config['uri_segment'4
            
$config['num_links'12;

// deserialize text query and add parameters to session_record array            
            
$ser $session['query_text'];
            
$session_record unserialize($ser);
            
// add the array in the flashdata and then just print fields in the view
            
$this->session->set_flashdata($session_record); 

Note I use URI segment = 4 (site.com/result/sell/session/page), and so I have the session and the page in the URL.

Pagination I do setting offset in the URL and limit hardcoded in the config.

 Signature 

Codito, ergo sum - I code, therefore I am

Profile
 
 
Posted: 14 July 2010 12:59 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  22
Joined  08-14-2007

Hello Everyone,
I wish to apply search filter with some fields. I have three tables, children, department and users, I want to keep search filters from table children and department.

I want to search by children firstname, by unique code, by sex and by department field.

there are four input fields but single search button, I have a problem now how do I know which filed is filled with character and bring the result accordingly. how do I know which filed…such as firstnam filed or unique code or sex or department field is filled in with values?

Don’t have much Idea on search filter plz suggest and guide guidance with examples..How it would be good to carry? It would be so much great help.

Thank you so much for the help in advance.
Samuel

Profile