Searching the index is relatively simple using ZendSearch\Lucene. The index needs to
be opened for querying and the query string needs to be passed to the find() method in
ZendSearch\Lucene\Index. The find methods return an array matching the hits for the
specific query, and this in turn can be used to render the search results.
There are two options for querying the index—you can pass the plain text query string to the find function or you can build your own Query object using ZendSearch\Lucene\ Search\Query.
In the following example, we will be using plain text queries, and you can manipulate the search results by using operators such as :,+,-, and field searches. For example, see the following list:
◆ A search for all documents uploaded by Anne could be retrieved by the following query:
owner:Anne
◆ A search for all documents having the word report and uploaded by the user named Anne could be retrieved by the following query:
report AND owner:Anne
◆ A search for all documents having the word report and excluding the ones
uploaded by Anne could be retrieved by the following query:
report -owner:Anne
Perform the following steps for displaying search results:
1. For displaying the search results, we will need to create a new form which will display the search textbox and render the search results right below the search form. The form will be placed in SearchController underCommunicationApp/ module/Users/src/Users/Controller/SearchController.php.
2. Create a new view which will be used for displaying the query window and also rendering search results. This will be placed under CommunicationApp/module/ Users/view/users/search/index.phtml.
<h3>Document Search</h3>
<?php
// Search Form
echo $this->form()->openTag($form); foreach ($form as $element) {
echo $this->formElement($element);
echo $this->formElementErrors($element);
}
echo $this->form()->closeTag();
// Search Results
if (count($searchResults)) {
?>
<h5>Results</h5>
<table style="width: 600px; border:1px solid #f5f5f5;">
<tr>
<th width="30%" align="left"> Label</th>
<th width="30%" align="left"> Owner</th>
<th align="left"> File</th>
</tr>
<?php foreach ($searchResults as $searchResult) {
?>
<tr>
<td><?php echo $searchResult->label; ?></td>
<td><?php echo $searchResult->owner; ?></td>
<td><a href="<?php echo $this->escapeHtml($this->url('users/ upload-manager',
array('action'=>'fileDownload', 'id' =>
$searchResult->upload_id)));?>">Download</a></td>
</tr>
<?php
}
?>
</table>
<?php }?>
3. Now create a new action which will display the Search form and also query the Lucene index with the input provided in the Search form. This will be placed in SearchController underCommunicationApp/module/Users/src/Users/ Controller/SearchController.php.
public function indexAction()
{
$request = $this->getRequest(); if ($request->isPost()) {
$queryText = $request->getPost()->get('query');
$searchIndexLocation = $this->getIndexLocation();
$index = Lucene\Lucene::open($searchIndexLocation);
$searchResults = $index->find($queryText);
}
// prepare search form
$form = new \Zend\Form\Form();
$form->add(array( 'name' => 'query', 'attributes' => array(
'type' => 'text', 'id' => 'queryText', 'required' => 'required'
),
'options' => array('label' => 'Search String',),));
$form->add(array(
'name' => 'submit', 'attributes' => array(
'type' => 'submit',
'value' => 'Search'
),
));
$viewModel = new ViewModel(array( 'form' => $form,
'searchResults' => $searchResults
)
);
return $viewModel;
}
4. Test the page in your browser; you should be able to see search results for keywords that are available in the label and owner fields:
On searching using Owner Name, you will get the following search results:
What just happened?
We have now implemented the search results page, which allows us to query for uploaded documents using their labels and owners. The retrieved search results are displayed in a customized view which allows us to download the document from the search result.
Our next step will be to expand the search to search the contents of the uploaded
documents; for this we will need to make changes to the way we generate the index.
Không có nhận xét nào:
Đăng nhận xét