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

Added Rescore Query #441

Merged
merged 24 commits into from
Mar 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7fdb558
added rescore query
jsonblob Aug 8, 2013
2711c13
added weight setters
jsonblob Aug 8, 2013
1ba471a
added rescore test stubs
jsonblob Aug 8, 2013
8627544
Finished rescore and tests
jsonblob Aug 11, 2013
574334e
fixed naming and added changes to changes.txt
jsonblob Aug 11, 2013
37f4b71
added elasticsearch test for rescore
jsonblob Aug 14, 2013
5f3d3f1
fixed changes.txt conflict
jsonblob Aug 14, 2013
26c7313
fixed naming convention again
jsonblob Aug 14, 2013
2ba6439
restructured rescore classes and added setRescore to both Query and S…
jsonblob Aug 14, 2013
1f0331a
updated changes.txt
jsonblob Aug 14, 2013
550c52b
removed some outdated changes
jsonblob Aug 14, 2013
c09bc15
Merge pull request #566 from jlinn/snapshot
ruflin Mar 8, 2014
df2c11a
Enable goecluster-facet again as now compatible
ruflin Mar 8, 2014
368c8e2
Run elasticsearch in the background to not have log output in travis …
ruflin Mar 8, 2014
5bb17ad
Set memache php version as environment variable
ruflin Mar 8, 2014
d0d6f11
Update to memcache 3.0.8 for travis
ruflin Mar 8, 2014
6297d82
Release v1.0.1.1
ruflin Mar 8, 2014
f32952d
added rescore query
jsonblob Aug 8, 2013
876c538
added weight setters
jsonblob Aug 8, 2013
c143f1a
added rescore test stubs
jsonblob Aug 8, 2013
b7dadc8
Finished rescore and tests
jsonblob Aug 11, 2013
674d280
Added all Rescore files
jsonblob Mar 8, 2014
42d727d
Merge branch 'master' into rescore
jsonblob Mar 8, 2014
ef9b125
Restructured to follow current scheme of Filters, Querys, etc. and ex…
jsonblob Mar 16, 2014
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ env:
- ES_TRANSPORT_THRIFT_VER=2.0.0.RC2
- ES_GEOCLUSTER_FACET_VER=0.0.10
- ES_WAIT_ON_MAPPING_CHANGE=true
- MEMCACHE_VER=3.0.8
matrix:
- ES_REQUIRE=dev
- ES_REQUIRE=no-dev
Expand Down
13 changes: 12 additions & 1 deletion changes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
CHANGES

2014-03-8
- Added Query\Rescore

2014-03-08
- Release v1.0.1.1
- Enable goecluster-facet again as now compatible with elasticsearch 1.0 on travis
- Run elasticsearch in the background to not have log output in travis build
- Set memache php version as environment variable
- Update to memcache 3.0.8 for travis

2014-03-07
- Add snapshot / restore functionality (Elastica\Snapshot) #566
- Issue #566: Add snapshot / restore functionality (Elastica\Snapshot)

2014-03-04
- Add PHP 5.6 to travis test environment
Expand Down
10 changes: 10 additions & 0 deletions lib/Elastica/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ public function setSuggest(Suggest $suggest)
$this->addParam(NULL, $suggest->toArray());
$this->_suggest = 1;
}

/**
* Add a Rescore
*
* @param \Elastica\Rescore\AbstractRescore $suggest suggestion object
*/
public function setRescore($rescore)
{
$this->setParam('rescore', $rescore->toArray());
}
}


Expand Down
36 changes: 36 additions & 0 deletions lib/Elastica/Rescore/AbstractRescore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Elastica\Rescore;
use Elastica\Param;

