mirror of
https://github.com/silverstripe/silverstripe-fulltextsearch
synced 2024-10-22 14:05:29 +02:00
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:
parent
ff62c2b930
commit
50f53e7bac
@ -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
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user