mirror of
https://github.com/silverstripe/silverstripe-fulltextsearch
synced 2024-10-22 12:05:29 +00:00
ENHANCEMENT Basic highlighting support
This commit is contained in:
parent
2a3e882d70
commit
78c5daa186
@ -269,10 +269,23 @@ abstract class SolrIndex extends SearchIndex {
|
||||
$res = $service->search($q ? implode(' ', $q) : '*:*', $offset, $limit, array('fq' => implode(' ', $fq)), Apache_Solr_Service::METHOD_POST);
|
||||
|
||||
$results = new ArrayList();
|
||||
|
||||
foreach ($res->response->docs as $doc) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ret = array();
|
||||
|
32
docs/Solr.md
32
docs/Solr.md
@ -76,6 +76,38 @@ You can also copy the `thirdparty/`solr directory somewhere else,
|
||||
just set the path value in `mysite/_config.php` to point to the new location.
|
||||
And of course run `java -jar start.jar` from the new directory.
|
||||
|
||||
### 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…
x
Reference in New Issue
Block a user