/**
* Abstract rescore object. Should be extended by all rescorers.
*
* @category Xodoa
* @package Elastica
* @author Jason Hu <mjhu91@gmail.com>
* @link http://www.elasticsearch.org/guide/reference/api/search/rescore/
*/
abstract class AbstractRescore extends Param
{
/**
* Overriden to return rescore as name
*
* @return string name
*/
protected function _getBaseName()
{
return 'rescore';
}

/**
* Sets window_size
*
* @param int $size
* @return \Elastica\Rescore
*/
public function setWindowSize($size)
{
return $this->setParam('window_size', $size);
}
}
90 changes: 90 additions & 0 deletions lib/Elastica/Rescore/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Elastica\Rescore;

use Elastica\Query as BaseQuery;

/**
* Query Rescore
*
* @category Xodoa
* @package Elastica
* @author Jason Hu <mjhu91@gmail.com>
* @link http://www.elasticsearch.org/guide/reference/api/search/rescore/
*/
class Query extends AbstractRescore
{
/**
* Constructor
*
* @param string|\Elastica\Query\AbstractQuery $rescoreQuery
* @param string|\Elastica\Query\AbstractQuery $query
*/
public function __construct($query = null)
{
$this->setParam('query', array());
$this->setRescoreQuery($query);
}

/**
* Override default implementation so params are in the format
* expected by elasticsearch
*
* @return array Rescore array
*/
public function toArray()
{
$data = $this->getParams();

if (!empty($this->_rawParams)) {
$data = array_merge($data, $this->_rawParams);
}

return $data;
}

/**
* Sets rescoreQuery object
*
* @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query
* @return \Elastica\Query\Rescore
*/
public function setRescoreQuery($rescoreQuery)
{
$query = BaseQuery::create($rescoreQuery);
$data = $query->toArray();

$query = $this->getParam('query');
$query['rescore_query'] = $data['query'];

return $this->setParam('query', $query);
}

/**
* Sets query_weight
*
* @param float $weight
* @return \Elastica\Query\Rescore
*/
public function setQueryWeight($weight)
{
$query = $this->getParam('query');
$query['query_weight'] = $weight;

return $this->setParam('query', $query);
}

/**
* Sets rescore_query_weight
*
* @param float $size
* @return \Elastica\Query\Rescore
*/
public function setRescoreQueryWeight($weight)
{
$query = $this->getParam('query');
$query['rescore_query_weight'] = $weight;

return $this->setParam('query', $query);
}
}
7 changes: 4 additions & 3 deletions test/bin/install_php_memcache.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

# Build and install PHP Memcache extension
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -xzf memcache-2.2.7.tgz
sh -c "cd memcache-2.2.7 && phpize && ./configure --enable-memcache && make && sudo make install"
wget http://pecl.php.net/get/memcache-${MEMCACHE_VER}.tgz
tar -xzf memcache-${MEMCACHE_VER}.tgz
sh -c "cd memcache-${MEMCACHE_VER} && phpize && ./configure --enable-memcache && make && sudo make install"
echo "extension=memcache.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

5 changes: 2 additions & 3 deletions test/bin/run_elasticsearch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ tar -xzf elasticsearch-${ES_VER}.tar.gz

elasticsearch-${ES_VER}/bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/${ES_MAPPER_ATTACHMENTS_VER}
elasticsearch-${ES_VER}/bin/plugin -install elasticsearch/elasticsearch-transport-thrift/${ES_TRANSPORT_THRIFT_VER}
# geocluster-facet has not been updated for ES 1.0.0.RC1
#elasticsearch-${ES_VER}/bin/plugin -install geocluster-facet --url https://github.com/zenobase/geocluster-facet/releases/download/${ES_GEOCLUSTER_FACET_VER}/geocluster-facet-${ES_GEOCLUSTER_FACET_VER}.jar
elasticsearch-${ES_VER}/bin/plugin -install geocluster-facet --url https://github.com/zenobase/geocluster-facet/releases/download/${ES_GEOCLUSTER_FACET_VER}/geocluster-facet-${ES_GEOCLUSTER_FACET_VER}.jar

export JAVA_OPTS="-server"

Expand Down Expand Up @@ -40,7 +39,7 @@ do

echo "Starting server on http port: $http_port"

