Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature (#28): Add SolrQuery::setHighlightQuery(string $q) #62

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add SolrQuery SolrQuery::setHighlightQuery(string $q) (#28)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to mention also getHighlightQuery()

- Fix compile error: libcurl on linux multiarch support (#46)
- Fix SegFault in SolrClient::optimize() (debug mode)
- Fix parsed parameter types (#37)
Expand Down
13 changes: 11 additions & 2 deletions docs/documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,15 @@ public function setFacetSort($facetSort, $field_override = null) {}
* @link http://docs.php.net/manual/en/solrquery.sethighlight.php
*/
public function setHighlight($flag) {}

/**
* A query to use for highlighting. This parameter allows you to highlight different terms than those being used to retrieve documents.
*
* @param string $q
* @return SolrQuery
* @link http://docs.php.net/manual/en/solrquery.sethighlightquery.php
*/
public function setHighlightQuery($q) {}
omars44 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Specifies the highlithing backup field to use
Expand Down Expand Up @@ -2431,11 +2440,11 @@ public function setOmitHeader($flag) {}
/**
* Sets the search query
*
* @param string $query
* @param string $q
* @return SolrQuery Returns the current SolrQuery object
* @link http://docs.php.net/manual/en/solrquery.setquery.php
*/
public function setQuery($query) {}
public function setQuery($q) {}

/**
* Specifies the maximum number of rows to return in the result
Expand Down
4 changes: 3 additions & 1 deletion examples/solrquery_004_highlight.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

$query = new SolrQuery();

$query->setHighlight(0);
$query->setHighlight(true);

$query->setHighlightQuery('title:PHP OR description:Solr');

$query->addHighlightField('a')->addHighlightField('a')->addHighlightField('a');

Expand Down
2 changes: 2 additions & 0 deletions src/php7/php_solr.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,8 @@ static zend_function_entry solr_query_methods[] = {
/* HighlightingParameters */
PHP_ME(SolrQuery, setHighlight, SolrQuery_hl_1_1_args, ZEND_ACC_PUBLIC)
PHP_ME(SolrQuery, getHighlight, Solr_no_args, ZEND_ACC_PUBLIC)
PHP_ME(SolrQuery, setHighlightQuery, SolrQuery_setQuery_args, ZEND_ACC_PUBLIC)
PHP_ME(SolrQuery, getHighlightQuery, Solr_no_args, ZEND_ACC_PUBLIC)
PHP_ME(SolrQuery, addHighlightField, SolrQuery_hl_1_1_args, ZEND_ACC_PUBLIC)
PHP_ME(SolrQuery, removeHighlightField, SolrQuery_hl_1_1_args, ZEND_ACC_PUBLIC)
PHP_ME(SolrQuery, getHighlightFields, Solr_no_args, ZEND_ACC_PUBLIC)
Expand Down
2 changes: 2 additions & 0 deletions src/php7/php_solr.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ PHP_METHOD(SolrQuery, getExpandFilterQueries);
/* HighlightingParameters */
PHP_METHOD(SolrQuery, setHighlight);
PHP_METHOD(SolrQuery, getHighlight);
PHP_METHOD(SolrQuery, setHighlightQuery);
PHP_METHOD(SolrQuery, getHighlightQuery);
PHP_METHOD(SolrQuery, addHighlightField);
PHP_METHOD(SolrQuery, removeHighlightField);
PHP_METHOD(SolrQuery, getHighlightFields);
Expand Down
42 changes: 42 additions & 0 deletions src/php7/php_solr_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,31 @@ PHP_METHOD(SolrQuery, setHighlight)
}
/* }}} */

/* {{{ proto SolrQuery SolrQuery::setHighlightQuery(string q)
Sets the hl.q parameter */
PHP_METHOD(SolrQuery, setHighlightQuery)
{
solr_char_t *param_name = (solr_char_t *) "hl.q";
COMPAT_ARG_SIZE_T param_name_len = sizeof("hl.q")-1;
solr_char_t *param_value = NULL;
COMPAT_ARG_SIZE_T param_value_len = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param_value, &param_value_len) == FAILURE) {

php_error_docref(NULL, E_WARNING, "Invalid parameters");

RETURN_NULL();
}

if (solr_set_normal_param(getThis(), param_name, param_name_len, param_value, param_value_len) == FAILURE) {

RETURN_NULL();
}
solr_return_solr_params_object();
}
/* }}} */


/* {{{ proto SolrQuery SolrQuery::addHighlightField(string value)
Adds another hl.fl parameter. */
PHP_METHOD(SolrQuery, addHighlightField)
Expand Down Expand Up @@ -4563,6 +4588,23 @@ PHP_METHOD(SolrQuery, getHighlight)
}
/* }}} */

