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 11 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
4 changes: 4 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGES

2013-08-14
- Added Rescore
- Set up structure to be easily extended for more types of rescores

2013-08-13
- Add a getQuery method on the FilteredQuery object

Expand Down
13 changes: 13 additions & 0 deletions lib/Elastica/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Elastica\Query\AbstractQuery;
use Elastica\Query\MatchAll;
use Elastica\Query\QueryString;
use Elastica\Rescore\AbstractRescore;

/**
* Elastica query object
Expand Down Expand Up @@ -100,6 +101,18 @@ public function setQuery(AbstractQuery $query)
return $this->setParam('query', $query->toArray());
}

/**
* Sets the rescore
*
* @param \Elastica\Rescore\AbstractRescore $rescore Rescore object
* @return \Elastica\Query Query object
*/
public function setRescore(AbstractRescore $rescore)
{
$data = $rescore->toArray();
return $this->setParam('rescore', $data['rescore']);
Copy link
Owner

Choose a reason for hiding this comment

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

Why do we have to use $data['rescore'] and not just $data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a side-effect of using Param as the base class of AbstractRescore

}

/**
* Gets the query array
*
Expand Down
77 changes: 77 additions & 0 deletions lib/Elastica/Rescore/QueryRescore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Elastica\Rescore;

use Elastica\Query as BaseQuery;

/**
* Rescore query
*
* @category Xodoa
* @package Elastica
* @author Jason Hu <mjhu91@gmail.com>
* @link http://www.elasticsearch.org/guide/reference/api/search/rescore/
*/
class QueryRescore extends AbstractRescore
{

/**
* Gets the query param
*
* @return array
*/
protected function getQueryParam()
{
if (!$this->hasParam('query'))
{
$this->setParam('query', array());
}

return $this->getParam('query');
}

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

$query['rescore_query'] = $data['query'];

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

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

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

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

return $this->setParam('query', $query);
}
}
19 changes: 19 additions & 0 deletions lib/Elastica/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elastica;

use Elastica\Exception\InvalidException;
use Elastica\Rescore\AbstractRescore;

/**
* Elastica search object
Expand Down Expand Up @@ -162,6 +163,24 @@ public function setQuery($query)
return $this;
}

/**
* Sets the rescore
*
* @param \Elastica\Rescore\AbstractRescore $rescore Rescore object
* @return \Elastica\Query Query object
*/
public function setRescore(AbstractRescore $rescore)
{
if (!isset($this->_query))
{
throw new InvalidException('Please set query before setting rescore');
}
$data = $rescore->toArray();
$this->_query->setParam('rescore', $data['rescore']);

return $this;
}

/**
* @param string $key
* @param mixed $value
Expand Down
56 changes: 56 additions & 0 deletions test/lib/Elastica/Test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Elastica\Query\Term;
use Elastica\Query\Text;
use Elastica\Query;
use Elastica\Query\Match;
use Elastica\Rescore\QueryRescore;
use Elastica\Client;
use Elastica\Test\Base as BaseTest;

class QueryTest extends BaseTest
Expand Down Expand Up @@ -174,4 +177,57 @@ public function testGetQuery()

$this->assertEquals($termQuery->toArray(), $query->getQuery());
}

public function testSetRescore()
{
$client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200))));
$index = $client->getIndex('elastica_test1');
$index->create(array(), true);

$mainQuery = new Match();
$mainQuery = $mainQuery->setFieldQuery('test1', 'foo');
$queryRescore = new QueryRescore();
$rescoreQuery = new Term();
$rescoreQuery = $rescoreQuery->setTerm('test2', 'bar', 2);
$queryRescore->setQuery($rescoreQuery);
$queryRescore->setWindowSize(50);
$queryRescore->setQueryWeight(.7);
$queryRescore->setRescoreQueryWeight(1.2);

$query = Query::create($mainQuery);
$query = $query->setRescore($queryRescore);

$response = $index->search($query)->getResponse();
$data = $response->getData();
$shards = $data['_shards'];
$failed = $shards['failed'];
$expectedData = 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($expectedData, $query->toArray());
$this->assertEquals(false, $response->hasError());
$this->assertEquals(0, $failed);
}
}
99 changes: 99 additions & 0 deletions test/lib/Elastica/Test/Rescore/QueryRescoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Elastica\Test;

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

class QueryRescoreTest extends BaseTest
{
public function testToArray()
{
$query = new QueryRescore();
$rescoreQuery = new Term();
$rescoreQuery = $rescoreQuery->setTerm('test2', 'bar', 2);
$query->setQuery($rescoreQuery);

$data = $query->toArray();
$expected = array(
'rescore' => array(
'query' => array(
'rescore_query' => array(
'term' => array(
'test2' => array(
'value' => 'bar',
'boost' => 2,
),
),
),
),
),
);

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

public function testSetSize()
{
$query = new QueryRescore();
$rescoreQuery = new Term();
$rescoreQuery = $rescoreQuery->setTerm('test2', 'bar', 2);
$query->setQuery($rescoreQuery);
$query->setWindowSize(50);

$data = $query->toArray();
$expected = array(
'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 QueryRescore();
$rescoreQuery = new Term();
$rescoreQuery = $rescoreQuery->setTerm('test2', 'bar', 2);
$query->setQuery($rescoreQuery);
$query->setWindowSize(50);
$query->setQueryWeight(.7);
$query->setRescoreQueryWeight(1.2);

$data = $query->toArray();
$expected = array(
'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);
}
}
58 changes: 58 additions & 0 deletions test/lib/Elastica/Test/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Elastica\Query;
use Elastica\Script;
use Elastica\Search;
use Elastica\Query\Match;
use Elastica\Query\Term;
use Elastica\Rescore\QueryRescore;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;

Expand Down Expand Up @@ -484,4 +487,59 @@ public function testCount() {
$this->assertInstanceOf('\Elastica\ResultSet', $result2);
$this->assertEquals(1, $result2->getTotalHits());
}

public function testSetRescore()
{
$client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200))));
$index = $client->getIndex('elastica_test1');
$index->create(array(), true);
$search = new Search($client);
$search = $search->addIndex($index);

$mainQuery = new Match();
$mainQuery = $mainQuery->setFieldQuery('test1', 'foo');
$queryRescore = new QueryRescore();
$rescoreQuery = new Term();
$rescoreQuery = $rescoreQuery->setTerm('test2', 'bar', 2);
$queryRescore->setQuery($rescoreQuery);
$queryRescore->setWindowSize(50);
$queryRescore->setQueryWeight(.7);
$queryRescore->setRescoreQueryWeight(1.2);

$search = $search->setQuery($mainQuery);
$search = $search->setRescore($queryRescore);

$response = $search->search()->getResponse();
$data = $response->getData();
$shards = $data['_shards'];
$failed = $shards['failed'];
$expectedData = 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($expectedData, $search->getQuery()->toArray());
$this->assertEquals(false, $response->hasError());
$this->assertEquals(0, $failed);
}
}