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.
This commit is contained in:
Ingo Schommer 2015-05-05 19:44:01 +12:00
parent ff62c2b930
commit 50f53e7bac
2 changed files with 53 additions and 2 deletions

View File

@ -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

View File

@ -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();