Skip to content

Commit c622ccf

Browse files
committed
Merge pull request #626 from Yitsushi/625-implement-boosting-query
Implement Boosting Query #625
2 parents ff18b9c + 3d659ee commit c622ccf

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

changes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
CHANGES
22

3+
2014-06-04
4+
- Implement Boosting Query (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html) #625
5+
36
2014-06-02
47
- add retry_on_conflict support to bulk #623
58

lib/Elastica/Query/Boosting.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Elastica\Query;
4+
5+
/**
6+
* Class Boosting
7+
* @package Elastica\Query
8+
* @author Balazs Nadasdi <[email protected]>
9+
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html
10+
*/
11+
class Boosting extends AbstractQuery
12+
{
13+
const NEGATIVE_BOOST = 0.2;
14+
15+
/**
16+
* Set the positive query for this Boosting Query
17+
* @param AbstractQuery $query
18+
* @return \Elastica\Query\Boosting
19+
*/
20+
public function setPositiveQuery(AbstractQuery $query)
21+
{
22+
return $this->setParam('positive', $query->toArray());
23+
}
24+
25+
/**
26+
* Set the negative query for this Boosting Query
27+
* @param AbstractQuery $query
28+
* @return \Elastica\Query\Boosting
29+
*/
30+
public function setNegativeQuery(AbstractQuery $query)
31+
{
32+
return $this->setParam('negative', $query->toArray());
33+
}
34+
35+
/**
36+
* Set the negative_boost parameter for this Boosting Query
37+
* @param Float $negativeBoost
38+
* @return \Elastica\Query\Boosting
39+
*/
40+
public function setNegativeBoost($negativeBoost)
41+
{
42+
return $this->setParam('negative_boost', (float)$negativeBoost);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Elastica\Test\Query;
4+
5+
use Elastica\Document;
6+
use Elastica\Query\Boosting;
7+
use Elastica\Test\Base as BaseTest;
8+
9+
class BoostingTest extends BaseTest
10+
{
11+
/**
12+
* @var \Elastica\Index
13+
*/
14+
protected $index;
15+
16+
/**
17+
* @var \Elastica\Type
18+
*/
19+
protected $type;
20+
21+
/*
22+
* @var array
23+
*/
24+
protected $sampleData;
25+
26+
protected function setUp()
27+
{
28+
parent::setUp();
29+
$this->index = $this->_createIndex('test_boostingquery');
30+
$this->type = $this->index->getType('test');
31+
$this->type->setMapping(array(
32+
'name' => array('type' => 'string', 'index' => 'analyzed'),
33+
'price' => array('type' => 'float')
34+
));
35+
36+
$this->sampleData = array(
37+
array("name" => "Vital Lama", "price" => 5.2),
38+
array("name" => "Vital Match", "price" => 2.1),
39+
array("name" => "Mercury Vital", "price" => 7.5),
40+
array("name" => "Fist Mercury", "price" => 3.8),
41+
array("name" => "Lama Vital 2nd", "price" => 3.2)
42+
);
43+
44+
foreach($this->sampleData as $key => $value) {
45+
$this->type->addDocument(new Document($key, $value));
46+
}
47+
48+
$this->index->refresh();
49+
}
50+
51+
protected function tearDown()
52+
{
53+
$this->index->delete();
54+
parent::tearDown();
55+
}
56+
57+
public function testToArray()
58+
{
59+
$keyword = "vital";
60+
$negativeKeyword = "Mercury";
61+
62+
$query = new Boosting();
63+
$positiveQuery = new \Elastica\Query\Term(array('name' => $keyword));
64+
$negativeQuery = new \Elastica\Query\Term(array('name' => $negativeKeyword));
65+
$query->setPositiveQuery($positiveQuery);
66+
$query->setNegativeQuery($negativeQuery);
67+
$query->setNegativeBoost(0.3);
68+
69+
$expected = array(
70+
'boosting' => array(
71+
'positive' => $positiveQuery->toArray(),
72+
'negative' => $negativeQuery->toArray(),
73+
'negative_boost' => 0.3
74+
)
75+
);
76+
$this->assertEquals($expected, $query->toArray());
77+
}
78+
79+
public function testNegativeBoost()
80+
{
81+
$keyword = "vital";
82+
$negativeKeyword = "mercury";
83+
84+
$query = new Boosting();
85+
$positiveQuery = new \Elastica\Query\Term(array('name' => $keyword));
86+
$negativeQuery = new \Elastica\Query\Term(array('name' => $negativeKeyword));
87+
$query->setPositiveQuery($positiveQuery);
88+
$query->setNegativeQuery($negativeQuery);
89+
$query->setNegativeBoost(0.2);
90+
91+
$response = $this->type->search($query);
92+
$results = $response->getResults();
93+
94+
$this->assertEquals($response->getTotalHits(), 4);
95+
96+
$lastResult = $results[3]->getData();
97+
$this->assertEquals($lastResult['name'], $this->sampleData[2]['name']);
98+
}
99+
}

0 commit comments

Comments
 (0)