From 50f53e7bac0a9bb0d384f4c6e3225ca9b8910c4a Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 5 May 2015 19:44:01 +1200 Subject: [PATCH] Fix highlight support when querying by fields (or boosting fields) Highlighting will simply be empty otherwise, presumably because it can't match against the complex query term in the "q" parameter. --- code/solr/SolrIndex.php | 13 +++++++++++-- tests/SolrIndexTest.php | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/code/solr/SolrIndex.php b/code/solr/SolrIndex.php index 43991fb..0f3abbe 100644 --- a/code/solr/SolrIndex.php +++ b/code/solr/SolrIndex.php @@ -369,9 +369,10 @@ abstract class SolrIndex extends SearchIndex { $q = array(); $fq = array(); + $hlq = array(); // Build the search itself - + foreach ($query->search as $search) { $text = $search['text']; preg_match_all('/"[^"]*"|\S+/', $text, $parts); @@ -380,7 +381,9 @@ abstract class SolrIndex extends SearchIndex { foreach ($parts[0] as $part) { $fields = (isset($search['fields'])) ? $search['fields'] : array(); - if(isset($search['boost'])) $fields = array_merge($fields, array_keys($search['boost'])); + if(isset($search['boost'])) { + $fields = array_merge($fields, array_keys($search['boost'])); + } if ($fields) { $searchq = array(); foreach ($fields as $field) { @@ -392,8 +395,14 @@ abstract class SolrIndex extends SearchIndex { else { $q[] = '+'.$part.$fuzzy; } + $hlq[] = $part; } } + // If using boosting, set the clean term separately for highlighting. + // See https://issues.apache.org/jira/browse/SOLR-2632 + if(array_key_exists('hl', $params) && !array_key_exists('hl.q', $params)) { + $params['hl.q'] = implode(' ', $hlq); + } // Filter by class if requested diff --git a/tests/SolrIndexTest.php b/tests/SolrIndexTest.php index b3aee60..9f45203 100644 --- a/tests/SolrIndexTest.php +++ b/tests/SolrIndexTest.php @@ -64,6 +64,48 @@ class SolrIndexTest extends SapphireTest { Phockito::verify($serviceMock)->search('+(Field1:term^1.5 OR HasOneObject_Field1:term^3)', anything(), anything(), anything(), anything()); } + function testHighlightQueryOnBoost() { + $serviceMock = $this->getServiceMock(); + Phockito::when($serviceMock)->search(anything(), anything(), anything(), anything(), anything())->return($this->getFakeRawSolrResponse()); + + $index = new SolrIndexTest_FakeIndex(); + $index->setService($serviceMock); + + // Search without highlighting + $query = new SearchQuery(); + $query->search( + 'term', + null, + array('Field1' => 1.5, 'HasOneObject_Field1' => 3) + ); + $index->search($query); + Phockito::verify( + $serviceMock)->search( + '+(Field1:term^1.5 OR HasOneObject_Field1:term^3)', + anything(), + anything(), + not(hasKeyInArray('hl.q')), + anything() + ); + + // Search with highlighting + $query = new SearchQuery(); + $query->search( + 'term', + null, + array('Field1' => 1.5, 'HasOneObject_Field1' => 3) + ); + $index->search($query, -1, -1, array('hl' => true)); + Phockito::verify( + $serviceMock)->search( + '+(Field1:term^1.5 OR HasOneObject_Field1:term^3)', + anything(), + anything(), + hasKeyInArray('hl.q'), + anything() + ); + } + function testIndexExcludesNullValues() { $serviceMock = $this->getServiceMock(); $index = new SolrIndexTest_FakeIndex();