Merge branch 'pull/highlighting-support' of github.com:silverstripe-big-o/silverstripe-fulltextsearch into silverstripe-big-o-pull/highlighting-support
This commit is contained in:
commit
cc144aa33e
|
@ -342,7 +342,22 @@ abstract class SolrIndex extends SearchIndex {
|
|||
if($res->getHttpStatus() >= 200 && $res->getHttpStatus() < 300) {
|
||||
foreach ($res->response->docs as $doc) {
|
||||
$result = DataObject::get_by_id($doc->ClassName, $doc->ID);
|
||||
if($result) $results->push($result);
|
||||
if($result) {
|
||||
$results->push($result);
|
||||
|
||||
// Add highlighting (optional)
|
||||
$docId = $doc->_documentid;
|
||||
if($res->highlighting && $res->highlighting->$docId) {
|
||||
// TODO Create decorator class for search results rather than adding arbitrary object properties
|
||||
// TODO Allow specifying highlighted field, and lazy loading
|
||||
// in case the search API needs another query (similar to SphinxSearchable->buildExcerpt()).
|
||||
$combinedHighlights = array();
|
||||
foreach($res->highlighting->$docId as $field => $highlights) {
|
||||
$combinedHighlights = array_merge($combinedHighlights, $highlights);
|
||||
}
|
||||
$result->Excerpt = implode(' ... ', $combinedHighlights);
|
||||
}
|
||||
}
|
||||
}
|
||||
$numFound = $res->response->numFound;
|
||||
} else {
|
||||
|
|
32
docs/Solr.md
32
docs/Solr.md
|
@ -253,6 +253,38 @@ from a new file `mysite/solr/templates/types.ss` instead:
|
|||
}
|
||||
}
|
||||
|
||||
### Highlighting
|
||||
|
||||
Solr can highlight the searched terms in context of the matched content,
|
||||
to help users determine the relevancy of results (e.g. in which part of a sentence
|
||||
the term is used). In order to use this feature, the full content of the
|
||||
field to be highlighted needs to be stored in the index,
|
||||
by declaring it through `addStoredField()`.
|
||||
|
||||
<?php
|
||||
class MyIndex extends SolrIndex {
|
||||
function init() {
|
||||
$this->addClass('Page');
|
||||
$this->addAllFulltextFields();
|
||||
$this->addStoredField('Content');
|
||||
}
|
||||
}
|
||||
|
||||
To search with highlighting enabled, you need to pass in a custom query parameter.
|
||||
There's a lot more parameters to tweak results on the [Solr Wiki](http://wiki.apache.org/solr/HighlightingParameters).
|
||||
|
||||
$index = new MyIndex();
|
||||
$query = new SearchQuery();
|
||||
$query->search('My Term');
|
||||
$results = $index->search($query, -1, -1, array('hl' => 'true'));
|
||||
|
||||
Each result will automatically contain an "Excerpt" property
|
||||
which you can use in your own results template.
|
||||
The searched term is highlighted with an `<em>` tag by default.
|
||||
|
||||
Note: It is recommended to strip out all HTML tags and convert entities on the indexed content,
|
||||
to avoid matching HTML attributes, and cluttering highlighted content with unparsed HTML.
|
||||
|
||||
## Debugging
|
||||
|
||||
### Using the web admin interface
|
||||
|
|
Loading…
Reference in New Issue