From 78c5daa18613c04a6c0401b2257c101ab8f4631d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 3 Sep 2012 16:30:05 +0200 Subject: [PATCH] ENHANCEMENT Basic highlighting support --- code/solr/SolrIndex.php | 19 ++++++++++++++++--- docs/Solr.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php index e023b02..9dc9470 100644 --- a/code/solr/SolrIndex.php +++ b/code/solr/SolrIndex.php @@ -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(); diff --git a/docs/Solr.md b/docs/Solr.md index 08b456e..fd5af8f 100644 --- a/docs/Solr.md +++ b/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()`. + + 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 `` 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