elasticsearch-${ES_VER}/bin/elasticsearch -Des.config=$config_yml &
elasticsearch-${ES_VER}/bin/elasticsearch -d -Des.config=$config_yml &

while ! check_port_http_code $http_port 200; do
echo -n "."
Expand Down
165 changes: 165 additions & 0 deletions test/lib/Elastica/Test/Query/RescoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Elastica\Test\Query;

use Elastica\Rescore\Query as QueryRescore;
use Elastica\Query\Term;
use Elastica\Query\Match;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class RescoreTest extends BaseTest
{
/**
* @var Index
*/
protected $_index;

protected function setUp()
{
parent::setUp();
$this->_index = $this->_createIndex("rescore_test");
$this->_index->refresh();
}

public function testToArray()
{
$query = new Query();
$mainQuery = new Match();
$mainQuery = $mainQuery->setFieldQuery('test1', 'foo');
$secQuery = new Term();
$secQuery = $secQuery->setTerm('test2', 'bar', 2);
$queryRescore = new QueryRescore($secQuery);
$query->setQuery($mainQuery);
$query->setRescore($queryRescore);
$data = $query->toArray();

$expected = array(
'query' => array(
'match' => array(
'test1' => array(
'query' => 'foo',
),
),
),
'rescore' => array(
'query' => array(
'rescore_query' => array(
'term' => array(
'test2' => array(
'value' => 'bar',
'boost' => 2,
),
),
),
),
),
);

$this->assertEquals($expected, $data);
}

public function testSetSize()
{
$query = new Query();
$mainQuery = new Match();
$mainQuery = $mainQuery->setFieldQuery('test1', 'foo');
$secQuery = new Term();
$secQuery = $secQuery->setTerm('test2', 'bar', 2);
$queryRescore = new QueryRescore($secQuery);
$queryRescore->setWindowSize(50);
$query->setQuery($mainQuery);
$query->setRescore($queryRescore);
$data = $query->toArray();

$expected = array(
'query' => array(
'match' => array(
'test1' => array(
'query' => 'foo',
),
),
),
'rescore' => array(
'window_size' => 50,
'query' => array(
'rescore_query' => array(
'term' => array(
'test2' => array(
'value' => 'bar',
'boost' => 2,
),
),
),
),
),
);

$this->assertEquals($expected, $data);
}

public function testSetWeights()
{
$query = new Query();
$mainQuery = new Match();
$mainQuery = $mainQuery->setFieldQuery('test1', 'foo');
$secQuery = new Term();
$secQuery = $secQuery->setTerm('test2', 'bar', 2);
$queryRescore = new QueryRescore($secQuery);
$queryRescore->setWindowSize(50);
$queryRescore->setQueryWeight(.7);
$queryRescore->setRescoreQueryWeight(1.2);
$query->setQuery($mainQuery);
$query->setRescore($queryRescore);
$data = $query->toArray();

$expected = array(
'query' => array(
'match' => array(
'test1' => array(
'query' => 'foo',
),
),
),
'rescore' => array(
'window_size' => 50,
'query' => array(
'rescore_query' => array(
'term' => array(
'test2' => array(
'value' => 'bar',
'boost' => 2,
),
),
),
'query_weight' => 0.7,
'rescore_query_weight' => 1.2
),
),
);

$this->assertEquals($expected, $data);
}

public function testQuery()
{
$query = new Query();
$mainQuery = new Match();
$mainQuery = $mainQuery->setFieldQuery('test1', 'foo');
$secQuery = new Term();
$secQuery = $secQuery->setTerm('test2', 'bar', 2);
$queryRescore = new QueryRescore($secQuery);
$queryRescore->setWindowSize(50);
$queryRescore->setQueryWeight(.7);
$queryRescore->setRescoreQueryWeight(1.2);
$query->setQuery($mainQuery);
$query->setRescore($queryRescore);
$data = $query->toArray();

$results = $this->_index->search($query);
$response = $results->getResponse();

$this->assertEquals(true, $response->isOk());
$this->assertEquals(0, $results->getTotalHits());
}
}