Skip to content

Commit b610d4f

Browse files
committed
Merge pull request #580 from MrHash/master
Options for TermsFilter lookup
2 parents 408ec4c + 9acfb23 commit b610d4f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

changes.txt

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

3+
2014-04-7
4+
- Support for Terms filter lookup options
5+
36
2014-03-29
47
- Update to elasticsearch 1.1.0
58

lib/Elastica/Filter/Terms.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public function setTerms($key, array $terms)
6767
* @param string|\Elastica\Type $type document type from which to fetch the terms values
6868
* @param string $id id of the document from which to fetch the terms values
6969
* @param string $path the field from which to fetch the values for the filter
70-
* @param string|\Elastica\Index $index the index from which to fetch the terms values. Defaults to the current index.
70+
* @param string|array|\Elastica\Index $options An array of options or the index from which to fetch the terms values. Defaults to the current index.
7171
* @return \Elastica\Filter\Terms Filter object
7272
*/
73-
public function setLookup($key, $type, $id, $path, $index = NULL)
73+
public function setLookup($key, $type, $id, $path, $options = array())
7474
{
7575
$this->_key = $key;
7676
if ($type instanceof \Elastica\Type) {
@@ -81,6 +81,16 @@ public function setLookup($key, $type, $id, $path, $index = NULL)
8181
'id' => $id,
8282
'path' => $path
8383
);
84+
85+
$index = $options;
86+
if(is_array($options)) {
87+
if(isset($options['index'])) {
88+
$index = $options['index'];
89+
unset($options['index']);
90+
}
91+
$this->_terms = array_merge($options, $this->_terms);
92+
}
93+
8494
if (!is_null($index)) {
8595
if ($index instanceof \Elastica\Index) {
8696
$index = $index->getName();

test/lib/Elastica/Test/Filter/TermsTest.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,29 @@ public function testLookup()
2626

2727
//use the terms lookup feature to query for some data
2828
$termsFilter = new Terms();
29-
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members');
29+
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', NULL);
3030
$query = new \Elastica\Query();
3131
$query->setFilter($termsFilter);
3232
$results = $index->search($query);
33-
34-
$this->assertEquals($results->count(), 4);
33+
$this->assertEquals($results->count(), 4, 'Terms lookup with null index');
34+
35+
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index);
36+
$query->setFilter($termsFilter);
37+
$results = $index->search($query);
38+
$this->assertEquals($results->count(), 4, 'Terms lookup with index as object');
39+
40+
//Query with index given as string
41+
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index->getName());
42+
$query->setFilter($termsFilter);
43+
$results = $index->search($query);
44+
$this->assertEquals($results->count(), 4, 'Terms lookup with index as string');
45+
46+
//Query with array of options
47+
$termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', array('index' => $index, 'cache' => false));
48+
$query->setFilter($termsFilter);
49+
$results = $index->search($query);
50+
$this->assertEquals($results->count(), 4, 'Terms lookup with options array');
51+
3552
$index->delete();
3653
}
3754
}

0 commit comments

Comments
 (0)