Skip to content

Commit ad45114

Browse files
author
Raymond Julin
committed
Added support for options in index calls, made http transport indexing send refresh to speed up testcases
1 parent c945fd1 commit ad45114

7 files changed

+60
-63
lines changed

ElasticSearchClient.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ public function request($path, $method, $payload, $verbose=false) {
8686
* @return array
8787
* @param array $document
8888
* @param mixed $id Optional
89+
* @param array $options Allow sending query parameters to control indexing further
90+
* _refresh_ *bool* If set to true, immediately refresh the shard after indexing
8991
*/
90-
public function index($document, $id=false) {
91-
return $this->transport->index($document, $id);
92+
public function index($document, $id=false, array $options = array()) {
93+
return $this->transport->index($document, $id, $options);
9294
}
9395

9496
/**
@@ -110,19 +112,21 @@ public function search($query) {
110112
* @return array
111113
* @param mixed $id If id is supplied, delete that id for this index
112114
* if not wipe the entire index
115+
* @param array $options Parameters to pass to delete action
113116
*/
114-
public function delete($id=false) {
115-
return $this->transport->delete($id);
117+
public function delete($id=false, array $options = array()) {
118+
return $this->transport->delete($id, $options);
116119
}
117120

118121
/**
119122
* Flush this index/type combination
120123
*
121124
* @return array
122125
* @param mixed $query Text or array based query to delete everything that matches
126+
* @param array $options Parameters to pass to delete action
123127
*/
124-
public function deleteByQuery($query) {
125-
return $this->transport->deleteByQuery($query);
128+
public function deleteByQuery($query, array $options = array()) {
129+
return $this->transport->deleteByQuery($query, $options);
126130
}
127131

128132
private function getMicroTime() {

lib/transport/ElasticSearchTransport.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
abstract class ElasticSearchTransport {
44
protected $index, $type;
55

6-
abstract public function index($document, $id=false);
6+
abstract public function index($document, $id=false, array $options = array());
77
abstract public function request($path, $method="GET", $payload=false);
88
abstract public function delete($id=false);
99
abstract public function search($query);
@@ -14,4 +14,22 @@ public function setIndex($index) {
1414
public function setType($type) {
1515
$this->type = $type;
1616
}
17+
18+
/**
19+
* Build a callable url
20+
*
21+
* @return string
22+
* @param array $path
23+
* @param array $options Query parameter options to pass
24+
*/
25+
protected function buildUrl($path=false, array $options = array()) {
26+
$url = "/" . $this->index;
27+
if ($path && count($path) > 0)
28+
$url .= "/" . implode("/", array_filter($path));
29+
if (substr($url, -1) == "/")
30+
$url = substr($url, 0, -1);
31+
if (count($options) > 0)
32+
$url .= "?" . http_build_query($options);
33+
return $url;
34+
}
1735
}

lib/transport/ElasticSearchTransportHTTP.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function __construct($host, $port) {
5959
* @param array $document
6060
* @param mixed $id Optional
6161
*/
62-
public function index($document, $id=false) {
63-
$url = $this->buildUrl(array($this->type, $id));
62+
public function index($document, $id=false, array $options = array()) {
63+
$url = $this->buildUrl(array($this->type, $id), $options);
6464
$method = ($id == false) ? "POST" : "PUT";
6565
try {
6666
$response = $this->call($url, $method, $document);
@@ -116,15 +116,16 @@ public function search($query) {
116116
*
117117
* @return array
118118
* @param mixed $id Optional
119+
* @param array $options Parameters to pass to delete action
119120
*/
120-
public function deleteByQuery($query) {
121+
public function deleteByQuery($query, array $options = array()) {
121122
if (is_array($query)) {
122123
/**
123124
* Array implies using the JSON query DSL
124125
*/
125126
$url = $this->buildUrl(array(
126127
$this->type, "_query"
127-
));
128+
), $options);
128129
try {
129130
$result = $this->call($url, "DELETE", $query);
130131
}
@@ -138,7 +139,7 @@ public function deleteByQuery($query) {
138139
*/
139140
$url = $this->buildUrl(array(
140141
$this->type, "_query?q=" . $query
141-
));
142+
), $options);
142143
try {
143144
$result = $this->call($url, "DELETE");
144145
}
@@ -170,8 +171,10 @@ public function request($path, $method="GET", $payload=false) {
170171
* Flush this index/type combination
171172
*
172173
* @return array
174+
* @param mixed $id Id of document to delete
175+
* @param array $options Parameters to pass to delete action
173176
*/
174-
public function delete($id=false) {
177+
public function delete($id=false, array $options = array()) {
175178
if ($id)
176179
return $this->request(array($this->type, $id), "DELETE");
177180
else
@@ -262,19 +265,4 @@ protected function handleError($url, $method, $payload, $response) {
262265
$err .= $response['error'] . "\n";
263266
//echo $err;
264267
}
265-
266-
/**
267-
* Build a callable url
268-
*
269-
* @return string
270-
* @param array $path
271-
*/
272-
protected function buildUrl($path=false) {
273-
$url = "/" . $this->index;
274-
if ($path && count($path) > 0)
275-
$url .= "/" . implode("/", array_filter($path));
276-
if (substr($url, -1) == "/")
277-
$url = substr($url, 0, -1);
278-
return $url;
279-
}
280268
}

lib/transport/ElasticSearchTransportMemcached.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct($host="127.0.0.1", $port=11311) {
1616
* @param array $document
1717
* @param mixed $id Optional
1818
*/
19-
public function index($document, $id=false) {
19+
public function index($document, $id=false, array $options = array()) {
2020
if ($id === false)
2121
throw new Exception("Memcached transport requires id when indexing");
2222

@@ -63,17 +63,18 @@ public function search($query) {
6363
* Search
6464
*
6565
* @return array
66-
* @param mixed $id Optional
66+
* @param mixed $query String or array to use as criteria for delete
67+
* @param array $options Parameters to pass to delete action
6768
*/
68-
public function deleteByQuery($query) {
69+
public function deleteByQuery($query, array $options = array()) {
6970
if (is_array($query)) {
7071
/**
7172
* Array implies using the JSON query DSL
7273
*/
7374
return;
7475
$url = $this->buildUrl(array(
7576
$this->type, "_query"
76-
));
77+
), $options);
7778
$result = $this->call($url, "DELETE", $query);
7879
}
7980
elseif (is_string($query)) {
@@ -83,7 +84,7 @@ public function deleteByQuery($query) {
8384
return;
8485
$url = $this->buildUrl(array(
8586
$this->type, "_query?q=" . $query
86-
));
87+
), $options);
8788
$result = $this->call($url, "DELETE");
8889
}
8990
return $result['ok'];
@@ -112,8 +113,9 @@ public function request($path, $method="GET", $payload=false) {
112113
* Flush this index/type combination
113114
*
114115
* @return array
116+
* @param array $options Parameters to pass to delete action
115117
*/
116-
public function delete($id=false) {
118+
public function delete($id=false, array $options = array()) {
117119
if ($id)
118120
return $this->request(array($this->type, $id), "DELETE");
119121
else
@@ -159,19 +161,4 @@ private function handleError($url, $method, $payload, $response) {
159161
$err .= $response['error'] . "\n";
160162
//echo $err;
161163
}
162-
163-
/**
164-
* Build a callable url
165-
*
166-
* @return string
167-
* @param array $path
168-
*/
169-
private function buildUrl($path=false) {
170-
$url = "/" . $this->index;
171-
if ($path && count($path) > 0)
172-
$url .= "/" . implode("/", array_filter($path));
173-
if (substr($url, -1) == "/")
174-
$url = substr($url, 0, -1);
175-
return $url;
176-
}
177164
}

tests/ElasticSearchParent.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ protected function generateDocument($words, $len=4) {
1919
protected function addDocuments($indexes=array("test-index"), $num=3, $rand=false) {
2020
$words = array("cool", "dog", "lorem", "ipsum", "dolor", "sit", "amet");
2121
// Generate documents
22+
$options = array(
23+
'refresh' => true
24+
);
2225
foreach ($indexes as $ind) {
2326
$this->search->setIndex($ind);
2427
$tmpNum = $num;
@@ -30,7 +33,7 @@ protected function addDocuments($indexes=array("test-index"), $num=3, $rand=fals
3033
$doc = $this->generateDocument($words, 5);
3134
else
3235
$doc = array('title' => 'One cool document', 'rank' => rand(1,10));
33-
$this->search->index($doc, $tmpNum + 1);
36+
$this->search->index($doc, $tmpNum + 1, $options);
3437
}
3538
}
3639
}
@@ -53,7 +56,6 @@ public function testIndexingDocument() {
5356
*/
5457
public function testStringSearch() {
5558
$this->addDocuments();
56-
sleep(2); // Indexing is only near real time
5759
$hits = $this->search->search("title:cool");
5860
$this->assertEquals(3, $hits['hits']['total']);
5961
}
@@ -64,7 +66,6 @@ public function testStringSearch() {
6466
public function testSearchMultipleIndexes() {
6567
$indexes = array("test-index", "test2");
6668
$this->addDocuments($indexes);
67-
sleep(1); // To make sure the documents will be ready
6869

6970
// Use both indexes when searching
7071
$this->search->setIndex($indexes);
@@ -82,7 +83,6 @@ public function testSearchMultipleIndexes() {
8283
*/
8384
public function testSearch() {
8485
$this->addDocuments();
85-
sleep(2); // To make sure the documents will be ready
8686

8787
$hits = $this->search->search(array(
8888
'query' => array(
@@ -97,7 +97,6 @@ public function testSearch() {
9797
*/
9898
public function testSort() {
9999
$this->addDocuments();
100-
sleep(2); // To make sure the documents will be ready
101100

102101
$arr = array(
103102
'sort' => array(

tests/HTTPTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@ public function testIndexingDocumentWithoutId() {
3030
* Test delete by query
3131
*/
3232
public function testDeleteByQuery() {
33+
$refresh = true;
3334
$doc = array('title' => 'not cool yo');
3435
$this->search->setIndex("test-index");
35-
$this->search->index($doc, 1);
36-
37-
sleep(1); // To make sure the documents will be ready
36+
$this->search->index($doc, 1, compact('refresh'));
3837

3938
$del = $this->search->deleteByQuery(array(
4039
'term' => array('title' => 'cool')
41-
));
40+
), compact('refresh'));
41+
sleep(1);
4242

4343
$this->assertTrue($del);
4444

45-
sleep(1); // To make sure the documents will be ready
46-
4745
// Use both indexes when searching
4846
$hits = $this->search->search(array(
4947
'query' => array(
@@ -63,8 +61,7 @@ public function testSlightlyComplexSearch() {
6361
'body' => 'Lorem ipsum dolor sit amet',
6462
'tag' => array('cool', "stuff", "2k")
6563
);
66-
$resp = $this->search->index($doc, 1);
67-
sleep(1); // Indexing is only near real time
64+
$resp = $this->search->index($doc, 1, array('refresh' => true));
6865

6966
$hits = $this->search->search(array(
7067
'query' => array(
@@ -83,7 +80,6 @@ public function testSlightlyComplexSearch() {
8380
$this->assertEquals(3, $hits['hits']['total']);
8481
}
8582

86-
8783
/**
8884
* @expectedException ElasticSearchTransportHTTPException
8985
*/

tests/MemcachedTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public function tearDown() {
1313
$this->search->delete();
1414
$this->search = null;
1515
}
16+
17+
protected function addDocuments($indexes=array("test-index"), $num=3, $rand=false) {
18+
parent::addDocuments($indexes, $num, $rand);
19+
sleep(1);
20+
}
1621
}

0 commit comments

Comments
 (0)