RSS

Monthly Archives: December 2011

[zend_search_lucene][tutorial] How to use Zend_Search_lucene

With the help of Simon Harris and Thomas Klytta, i get to use Zend_Search_lucence for full text search. Following are the steps for using Zend_search_Lucence within an MVC based module:

Do the following steps in indexAction() of Controller

1) Create index within index/ directory

$index = Zend_Search_Lucene::create('index/');

2) In order to search record from any field of a particular table within the database, get the table records by calling model object

$mapper = new Application_Model_SampletestMapper();
$records = $mapper->fetchAll();

On printing $records you will get list of all the records.

3) Once you have all the records just loop them through; so that to get it documented km populated in the index file

foreach($records as $record)
{
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::text
('id',$record->id, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('name',$record->name, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('email',$record->email, 'UTF-8'));
$index->addDocument($doc);
}

4) Save the index with the updations

$index->commit();

Steps 1) to 4) did the Index Building, now we have to search query across this index file.

Before searching we have to code a simple form having search field and submit button.

5) Add following self explanatory code in controller file

public function getForm()
{
$form = new Zend_Form();
$form->setAction('index')
->setMethod('post');
// Create and configure search_field element:
$search_field = $form->createElement('text', 'search_field');
// Add elements to form:
$form->addElement($search_field)
->addElement('submit', 'search', array('label' => 'Search'));
return $form;
}

6) Call this form in the indexAction()

public function indexAction()
{
// render sampletest/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
// remaining code here …
}

7) For displaying the form add following code in sampletest/form.phtml

<h2>Search:</h2>
<?php echo $this->form; ?>

8) Now come back to the controller file for getting the details of search results. In the indexAction add the following code:

if(isset($_POST['search_field']))
{
$index   = Zend_Search_Lucene::open('index/');
$results = $index->find($_POST['search_field']);
$this->view->index = $results;
$this->view->query = $query;
}

9) Now display the search results through view file; sampletest/form.phtml

<?php
$results = $this->index;
if(count($results) == 0 && isset($this->query))
{
echo "No search results found for the desired Search".
"(i.e. $this->query)";
}
else
{
foreach($results as $result)
{
echo "<br> id: ".$result->id;
echo "<br> name: ".$result->name;
echo "<br> email: ".$result->email;
}
}
?>

C1) Complete code to be added in SampletestController.php:

public function getForm()
{
$form = new Zend_Form();
$form->setAction('index')
->setMethod('post');
// Create and configure search_field element:
$search_field = $form->createElement('text', 'search_field');
// Add elements to form:
$form->addElement($search_field)
->addElement('submit', 'search', array('label' => 'Search'));
return $form;
}
public function indexAction()
{
// render sampletest/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
// building index file
$index = Zend_Search_Lucene::create('index/');
$mapper = new Application_Model_SampletestMapper();
$records = $mapper->fetchAll();
foreach($records as $record)
{
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::text
('id',$record->id, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('name',$record->name, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('email',$record->email, 'UTF-8'));
$index->addDocument($doc);
}
$index->commit();
//searching the desired query string in the index file
if(isset($_POST['search_field']))
{
$index   = Zend_Search_Lucene::open('index/');
$results = $index->find($_POST['search_field']);
$this->view->index = $results;
$this->view->query = $query;
}
}

C2) Complete code to be added in sampletest/form.phtml:

<h2>Search:</h2>
<?php echo $this->form; ?>

<?php
$results = $this->index;
if(count($results) == 0 && isset($this->query))
{
echo "No search results found for the desired Search".
"(i.e. $this->query)";
}
else
{
foreach($results as $result)
{
echo "<br> id: ".$result->id;
echo "<br> name: ".$result->name;
echo "<br> email: ".$result->email;
}
}
?>

In the same pattern add the data of all tables in the index/ directory so that to ensure full text search across whole database instead of single field search of a single table.

Great Thanks to Simon Harris and Thomas Klytta

 
2 Comments

Posted by on December 30, 2011 in zendframework

 

Tags: , , ,

[ajax][IE][tip] ajax functions show random behaviour in IE and works fine in all other browsers

IE needs special care while designing css and scripting js/ajax. Once while using sortable function and updating db as per latest sorting i get random behavior of IE as compare to other browsers (Firefox, Chrome etc). On first move the html element got sorted but is not updated in db; and for all moves-other than the first one-it works fine.
After debugging the issue for hours i was just like…

Stucked

Stucked

Then i ‘Delete Browser History’ and thus it works perfectly fine for all moves even for FIRST ONE. Following are the screenshots illustrating the ‘Delete Browser History’ process

Delete Browser History

Delete Browser History

Delete

Delete

Thanks to ‘Delete Browser History’ option 🙂

Later on, I found How to solve Internet Explorer cache issue in Ajax .

When you call the method:

http.open

add a random variable using Math object and add it in the method’s URL argument, for example:

nocache = Math.random();
http.open('get','page.php?url_parameter='+
parameter+'&nocache ='+nocache);
In this way all the new request will have an URL parameter (&nocache) which force the browser to “refresh” the old value with the new value.
 
4 Comments

Posted by on December 24, 2011 in ajax

 

Tags: , , , , , , , , , ,

[php][tip] How to map date field across different tables when one is of date format and the other of unixtimestamp?

While merging record of multiple tables into a single table, I need to merge the date field as well and the issue with merging date field is that the field in which I have to merge it have unixtimestamp and the source filed have the date format yyyy-mm-dd hh:ii:ss.

After searching alot on any mysql function that can convert date format, found nothing. Rather find strtotime php function to meet the purpose. Thus tried a bit lengthy way to merge two tables by first merging all the required data except date field and merge the date field afterwards by using following update query.

$query = "SELECT * FROM `src_table` ;
$result = mysql_query($query);
$dts = array();
while($row = mysql_fetch_array($result))
{
   $id = $row[‘id’];
   $dts[$id] = strtotime($row['date']);
}
foreach($dts as $id => $date)
{
   $query = "UPDATE dest_table set date = '".$date."'".
   " WHERE id = '".$id."'";
   mysql_query($query);
}

That’s it 🙂

 
Leave a comment

Posted by on December 10, 2011 in php

 

Tags: , , , , , ,

[mysql | tip] Custom import huge table in phpmyadmin

For importing, huge table in phpmyadmin without using sqlyog or file uploading through ftp and even if bigdump couldn’t upload your huge file then there is an easy way around to achieve the target by custom exporting the table first into number of files by first checking out the maximum file size to be allowed for importing file.

By custom export, it means to say exporting thousands of rows in a single file as shown in following screen-shots:

custom export

custom export

OR

custom export

custom export

and thus exporting table in number of files and then import all files one by one.
While moving vBulletin post table, this really helped me 🙂

 
Leave a comment

Posted by on December 8, 2011 in mysql

 

Tags: , , , , ,

 
%d bloggers like this: