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

Implement the weight in the function score query #735

Merged
merged 19 commits into from
Jan 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6

matrix:
allow_failures:
- php: 5.6

env:
matrix:
- ES_COMPOSER_NODEV=no
Expand Down
10 changes: 5 additions & 5 deletions ansible/es-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
hosts: localhost
connection: local
vars:
- ES_VER: "1.3.4"
- ES_SHORT_VER: "1.3"
- ES_MAPPER_ATTACHMENTS_VER: "2.3.2"
- ES_TRANSPORT_THRIFT_VER: "2.3.0"
- ES_GEOCLUSTER_FACET_VER: "0.0.11"
- ES_VER: "1.4.2"
- ES_SHORT_VER: "1.4"
- ES_MAPPER_ATTACHMENTS_VER: "2.4.1"
- ES_TRANSPORT_THRIFT_VER: "2.4.1"
- ES_GEOCLUSTER_FACET_VER: "0.0.12"
- ES_PROJECT_ROOT: "{{ lookup('env', 'ES_PROJECT_ROOT') }}"
- ES_COMPOSER_NODEV: "{{ lookup('env', 'ES_COMPOSER_NODEV') }}"
roles:
Expand Down
6 changes: 6 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGES

2015-01-19
- Update to elasticsearch 1.4.2
- Remove support for PHP 5.3

2015-01-14
- added @return annotation to top_hits aggregation DSL method

Expand Down Expand Up @@ -31,6 +35,8 @@ CHANGES

2014-11-16
- Added Elastica\QueryBuilder #724
- Update to elasticsearch 1.4.0
- Disable official support for PHP 5.3

2014-11-13
- fixed reserved words in queries which composed of upper case letters (Util::replaceBooleanWords) #722
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"psr/log": "~1.0"
},
"require-dev": {
"psr/log": "~1.0",
"munkie/elasticsearch-thrift-php": "1.4.*",
"phpunit/phpunit": "4.1.*",
"satooshi/php-coveralls": "dev-master"
Expand Down
2 changes: 1 addition & 1 deletion lib/Elastica/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function addFile($key, $filepath, $mimeType = '')
$value = base64_encode(file_get_contents($filepath));

if (!empty($mimeType)) {
$value = array('_content_type' => $mimeType, '_name' => $filepath, 'content' => $value);
$value = array('_content_type' => $mimeType, '_name' => $filepath, '_content' => $value);
}

$this->set($key, $value);
Expand Down
78 changes: 54 additions & 24 deletions lib/Elastica/Index/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elastica\Index;

use Elastica\Exception\NotFoundException;
use Elastica\Exception\ResponseException;
use Elastica\Index as BaseIndex;
use Elastica\Request;

Expand Down Expand Up @@ -68,31 +70,34 @@ public function get($setting = '')
$requestData = $this->request()->getData();
$data = reset($requestData);

if (empty($data['settings']) || empty($data['settings']['index'])) {
// should not append, the request should throw a ResponseException
throw new NotFoundException('Index ' . $this->getIndex()->getName() . ' not found');
}
$settings = $data['settings']['index'];

if (!empty($setting)) {
if (isset($settings[$setting])) {
return $settings[$setting];
} else {
if (strpos($setting, '.') !== false) {
// translate old dot-notation settings to nested arrays
$keys = explode('.', $setting);
foreach ($keys as $key) {
if (isset($settings[$key])) {
$settings = $settings[$key];
} else {
return;
}
}
if (!$setting) {
// return all array
return $settings;
}

return $settings;
if (isset($settings[$setting])) {
return $settings[$setting];
} else {
if (strpos($setting, '.') !== false) {
// translate old dot-notation settings to nested arrays
$keys = explode('.', $setting);
foreach ($keys as $key) {
if (isset($settings[$key])) {
$settings = $settings[$key];
} else {
return null;
}
}

return;
return $settings;
}
return null;
}

return $settings;
}

/**
Expand All @@ -118,7 +123,18 @@ public function setNumberOfReplicas($replicas)
*/
public function setReadOnly($readOnly = true)
{
return $this->set(array('blocks.read_only' => $readOnly));
return $this->set(array('blocks.write' => $readOnly));
}

/**
* getReadOnly
*
* @access public
* @return bool
*/
public function getReadOnly()
{
return $this->get('blocks.write') === 'true'; // ES returns a string for this setting
}

/**
Expand Down Expand Up @@ -156,15 +172,26 @@ public function setBlocksWrite($state = true)
{
$state = $state ? 1 : 0;

return $this->set(array('blocks.write' => (int) $state));
return $this->set(array('blocks.write' => $state));
}

/**
* @return bool
*/
public function getBlocksMetadata()
{
return (bool) $this->get('blocks.metadata');
// TODO will have to be replace by block.metadata.write once https://github.com/elasticsearch/elasticsearch/pull/9203 has been fixed
// the try/catch will have to be remove too
try {
return (bool) $this->get('blocks.metadata');
} catch (ResponseException $e) {
if (strpos($e->getMessage(), 'ClusterBlockException') !== false) {
// hacky way to test if the metadata is blocked since bug 9203 is not fixed
return true;
} else {
throw $e;
}
}
}

/**
Expand All @@ -173,9 +200,10 @@ public function getBlocksMetadata()
*/
public function setBlocksMetadata($state = true)
{
// TODO will have to be replace by block.metadata.write once https://github.com/elasticsearch/elasticsearch/pull/9203 has been fixed
$state = $state ? 1 : 0;

return $this->set(array('blocks.metadata' => (int) $state));
return $this->set(array('blocks.metadata' => $state));
}

/**
Expand Down Expand Up @@ -314,7 +342,9 @@ public function request(array $data = array(), $method = Request::GET)
{
$path = '_settings';

$data = array('index' => $data);
if (!empty($data)) {
$data = array('index' => $data);
}

return $this->getIndex()->request($path, $method, $data);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Elastica/Index/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function getAliases()
{
$responseData = $this->getIndex()->request('_aliases', \Elastica\Request::GET)->getData();

return array_keys($responseData[$this->getIndex()->getName()]['aliases']);
$data = $responseData[$this->getIndex()->getName()];
if (!empty($data['aliases'])) {
return array_keys($data['aliases']);
}
return [];
}

/**
Expand Down
55 changes: 40 additions & 15 deletions lib/Elastica/Query/FunctionScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,35 @@ public function setFilter(AbstractFilter $filter)
* @param string $functionType valid values are DECAY_* constants and script_score
* @param array|float $functionParams the body of the function. See documentation for proper syntax.
* @param AbstractFilter $filter optional filter to apply to the function
* @param float $weight function weight
* @return \Elastica\Query\FunctionScore
*/
public function addFunction($functionType, $functionParams, AbstractFilter $filter = null)
public function addFunction($functionType, $functionParams, AbstractFilter $filter = null, $weight = null)
{
$function = array(
$functionType => $functionParams,
);
if (!is_null($filter)) {
$function['filter'] = $filter->toArray();
}
$this->_functions[] = $function;
if ($weight !== null) {
$function['weight'] = $weight;
}

$this->_functions[] = $function;
return $this;
}

/**
* Add a script_score function to the query
* @param Script $script a Script object
* @param AbstractFilter $filter an optional filter to apply to the function
* @param float $weight the weight of the function
* @return \Elastica\Query\FunctionScore
*/
public function addScriptScoreFunction(Script $script, AbstractFilter $filter = null)
public function addScriptScoreFunction(Script $script, AbstractFilter $filter = null, $weight = null)
{
return $this->addFunction('script_score', $script->toArray(), $filter);
return $this->addFunction('script_score', $script->toArray(), $filter, $weight);
}

/**
Expand All @@ -91,12 +96,20 @@ public function addScriptScoreFunction(Script $script, AbstractFilter $filter =
* @param string $offset If defined, this function will only be computed for documents with a distance from the origin greater than this value
* @param float $decay optionally defines how documents are scored at the distance given by the $scale parameter
* @param float $scaleWeight optional factor by which to multiply the score at the value provided by the $scale parameter
* @param float $weight optional factor by which to multiply the score at the value provided by the $scale parameter
* @param AbstractFilter $filter a filter associated with this function
* @return \Elastica\Query\FunctionScore
*/
public function addDecayFunction($function, $field, $origin, $scale, $offset = null, $decay = null, $scaleWeight = null,
AbstractFilter $filter = null)
{
public function addDecayFunction(
$function,
$field,
$origin,
$scale,
$offset = null,
$decay = null,
$weight = null,
AbstractFilter $filter = null
) {
$functionParams = array(
$field => array(
'origin' => $origin,
Expand All @@ -109,32 +122,44 @@ public function addDecayFunction($function, $field, $origin, $scale, $offset = n
if (!is_null($decay)) {
$functionParams[$field]['decay'] = (float) $decay;
}
if (!is_null($scaleWeight)) {
$functionParams[$field]['scale_weight'] = (float) $scaleWeight;
}

return $this->addFunction($function, $functionParams, $filter);
return $this->addFunction($function, $functionParams, $filter, $weight);
}

/**
* Add a boost_factor function to the query
*
* @param float $boostFactor the boost factor value
* @param AbstractFilter $filter a filter associated with this function
*
* @return void
* @deprecated
*/
public function addBoostFactorFunction($boostFactor, AbstractFilter $filter = null)
{
$this->addFunction('boost_factor', $boostFactor, $filter);
$this->addWeightFunction($boostFactor, $filter);
}

/**
* @param float $weight the weight of the function
* @param AbstractFilter $filter a filter associated with this function
*
* @return void
*/
public function addWeightFunction($weight, AbstractFilter $filter = null)
{
$this->addFunction('weight', $weight, $filter);
}

/**
* Add a random_score function to the query
* @param number $seed the seed value
* @param AbstractFilter $filter a filter associated with this function
* @param float $boost an optional boost value associated with this function
* @param float $weight an optional boost value associated with this function
*/
public function addRandomScoreFunction($seed, AbstractFilter $filter = null, $boost = null)
public function addRandomScoreFunction($seed, AbstractFilter $filter = null, $weight = null)
{
$this->addFunction('random_score', array('seed' => $seed), $filter, $boost);
$this->addFunction('random_score', array('seed' => $seed), $filter, $weight);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions lib/Elastica/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ public function getIndexStatuses()
*/
public function getIndexNames()
{
$names = array();
foreach ($this->_data['indices'] as $name => $data) {
$names[] = $name;
}

return $names;
return array_keys($this->_data['indices']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/benchmark/BulkMemoryUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public function testServersArray()

$endMemory = memory_get_usage();

$this->assertLessThan(1.2, $endMemory/$startMemory);
$this->assertLessThan(1.3, $endMemory/$startMemory);
}
}
14 changes: 14 additions & 0 deletions test/lib/Elastica/Test/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elastica\Test;

use Elastica\Client;
use Elastica\Index;

class Base extends \PHPUnit_Framework_TestCase
{
Expand All @@ -28,4 +29,17 @@ protected function _createIndex($name = 'test', $delete = true, $shards = 1)

return $index;
}

protected function _waitForAllocation(Index $index)
{
do {
$settings = $index->getStatus()->get();
$allocated = true;
foreach ($settings['shards'] as $shard) {
if ($shard[0]['routing']['state'] != 'STARTED') {
$allocated = false;
}
}
} while (!$allocated);
}
}
Loading