Skip to content

Commit 5da3bb5

Browse files
committed
License, updated composer rules, wrapping up Response code
1 parent 729e862 commit 5da3bb5

File tree

8 files changed

+132
-130
lines changed

8 files changed

+132
-130
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#composer related
2-
composer.json
32
composer.lock
43
vendor/
54

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (C) 2013 Zachary Tong
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "sherlock/sherlock",
3+
"description": "PHP Client for Elasticsearch",
4+
"keywords": ["search","client", "elasticsearch"],
5+
"homepage": "https://github.com/polyfractal/elasticsearch-sherlock",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Zachary Tong",
11+
"homepage": "http://euphonious-intuition.com/"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3.0",
16+
"guzzle/guzzle": "~3.1"
17+
"analog/analog": "dev-master"
18+
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "3.7.*"
22+
},
23+
"autoload": {
24+
"psr-0": {
25+
"sherlock": "src/",
26+
"sherlock/tests" : "tests/"
27+
}
28+
}
29+
}

src/Sherlock/Sherlock.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace sherlock;
99

10-
use sherlock\Template;
10+
1111
use sherlock\requests;
1212
use sherlock\components;
1313
use sherlock\common\exceptions;
@@ -83,35 +83,6 @@ public function autodetect()
8383
}
8484

8585

86-
public function loadTemplates($path = "", $merge = true)
87-
{
88-
if($path == "") {
89-
$path = $this->settings['templates.path'];
90-
}
91-
92-
$files = $this->directoryScan($path);
93-
$newTemplates = array();
94-
foreach($files as $file => $fullPath){
95-
$newTemplates[$file] = new \sherlock\Template\Template($path.$fullPath);
96-
}
97-
98-
if ($merge == true)
99-
$this->templates = array_merge($this->templates, $newTemplates);
100-
else
101-
$this->templates = $newTemplates;
102-
103-
}
104-
105-
/**
106-
* @param $key
107-
* @return \sherlock\Template\Template
108-
*/
109-
public function getTemplate($key)
110-
{
111-
return $this->templates[$key];
112-
}
113-
114-
11586
/**
11687
* Add a new node to the ES cluster
11788
* @param string $host server host address (either IP or domain)

src/Sherlock/requests/SearchRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function execute()
145145
//parent and children under Strict
146146
$this->_uri = $uri;
147147
$this->_data = $finalQuery;
148-
parent::execute();
148+
return parent::execute();
149149
}
150150
}
151151

src/Sherlock/responses/Response.php

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,100 @@
88
namespace sherlock\responses;
99
use Guzzle\Http\Message;
1010

11-
class Response
11+
class Response implements \IteratorAggregate, \Countable
1212
{
13+
/**
14+
* @var int
15+
*/
16+
public $took;
17+
18+
/**
19+
* @var bool
20+
*/
21+
public $timed_out;
1322

14-
protected $responseData;
23+
/**
24+
* @var int
25+
*/
26+
public $total;
27+
28+
/**
29+
* @var float
30+
*/
31+
public $max_score;
32+
33+
/**
34+
* @var array
35+
*/
36+
public $hits;
37+
38+
/**
39+
* @var array
40+
*/
41+
public $responseData;
42+
43+
44+
private $_key;
1545

1646
/**
1747
* @param \Guzzle\Http\Message\Response $response
48+
* @throws \sherlock\common\exceptions\BadMethodCallException
1849
*/
1950
public function __construct($response)
2051
{
52+
if (!isset($response))
53+
{
54+
\Analog\Analog::log("Response must be set in constructor.",\Analog\Analog::ERROR);
55+
throw new \sherlock\common\exceptions\BadMethodCallException("Response must be set in constructor.");
56+
}
57+
2158
$this->responseData = $response->json();
59+
\Analog\Analog::log("Response->__construct() : ".print_r($this->responseData,true),\Analog\Analog::DEBUG);
60+
61+
$this->took = $this->responseData['took'];
62+
$this->timed_out = ($this->responseData['timed_out'] == '') ? false : true;
63+
64+
if (isset($this->responseData['hits']['total']))
65+
$this->total = $this->responseData['hits']['total'];
66+
67+
if (isset($this->responseData['hits']['max_score']))
68+
$this->max_score = $this->responseData['hits']['max_score'];
69+
70+
if (isset($this->responseData['hits']['hits']))
71+
{
72+
$this->hits = $this->responseData['hits']['hits'];
73+
74+
//get rid of the underscores
75+
foreach($this->hits as $hitKey => $hit)
76+
{
77+
foreach($hit as $key => $value)
78+
{
79+
if (substr($key,0,1)=='_'){
80+
$this->hits[$hitKey][ltrim($key, '_')] = $value;
81+
unset($this->hits[$hitKey][$key]);
82+
}
83+
}
84+
}
85+
86+
}
87+
88+
89+
2290
}
2391

2492

93+
public function getIterator()
94+
{
95+
return new \ArrayIterator($this->hits);
96+
}
97+
98+
public function count()
99+
{
100+
return sizeof($this->hits);
101+
}
102+
103+
104+
105+
106+
25107
}

src/Sherlock/template/Template.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

tests/SherlockTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,22 @@ public function testAddNode()
4343

4444
/**
4545
* @covers sherlock\Sherlock::query
46+
* @todo make this test actually assert things
4647
*/
4748
public function testBuildQuery()
4849
{
49-
$ret = $this->object->addNode('loopback.com', '9200');
50+
$this->object->addNode('loopback.com', '9200');
5051
$req = $this->object->search();
5152
$req->index("test")->type("benchmark");
5253
$req->query($this->object->query()->Term()->field("field1")->term("town"));
53-
$req->execute();
54+
$resp = $req->execute();
55+
56+
echo $resp->took;
57+
foreach($resp as $hit)
58+
{
59+
echo $hit['score'].' - '.$hit['source']['field1']."\r\n";
60+
}
61+
5462

5563

5664

0 commit comments

Comments
 (0)