Skip to content
Snippets Groups Projects
Commit 2136b734 authored by Demian Katz's avatar Demian Katz
Browse files

More refinements to hl.q behavior.

parent fd4e8b51
No related merge requests found
...@@ -105,6 +105,10 @@ class InjectHighlightingListener ...@@ -105,6 +105,10 @@ class InjectHighlightingListener
$params->set('hl.fl', '*'); $params->set('hl.fl', '*');
$params->set('hl.simple.pre', '{{{{START_HILITE}}}}'); $params->set('hl.simple.pre', '{{{{START_HILITE}}}}');
$params->set('hl.simple.post', '{{{{END_HILITE}}}}'); $params->set('hl.simple.post', '{{{{END_HILITE}}}}');
// Turn on hl.q generation in query builder:
$this->backend->getQueryBuilder()
->setCreateHighlightingQuery(true);
} }
} }
} }
......
...@@ -75,17 +75,24 @@ class QueryBuilder implements QueryBuilderInterface ...@@ -75,17 +75,24 @@ class QueryBuilder implements QueryBuilderInterface
/** /**
* Force ranges to uppercase? * Force ranges to uppercase?
* *
* @var boolean * @var bool
*/ */
public $caseSensitiveRanges = true; public $caseSensitiveRanges = true;
/** /**
* Force boolean operators to uppercase? * Force boolean operators to uppercase?
* *
* @var boolean * @var bool
*/ */
public $caseSensitiveBooleans = true; public $caseSensitiveBooleans = true;
/**
* Should we create the hl.q parameter when appropriate?
*
* @var bool
*/
public $createHighlightingQuery = false;
/** /**
* Constructor. * Constructor.
* *
...@@ -129,7 +136,7 @@ class QueryBuilder implements QueryBuilderInterface ...@@ -129,7 +136,7 @@ class QueryBuilder implements QueryBuilderInterface
// If a boost was added, we don't want to highlight based on // If a boost was added, we don't want to highlight based on
// the boost query, so we should use the non-boosted version: // the boost query, so we should use the non-boosted version:
if ($oldString != $string) { if ($this->createHighlightingQuery && $oldString != $string) {
$params->set('hl.q', $oldString); $params->set('hl.q', $oldString);
} }
} }
...@@ -155,12 +162,26 @@ class QueryBuilder implements QueryBuilderInterface ...@@ -155,12 +162,26 @@ class QueryBuilder implements QueryBuilderInterface
return $params; return $params;
} }
/**
* Control whether or not the QueryBuilder should create an hl.q parameter
* when the main query includes clauses that should not be factored into
* highlighting. (Turned off by default).
*
* @param bool $enable Should highlighting query generation be enabled?
*
* @return void
*/
public function setCreateHighlightingQuery($enable)
{
$this->createHighlightingQuery = $enable;
}
/** /**
* Return true if the search string contains advanced Lucene syntax. * Return true if the search string contains advanced Lucene syntax.
* *
* @param string $searchString Search string * @param string $searchString Search string
* *
* @return boolean * @return bool
* *
* @todo Maybe factor out to dedicated UserQueryAnalyzer * @todo Maybe factor out to dedicated UserQueryAnalyzer
*/ */
......
...@@ -56,4 +56,14 @@ interface QueryBuilderInterface ...@@ -56,4 +56,14 @@ interface QueryBuilderInterface
*/ */
public function build(AbstractQuery $query); public function build(AbstractQuery $query);
/**
* Control whether or not the QueryBuilder should create an hl.q parameter
* when the main query includes clauses that should not be factored into
* highlighting. (Turned off by default).
*
* @param bool $enable Should highlighting query generation be enabled?
*
* @return void
*/
public function setCreateHighlightingQuery($enable);
} }
\ No newline at end of file
...@@ -190,4 +190,35 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase ...@@ -190,4 +190,35 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase
$this->assertEquals($output, $processedQ[0]); $this->assertEquals($output, $processedQ[0]);
} }
} }
/**
* Test generation with highlighting
*
* @return void
*/
public function testHighlighting()
{
$qb = new QueryBuilder(
array(
'test' => array(
'DismaxFields' => array('test1'),
'DismaxParams' => array(array('bq', 'boost'))
)
)
);
$q = new Query('*:*', 'test');
// No hl.q if highlighting query disabled:
$qb->setCreateHighlightingQuery(false);
$response = $qb->build($q);
$hlQ = $response->get('hl.q');
$this->assertEquals(null, $hlQ[0]);
// hl.q if highlighting query enabled:
$qb->setCreateHighlightingQuery(true);
$response = $qb->build($q);
$hlQ = $response->get('hl.q');
$this->assertEquals('*:*', $hlQ[0]);
}
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment