This is the third installment in a four-part series demonstrating Solarium's integration with Apache SOLR for search implementation. The first part covered core concepts and setup, while the second detailed Solarium's installation and configuration as a PHP interface to SOLR. This article focuses on building the search functionality itself.
Key Concepts:
This tutorial covers building a basic search in Laravel using the Solarium client. We'll explore enhanced search using DisMax, implement faceted search for refined results, manage dynamic fields and pagination, and implement filtering for improved user interaction.
Basic Search Implementation:
A simple search can be implemented as follows:
$query = $client->createSelect(); $query->setQuery('%P1%', [Input::get('q')]); // Using a placeholder for secure input
Input::get('q')
retrieves the search query from a Laravel GET or POST variable (named 'q'). The placeholder %P1%
escapes the search phrase.
Executing the search:
$resultset = $client->select($query);
Retrieve the number of results:
printf('Your search yielded %d results:', $resultset->getNumFound());
Iterating through results:
foreach ($resultset as $document) { // Access fields as public properties (e.g., $document->title) or iterate: foreach ($document as $field => $value) { if (is_array($value)) $value = implode(', ', $value); // Handle multi-value fields print '<strong>' . $field . '</strong>: ' . $value . '<br>'; } }
Integrating into a Laravel Application:
For a GET request, the getIndex
method in the home controller could be:
public function getIndex() { if (Input::has('q')) { $query = $this->client->createSelect(); $query->setQuery('%P1%', [Input::get('q')]); $resultset = $this->client->select($query); return View::make('home.index', ['q' => Input::get('q'), 'resultset' => $resultset]); } return View::make('home.index'); }
The corresponding view (app/views/home/index.blade.php
) would display results:
@if (isset($resultset)) <p>Your search yielded <strong>{{ $resultset->getNumFound() }}</strong> results:</p> @foreach ($resultset as $document) <h3>{{ $document->title }}</h3> <dl> <dt>Year</dt><dd>{{ $document->year }}</dd> @if (is_array($document->cast)) <dt>Cast</dt><dd>{{ implode(', ', $document->cast) }}</dd> @endif </dl> {{ $document->synopsis }} @endforeach @endif
Enhancing Search with DisMax:
To search across multiple fields (e.g., 'title', 'cast', 'synopsis'), use DisMax:
$dismax = $query->getDisMax(); $dismax->setQueryFields('title^3 cast^2 synopsis^1'); // Assign weights
This prioritizes matches in the 'title' field.
Specifying Returned Fields:
Control which fields are returned using:
$query->clearFields()->addFields(['title', 'cast']); // Or $query->addFields('*') for all
Sorting Results:
Sort results using:
$query->addSort('title', 'asc'); // Ascending order by title
Pagination:
Implement pagination using $query->setStart(0);
and $query->setRows(20);
.
Faceted Search:
Facets allow users to filter results. To create a facet on the 'rating' field:
$facetSet = $query->getFacetSet(); $facetSet->createFacetField('rating')->setField('rating');
Display facet counts:
$facet = $resultset->getFacetSet()->getFacet('rating'); foreach ($facet as $value => $count) { echo $value . ' [' . $count . ']<br>'; }
Range facets (e.g., by decade):
$facet = $facetSet->createFacetRange('years') ->setField('year') ->setStart(1900) ->setGap(10) ->setEnd(2020);
Filtering with Facets:
Add filtering based on facet selections:
if (Input::has('rating')) { $query->createFilterQuery('rating')->setQuery('rating:%T1%', [Input::get('rating')]); } if (Input::has('decade')) { $query->createFilterQuery('years')->setQuery($helper->rangeQuery('year', Input::get('decade'), Input::get('decade') + 9)); }
The view should be updated to include links for filtering based on these facets. This requires generating URLs with appropriate query parameters using http_build_query
. (Detailed view code omitted for brevity but follows the principles outlined in the original text).
This enhanced explanation provides a more structured and detailed walkthrough of Solarium and SOLR integration within a Laravel application. Remember to adapt the code to your specific data model and application structure.
The above is the detailed content of Using Solarium with SOLR for Search - Implementation. For more information, please follow other related articles on the PHP Chinese website!