mirror of
https://github.com/silverstripe/silverstripe-fulltextsearch
synced 2024-06-17 02:01:30 +02:00
Merge pull request #63 from chillu/pulls/fix-highlight-support
Fix highlight support when querying by fields (or boosting fields)
This commit is contained in:
commit
1608d8384d
|
@ -369,9 +369,10 @@ abstract class SolrIndex extends SearchIndex {
|
||||||
|
|
||||||
$q = array();
|
$q = array();
|
||||||
$fq = array();
|
$fq = array();
|
||||||
|
$hlq = array();
|
||||||
|
|
||||||
// Build the search itself
|
// Build the search itself
|
||||||
|
|
||||||
foreach ($query->search as $search) {
|
foreach ($query->search as $search) {
|
||||||
$text = $search['text'];
|
$text = $search['text'];
|
||||||
preg_match_all('/"[^"]*"|\S+/', $text, $parts);
|
preg_match_all('/"[^"]*"|\S+/', $text, $parts);
|
||||||
|
@ -380,7 +381,9 @@ abstract class SolrIndex extends SearchIndex {
|
||||||
|
|
||||||
foreach ($parts[0] as $part) {
|
foreach ($parts[0] as $part) {
|
||||||
$fields = (isset($search['fields'])) ? $search['fields'] : array();
|
$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) {
|
if ($fields) {
|
||||||
$searchq = array();
|
$searchq = array();
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
|
@ -392,8 +395,14 @@ abstract class SolrIndex extends SearchIndex {
|
||||||
else {
|
else {
|
||||||
$q[] = '+'.$part.$fuzzy;
|
$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
|
// 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());
|
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() {
|
function testIndexExcludesNullValues() {
|
||||||
$serviceMock = $this->getServiceMock();
|
$serviceMock = $this->getServiceMock();
|
||||||
$index = new SolrIndexTest_FakeIndex();
|
$index = new SolrIndexTest_FakeIndex();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user