/* {{{ proto string SolrQuery::getHighlightQuery()
Returns the query */
PHP_METHOD(SolrQuery, getHighlightQuery)
{
solr_char_t *param_name = (solr_char_t *) "hl.q";
COMPAT_ARG_SIZE_T param_name_len = sizeof("hl.q")-1;
solr_param_t *solr_param = NULL;

if (solr_param_find(getThis(), param_name, param_name_len, (solr_param_t **) &solr_param) == FAILURE) {

RETURN_NULL();
}

solr_normal_param_value_display_string(solr_param, return_value);
}
/* }}} */

/* {{{ proto array SolrQuery::getHighlightFields()
Returns the parameter */
PHP_METHOD(SolrQuery, getHighlightFields)
Expand Down
9 changes: 6 additions & 3 deletions tests/063.solrquery_HighlightingParameters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $query = new SolrQuery();
ob_start();
var_dump(
$query->getHighlight(),
$query->getHighlightQuery(),
$query->getHighlightFields(),
$query->getHighlightSnippets(),
$query->getHighlightSnippets('june'),
Expand Down Expand Up @@ -38,7 +39,8 @@ var_dump(
$nullOutput = ob_get_clean();


$query->setHighlight(0);
$query->setHighlight(true);
$query->setHighlightQuery('title:PHP OR description:Solr');

$query->addHighlightField('a')->addHighlightField('a')->addHighlightField('a')
->addHighlightField('b');
Expand Down Expand Up @@ -115,9 +117,9 @@ var_dump(
echo PHP_EOL.$nullOutput;
?>
--EXPECT--
hl=false&hl.fl=b&hl.snippets=4&f.june.hl.snippets=5&hl.fragsize=41&f.june.hl.fragsize=52&hl.mergeContiguous=true&f.june.hl.mergeContiguous=false&hl.requireFieldMatch=true&hl.maxAnalyzedChars=53&hl.alternateField=a&f.june.hl.alternateField=b&hl.maxAlternateFieldLength=300&f.june.hl.maxAlternateFieldLength=335&hl.formatter=F&f.june.hl.formatter=G&hl.simple.pre=OO&f.june.hl.simple.pre=PP&hl.simple.post=ZZ&f.june.hl.simple.post=YY&hl.fragmenter=gap&f.june.hl.fragmenter=space&hl.usePhraseHighlighter=false&hl.highlightMultiTerm=true&hl.regex.slop=0.0025&hl.regex.pattern=ekpo&hl.regex.maxAnalyzedChars=5
hl=true&hl.q=title:PHP OR description:Solr&hl.fl=b&hl.snippets=4&f.june.hl.snippets=5&hl.fragsize=41&f.june.hl.fragsize=52&hl.mergeContiguous=true&f.june.hl.mergeContiguous=false&hl.requireFieldMatch=true&hl.maxAnalyzedChars=53&hl.alternateField=a&f.june.hl.alternateField=b&hl.maxAlternateFieldLength=300&f.june.hl.maxAlternateFieldLength=335&hl.formatter=F&f.june.hl.formatter=G&hl.simple.pre=OO&f.june.hl.simple.pre=PP&hl.simple.post=ZZ&f.june.hl.simple.post=YY&hl.fragmenter=gap&f.june.hl.fragmenter=space&hl.usePhraseHighlighter=false&hl.highlightMultiTerm=true&hl.regex.slop=0.0025&hl.regex.pattern=ekpo&hl.regex.maxAnalyzedChars=5

bool(false)
bool(true)
array(1) {
[0]=>
string(1) "b"
Expand Down Expand Up @@ -175,3 +177,4 @@ NULL
NULL
NULL
NULL
NULL
Loading