0

How do i use the following function? (For elastica in PHP with respect to Function Score query)

addScriptScoreFunction($script, $filter)

Does the filter filter out results or only score based on the script for those that pass the filter? How efficient is the scoring?

Also can i add more than one script score function to function score query?

1 Answer 1

1
$keyword = 'foo';
$fiels = 'name';

$inner_query = new Elastica\Query\Match();
$inner_query->setFieldQuery($field, $keyword);

// Wrap the function_score around the initial query

$scorefunction = new Elastica\Query\FunctionScore();
$scorefunction->setQuery($inner_query);
$scorefunction->setBoostMode('replace'); // Otherwise it will be multiplied with _score

// Make the custom score function: boost max 20% of initial _score, depending on popularity

$script = new Elastica\Script("_score + (doc['popularity'].value * 0.2 * _score)/100");
$scorefunction->addScriptScoreFunction($script);

// Last step: put that all in Elastica\Query and execute with Elastica\Search

There are some possible pitfalls:

  • without ->setBoostMode('replace'); the original _score will be multiplied with the result of the script. As in my case the addition was desired, therefore 'replace'.

  • It seems that divisions are rounded down. As the popularity that I used in my formula is allways between 1 and 100, thus popularity/100 alone was allways rounded down to 0 and the formula seemed to have no effect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.