.gitignore000064400000000064144761607160006550 0ustar00/vendor/ /tests/ composer.lock /examples/config.jsonREADME.md000064400000000016144761607160006034 0ustar00# boruelastic composer.json000064400000000746144761607160007311 0ustar00{ "name": "boru/boruelastic", "type": "library", "autoload": { "psr-4": { "boru\\boruelastic\\": "src/" } }, "authors": [ { "name": "Daniel Hayes", "email": "dhayes@boruapps.com" } ], "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ], "require": { "boru/dhttp": "*", "boru/dhutils": "*" } } examples/Bulk.php000064400000001712144761607160010005 0ustar00"John", "last_name"=>"Doe", "age"=>30, "about"=>"I love to go rock climbing", "interests"=>[ "sports", "music" ] ] )->index("boru_test")->id("john_doe_test_1"); $testJane = Record::fromArray( [ "first_name"=>"Jane", "last_name"=>"Doe", "age"=>25, "about"=>"I love to go rock climbing", "interests"=>[ "sports", "music" ] ] )->index("boru_test")->id("jane_doe_test_1"); $bulk = new Bulk(); $bulk->index($testJohn); $bulk->index($testJane); $result = $bulk->run(); echo "Sent ".$result->totalSent()." requests\n"; echo "Received ".$result->total()." responses\n"; foreach($result as $bulkResult) { echo $bulkResult."\n"; } examples/Client.php000064400000001254144761607160010327 0ustar00get("endpoint"),["auth"=>$config->get("auth")]); //Dynamic configuration, allows for more than one elastic connection per script //If using Dynamic, you must construct the Search, Record, etc objects with the client. //This is handled automatically with static configuration. //Note: The first created Client will be set as the default for Elastic::search(), Elastic::record(), if //Static init has not been called, before hand. //$client = new Client($config->get("endpoint"),["auth"=>$config->get("auth")]);examples/Record.php000064400000001273144761607160010330 0ustar00"John","last_name"=>"Doe","age"=>30,"about"=>"I love to go rock climbing","interests"=>["sports","music"]]); $record->index("boru_test"); $record->id("test_id_2"); $record->save(); $record = Record::fromId("test_id_2","boru_test"); echo "The created record's first name is: ".$record->get("first_name")."\n"; echo "Created record: ".$record."\n"; $record->set("first_name","Jane"); $record->save(); $record = Record::fromId("test_id_2","boru_test"); echo "The updated record's first name is: ".$record->get("first_name")."\n"; echo "Updated record: ".$record."\n";examples/Search.php000064400000000667144761607160010325 0ustar00index("boru_test"); $search->must(Query::match("first_name","Jane")); $search->source(true)->size(3); $result = $search->run(); print_r($result); foreach($result->hits() as $hit) { echo "Hit with score of ".$hit->score()." :: ID: ".$hit->id().", FirstName: ".$hit->get("first_name")."\n"; }examples/init.php000064400000002065144761607160010055 0ustar00__DIR__."/config.json",["prettyJson"=>true,"sort"=>true]]); $config->load(); if($config->get("endpoint",false) === false) { $config->set("endpoint","http://localhost:9200"); $config->save(); } if($config->get("auth",false) === false) { $config->set("auth",["username"=>"elastic","password"=>"changeme"]); $config->save(); } if($configCreate) { echo "Config file created. Please edit it with your own values.\nCurrent defaults:\n"; echo "Endpoint: ".$config->get("endpoint")."\n"; echo "Auth: ".json_encode($config->get("auth"))."\n"; echo "\n\n"; echo "Raw file:\n"; echo file_get_contents(__DIR__."/config.json"); echo "\n\n"; }instructions-composer.txt000064400000000303144761607160011706 0ustar00{ "require": { "boru/borudiff": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/Aggs.php000064400000002172144761607160006743 0ustar00aggs as $name=>$part) { if(!$part->isEmpty()) { $data[$name] = $part; } } return $data; } public function add($name,$agg) { $this->aggs[$name] = $agg; } public static function avg($field) { return new AvgAgg($field); } public static function terms($field,$size=1) { return new TermsAgg($field,$size); } public static function filter($filter,$aggs=null) { return new FilterAgg($filter,$aggs); } public static function topHits($size=1,$source=null,$sort=null) { return new TopHitsAgg($size,$source,$sort); } public static function instance() { return new static(); } }src/Bulk.php000064400000004771144761607160006766 0ustar00client($client); } } public function client($client=null) { if(is_null($client)) return $this->client; $this->client = $client; } public function run() { $this->assertClient(); return new BulkResult($this->client->_bulk($this->bulkItems,$this->onlyReturnErrors,false),$this->itemCount); } public function onlyReturnErrors($onlyReturnErrors=true) { if(is_null($onlyReturnErrors)) return $this->onlyReturnErrors; $this->onlyReturnErrors = $onlyReturnErrors; } public function index($record) { $this->addHeaderItem("index",$record); $this->addBodyItem($record); } public function create($record) { $this->addHeaderItem("create",$record); $this->addBodyItem($record); } public function update($record) { $this->addHeaderItem("update",$record); $this->addBodyItem($record,true); } public function delete($record) { $this->addHeaderItem("delete",$record); } public function deleteById($index,$id) { $this->add(BulkEntry::fromArray(["delete"=>["_index"=>$index,"_id"=>$id]])); } public function add($bulkEntryItem) { $this->bulkItems[] = $bulkEntryItem; } private function addHeaderItem($action="index",$record) { $header = []; $header[$action] = ["_index"=>$record->index(),"_id"=>$record->id()]; $this->add(BulkEntry::fromArray($header)); $this->itemCount++; return $header; } private function addBodyItem($record,$isDoc=false) { $body = []; if($isDoc) { $body["doc"] = $record->get(); } else { $body = $record->get(); } $this->add(BulkEntry::fromArray($body)); return $body; } private function assertClient() { if(is_null($this->client)) { $this->client = Elastic::client(); } if(is_null($this->client)) { throw new \Exception("Bulk::run() called without a Client set"); } } }src/Client.php000064400000002767144761607160007312 0ustar00headRequest($index,[],false); } catch (\Exception $e) { if($e->getCode() == 404) return false; throw $e; } return true; } public function templateExists($templateName) { $path = "_template/".$templateName; try { $response = $this->headRequest($path,[],false); } catch (\Exception $e) { if($e->getCode() == 404) return false; throw $e; } return true; } public function deleteById($index,$id,$async=null) { $path = $index."/_doc/".$id; return $this->deleteRequest($path,[],$async); } public function search($search, $index=null) { if(!($search instanceof Search)) throw new \Exception("search() expects a Search object"); $search->client($this); return $search->run($index); } public function index($record) { if(!($record instanceof Record)) throw new \Exception("index() expects a Record object"); $record->client($this); return $record->save(); } public function delete($record) { if(!($record instanceof Record)) throw new \Exception("delete() expects a Record object"); $record->client($this); return $record->delete(); } }src/Elastic.php000064400000005545144761607160007455 0ustar00endpoint(); static::$client->endpoint($endpoint); } public static function auth($auth=null) { static::assertInit(); if(is_null($auth)) return static::$client->auth(); static::$client->auth($auth); } public static function async($async=null) { static::assertInit(); if(is_null($async)) return static::$client->async(); static::$client->async($async); } public static function client() { static::assertInit(); return static::$client; } public static function setClient($connector,$overwrite=false) { if(is_null(static::$client) || $overwrite) { static::$client = $connector; } } public static function indexExists($index) { static::assertInit(); return static::$client->indexExists($index); } public static function templateExists($templateName) { static::assertInit(); return static::$client->templateExists($templateName); } /** * @param Search $search * @param string|null $index * @return \React\Promise\PromiseInterface|\boru\boruelastic\result\Results * @throws Exception */ public static function search($search,$index=null) { static::assertInit(); return static::$client->search($search,$index); } /** * @param Record $record * @return \React\Promise\PromiseInterface|\boru\boruelastic\result\Results * @throws Exception */ public static function index($record) { static::assertInit(); return static::$client->index($record); } /** * @param Record $record * @return \React\Promise\PromiseInterface|\boru\boruelastic\result\Results * @throws Exception */ public static function delete($record) { static::assertInit(); return static::$client->delete($record); } /** * @param string $index * @param string $id * @param bool|null $async * @return \React\Promise\PromiseInterface|\boru\boruelastic\result\Results * @throws Exception */ public static function deleteById($index,$id,$async=null) { static::assertInit(); return static::$client->deleteById($index,$id,$async); } private static function assertInit() { if(is_null(static::$client)) throw new \Exception("Elastic::init() must be called before using Elastic"); } }src/Filter.php000064400000001621144761607160007305 0ustar00filters as $part) { if(!$part->isEmpty()) { $data[] = $part; } } return ["filter"=>$data]; } public function add($name,$filter) { $this->filters[$name] = $filter; } public static function term($field,$value) { return new TermFilter($field,$value); } public static function range($field,$comparator,$value) { return new RangeFilter($field,$comparator,$value); } public static function instance() { return new static(); } }src/Query.php000064400000003554144761607160007174 0ustar00toArray(),JSON_PRETTY_PRINT); } public function jsonSerialize() { return $this->toArray(); } public static function bool($must=[],$mustNot=[],$should=[],$minimumShould=1) { $bool = new BoolQuery("bool"); if(!empty($must)) { foreach($must as $q) { $bool->must($q); } } if(!empty($mustNot)) { foreach($mustNot as $q) { $bool->mustNot($q); } } if(!empty($should)) { foreach($should as $q) { $bool->should($q); } } $bool->minimumShould($minimumShould); return $bool; } public static function exists($field) { return new ExistsQuery($field); } public static function match($field,$value,$boost=null) { return new MatchQuery($field,$value,$boost); } public static function range($field,$comparator,$value) { return new RangeQuery($field,$comparator,$value); } public static function term($field,$value,$boost=null) { return new TermQuery($field,$value,$boost); } public static function terms($field,$values,$boost=null) { return new TermsQuery($field,$values,$boost); } public static function instance() { return new static(); } }src/Record.php000064400000010604144761607160007277 0ustar00client($client); } if(!is_null($data)) { $this->setRaw($data); } } public function client($client=null) { if(is_null($client)) return $this->client; $this->client = $client; } public function id($id=null) { if(is_null($id)) return $this->getRaw("_id",false); $this->setRaw("_id",$id); return $this; } public function index($index=null) { if(is_null($index)) return $this->getRaw("_index",false); $this->setRaw("_index",$index); return $this; } public function type($type=null) { if(is_null($type)) return $this->getRaw("_type",false); $this->setRaw("_type",$type); return $this; } public function version($version=null) { if(is_null($version)) return $this->getRaw("_version",false); $this->setRaw("_version",$version); return $this; } public function seqNo($seqNo=null) { if(is_null($seqNo)) return $this->getRaw("_seq_no",false); $this->setRaw("_seq_no",$seqNo); return $this; } public function primaryTerm($primaryTerm=null) { if(is_null($primaryTerm)) return $this->getRaw("_primary_term",false); $this->setRaw("_primary_term",$primaryTerm); return $this; } public function found($found=null) { if(is_null($found)) return $this->getRaw("found",false); $this->setRaw("found",$found); return $this; } public function source($source=null) { if(is_null($source)) return $this->getRaw("_source",false); $this->setRaw("_source",$source); return $this; } public function getRaw($key,$default=null) { return parent::get($key,$default); } public function get($key=null,$default=NULL) { if(is_null($key)) { return parent::get("_source"); } return parent::get("_source.".$key,$default); } public function setRaw($key,$value=null) { return parent::set($key,$value); } public function set($key,$value=null) { if(is_array($key)) { return parent::set($key); } return parent::set("_source.".$key,$value); } public function save() { $this->assertClient(); if($this->id()) { $path = $this->index()."/_doc/".$this->id(); $result = new ResultItem($this->client->putRequest($path,$this->source())); } else { $path = $this->index()."/_doc"; $result = new ResultItem($this->client->postRequest($path,$this->source())); } if($result->id()) { $this->id($result->id()); } if($result->version()) { $this->version($result->version()); } if($result->index()) { $this->index($result->index()); } return $result; } public function delete() { $this->assertClient(); if($this->id()) { $path = $this->index()."/_doc/".$this->id(); $this->client->delete($path); } } public function load($id,$index) { $this->assertClient(); $path = $index."/_doc/".$id; $response = $this->client->getRequest($path); $this->setRaw($response); return $this; } public static function fromId($id,$index,$client=null) { $record = new Record(null,$client); $record->load($id,$index); return $record; } public static function fromArray($data,$id=null,$client=null) { $record = new Record(null,$client); $record->source($data); if(!is_null($id)) { $record->id($id); } return $record; } private function assertClient() { if(is_null($this->client)) { $this->client = Elastic::client(); } if(is_null($this->client)) { throw new \Exception("Record::save() and Record::delete() cannot be called without a Client."); } } }src/Search.php000064400000010604144761607160007266 0ustar00endpoint($endpoint); $this->auth($auth); }*/ public function __construct($client=null) { if(!is_null($client)) { $this->client($client); } } public function client($client=null) { if(is_null($client)) return $this->client; $this->client = $client; } public function index($index=null) { if(is_null($index)) return $this->index; $this->index = $index; } public function source($source=true) { $this->source=$source ? true : false; return $this; } public function size($size=1000) { $this->size= (int) $size; return $this; } public function query($query) { $this->query = $query; return $this; } public function aggs($aggs) { if($aggs instanceof Aggs) { $this->aggs = $aggs; } else { throw new \Exception("aggs must be an instance of Aggs"); } $this->aggs = $aggs; return $this; } public function fields($fields) { $this->fields = $fields; return $this; } public function collapse($collapse) { $this->collapse = $collapse; return $this; } //convinience functions public function addAgg($name,$agg) { if(is_null($this->aggs)) { $this->aggs = new Aggs(); } $this->aggs->add($name,$agg); } public function must($must) { if(is_null($this->query)) { $this->query = new Query(); } $this->query->must($must); return $this; } public function mustNot($mustNot) { if(is_null($this->query)) { $this->query = new Query(); } $this->query->mustNot($mustNot); return $this; } public function should($should) { if(is_null($this->query)) { $this->query = new Query(); } $this->query->should($should); return $this; } public function minimumShould($minimumShouldMatch) { if(is_null($this->query)) { $this->query = new Query(); } $this->query->minimumShould($minimumShouldMatch); return $this; } public function toArray() { $query = []; if(!is_null($this->query)) { $query['query'] = $this->query; } if(!is_null($this->aggs)) { $query['aggs'] = $this->aggs; } if(!is_null($this->fields)) { $query['fields'] = $this->fields; } if(!is_null($this->collapse)) { $query['collapse'] = $this->collapse; } if(!is_null($this->source)) { $query['_source'] = $this->source; } if(!is_null($this->size)) { $query['size'] = $this->size; } return $query; } public function run($index="") { $this->assertClient(); if(!empty($index)) { $this->index($index); } if($this->client->async()) { $deferred = new \React\Promise\Deferred(); $this->client->getRequest($this->index."/_search",$this->toArray())->then(function($results) use ($deferred) { $deferred->resolve(new Results($results)); },function($error) use ($deferred) { $deferred->reject($error); }); return $deferred->promise(); } return new Results($this->client->getRequest($this->index."/_search",$this->toArray())); } private function assertClient() { if(is_null($this->client)) { $this->client = Elastic::client(); } if(is_null($this->client)) { throw new \Exception("Search::run() called without a Client set"); } } }src/agg/AvgAgg.php000064400000000663144761607160007757 0ustar00 [ "field" => $field ] ]; $this->set($template); } public function avg($field) { $this->set("avg.field",$field); return $this; } public function isEmpty() { return false; } }src/agg/BaseAgg.php000064400000001760144761607160010113 0ustar00get()); } public function getSubAggs() { return $this->aggs; } public function add($name,$agg) { $this->aggs[$name] = $agg; return $this; } public function agg($agg) { if(is_array($agg)) { return $this->aggs($agg); } $this->aggs[] = $agg; return $this; } public function aggs($aggs) { if(!is_array($aggs)) { return $this->agg($aggs); } $this->aggs = array_merge($this->aggs,$aggs); return $this; } public function toArray() { $template = $this->get(); $subAggs = $this->getSubAggs(); if(!empty($subAggs)) { $template["aggs"] = $subAggs; } return $template; } }src/agg/FilterAgg.php000064400000001451144761607160010463 0ustar00filter($filter); } if(!is_null($aggs)) { $this->aggs($aggs); } } public function filter($filter) { $this->filter = $filter; return $this; } public function toArray() { $template = [ "filter" => $this->filter, ]; $subAggs = $this->getSubAggs(); if(!empty($subAggs)) { $template["aggs"] = $subAggs; } return $template; } public function isEmpty() { return empty($this->filter) && empty($this->getSubAggs()); } }src/agg/TermsAgg.php000064400000001316144761607160010330 0ustar00 [ "field" => $field, "size" => $size ] ]; $this->set($template); } public function terms($field,$size=null) { $this->set("terms.field",$field); if(!is_null($size)) { $this->set("terms.size",$size); } return $this; } public function size($size=1) { $this->set("terms.size",$size); return $this; } public function isEmpty() { return false; } }src/agg/TopHitsAgg.php000064400000004412144761607160010630 0ustar00 [ "size" => null, "sort" => [], "_source" => [] ] ]; $this->set($template); if(!is_null($size)) { $this->size($size); } if(!is_null($source)) { $this->source($source); } if(!is_null($sort)) { $this->sort($sort); } } public function topHits($size=1,$source=null,$sort=null) { $this->size($size); if(!is_null($source)) { $this->source($source); } if(!is_null($sort)) { $this->sort($sort); } } public function size($size) { $this->set("top_hits.size",$size); return $this; } public function source($fields) { $current = $this->get("top_hits._source",[]); if(!is_array($current)) { $current = [$current]; } if(!is_array($fields)) { $fields = [$fields]; } $this->set("top_hits._source",array_merge($current,$fields)); return $this; } public function sort($sort) { $current = $this->get("top_hits.sort",[]); if(!is_array($current)) { $current = [$current]; } if(!is_array($sort)) { $sort = [$sort]; } $this->set("top_hits.sort",array_merge($current,$sort)); return $this; } public function toArray() { $data = ["top_hits"=>[]]; $size = $this->get("top_hits.size"); if(is_null($size) || $size<1) { $this->set("top_hits.size",1); } $data["top_hits"]["size"] = $this->get("top_hits.size"); $sort = $this->get("top_hits.sort",[]); if(!empty($sort)) { $data["top_hits"]["sort"] = $sort; } $source = $this->get("top_hits._source",[]); if(!empty($source)) { $data["top_hits"]["_source"] = $source; } return $data; } public function isEmpty() { return false; } }src/bulk/BulkEntry.php000064400000001100144761607160010724 0ustar00set($data); } public static function fromArray($array) { $bulkEntry = new BulkEntry($array); return $bulkEntry; } public function toJson($pretty=false) { if($pretty) { return json_encode($this->toArray(),JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); } return json_encode($this->toArray(),JSON_UNESCAPED_SLASHES); } }src/conn/Connector.php000064400000013775144761607160010764 0ustar00endpoint($endpoint); if(isset($options["auth"])) { $this->auth($options["auth"]); unset($options["auth"]); } if(isset($options["async"])) { $this->async($options["async"]); unset($options["async"]); } Elastic::setClient($this); } public function endpoint($endpoint=null) { if(is_null($endpoint)) return $this->endpoint; $this->endpoint = $endpoint; } public function auth($auth=null) { if(is_null($auth)) return $this->auth; if(is_array($auth)) { $u = $p = ""; if(isset($auth["user"])) { $u = $auth["user"]; } elseif(isset($auth["username"])) { $u = $auth["username"]; } if(isset($auth["pass"])) { $p = $auth["pass"]; } elseif(isset($auth["password"])) { $p = $auth["password"]; } if($u && $p) { $auth = $u.":".$p; } else { $auth = implode(":",$auth); } } $this->auth = $auth; } public function async($async=null) { if(is_null($async)) return $this->async; $this->async = $async; } public function getRequest($path,$data=[],$async=null) { return $this->request("GET",$path,$data,$async); } public function postRequest($path,$data=[],$async=null) { return $this->request("POST",$path,$data,$async); } public function putRequest($path,$data=[],$async=null) { return $this->request("PUT",$path,$data,$async); } public function deleteRequest($path,$data=[],$async=null) { return $this->request("DELETE",$path,$data,$async); } public function headRequest($path,$data=[],$async=null) { return $this->request("HEAD",$path,$data,$async); } public function request($method="GET",$path,$data=[],$async=null) { if(is_null($async)) { $async = $this->async(); } $url = $this->pathToUrl($path); $options = $this->buildRequestOptions($data); return $this->rawRequest($method,$url,$options,$async); } public function rawRequest($method="GET",$url,$options=[],$async=null) { if(strtoupper($method) == "GET") { $response = Client::get($url,$options,$async); } elseif(strtoupper($method) == "POST") { $response = Client::post($url,$options,$async); } elseif(strtoupper($method) == "PUT") { $response = Client::put($url,$options,$async); } elseif(strtoupper($method) == "DELETE") { $response = Client::delete($url,$options,$async); } elseif(strtoupper($method) == "HEAD") { $response = Client::head($url,$options,$async); } else { throw new \Exception("Method not supported"); } $this->throwIfError($response); if($this->isPromise($response)) { return $response; } return $response->asArray(); } //helper functions public function _index($index,$data=[],$async=null) { return $this->putRequest($index,$data,$async); } public function _template($templateName,$data=[],$async=null) { $path = "_template/".$templateName; return $this->putRequest($path,$data,$async); } public function _update($index,$id,$data=[],$async=null) { $path = $index."/_doc/".$id; return $this->postRequest($path,$data,$async); } public function _insert($index,$id,$data=[],$async=null) { $path = $index."/_doc/".$id; return $this->putRequest($path,$data,$async); } public function _bulk($data=[],$onlyErrorReturn=false,$async=null) { $path = "/_bulk"; if($onlyErrorReturn) { $path .= "?filter_path=items.*.error"; } $bulkData = ""; foreach($data as $item) { $bulkData .= json_encode($item)."\n"; } if(is_null($async)) { $async = $this->async(); } $url = $this->pathToUrl("_bulk"); $options = $this->buildRequestOptions(); $options["body"] = $bulkData; return $this->rawRequest("POST",$url,$options,$async); } private function buildRequestOptions($data=[]) { $options = []; if(!empty($data)) { if(is_array($data)) { $options["json"] = json_encode($data); } else { $options["json"] = $data; } } $options["headers"] = ["Content-Type"=>"application/json"]; if(!is_null($this->auth)) { $t = explode(":",$this->auth); if(count($t) == 2) { $options["auth"] = $t; //basic auth } else { $options["auth"] = $this->auth; //token } } return $options; } private function isPromise($data) { return $data instanceof PromiseInterface; } private function throwIfError($data,$throwIfFalse=true) { if($data instanceof \Exception) throw $data; if($data === false && $throwIfFalse) { throw new \Exception("Request failed"); return; } return $data; } private function pathToUrl($path) { if(substr($path,0,4) != "http") { $url = $this->endpoint(); if(substr($url,-1) != "/") $url .= "/"; if(substr($path,0,1) == "/") $path = substr($path,1); $url .= $path; } else { $url = $path; } return $url; } }src/filter/BaseFilter.php000064400000000307144761607160011365 0ustar00get()); } }src/filter/RangeFilter.php000064400000000200144761607160011537 0ustar00get()); } }src/query/BoolQuery.php000064400000003527144761607160011155 0ustar00 [ "must" => [], "should" => [], "must_not" => [], "minimum_should_match" => 1 ] ]; $this->set($template); } public function must($query) { if($query !== false && !is_null($query)) { $must = $this->get("bool.must"); $must[] = $query; $this->set("bool.must",$must); } return $this; } public function should($query) { if($query !== false && !is_null($query)) { $should = $this->get("bool.should"); $should[] = $query; $this->set("bool.should",$should); } return $this; } public function mustNot($query) { if($query !== false && !is_null($query)) { $must_not = $this->get("bool.must_not"); $must_not[] = $query; $this->set("bool.must_not",$must_not); } return $this; } public function minimumShould($num=1) { $this->set("bool.minimum_should_match",$num); return $this; } public function toArray() { $data = $this->get("bool"); if(empty($data["must"])) { unset($data["must"]); } if(empty($data["should"])) { unset($data["should"]); unset($data["minimum_should_match"]); } if(empty($data["must_not"])) { unset($data["must_not"]); } return ["bool"=>$data]; } public function isEmpty() { return empty($this->get("bool.must")) && empty($this->get("bool.should")) && empty($this->get("bool.must_not")); } }src/query/ExistsQuery.php000064400000000754144761607160011540 0ustar00exists($field); } public function exists($field) { $template = [ "exists" => [ "field"=>$field ] ]; $this->set($template); return $this; } public function boost($value) { $this->set("match.boost",$value); return $this; } }src/query/MatchQuery.php000064400000001134144761607160011306 0ustar00match($field,$values); if(!is_null($boost)) { $this->boost($boost); } } public function match($field,$values) { $template = [ "match" => [ $field => $values ] ]; $this->set($template); return $this; } public function boost($value) { $this->set("match.boost",$value); return $this; } }src/query/RangeQuery.php000064400000003126144761607160011311 0ustar00setField($field); if(!is_null($comparator) && !is_null($value)) { $this->range($field,$comparator,$value); } } public function range($field,$comparator,$value) { $template = [ "range" => [ $field => [ $comparator => $value ] ] ]; $this->set($template); return $this; } public function lte($value) { if(is_null($this->field)) { throw new \Exception("Field must be set before calling lte()"); } return $this->range($this->field,"lte",$value); } public function lt($value) { if(is_null($this->field)) { throw new \Exception("Field must be set before calling lt()"); } return $this->range($this->field,"lt",$value); } public function gte($value) { if(is_null($this->field)) { throw new \Exception("Field must be set before calling gte()"); } return $this->range($this->field,"gte",$value); } public function gt($value) { if(is_null($this->field)) { throw new \Exception("Field must be set before calling gt()"); } return $this->range($this->field,"gt",$value); } public function setField($field) { $this->field = $field; return $this; } }src/query/TermQuery.php000064400000001127144761607160011163 0ustar00term($field,$values); if(!is_null($boost)) { $this->boost($boost); } } public function term($field,$values) { $template = [ "term" => [ $field => $values ] ]; $this->set($template); return $this; } public function boost($value) { $this->set("term.boost",$value); return $this; } }src/query/TermsQuery.php000064400000001134144761607160011344 0ustar00terms($field,$values); if(!is_null($boost)) { $this->boost($boost); } } public function terms($field,$values) { $template = [ "terms" => [ $field => $values ] ]; $this->set($template); return $this; } public function boost($value) { $this->set("terms.boost",$value); return $this; } }src/result/Aggregation.php000064400000003375144761607160011635 0ustar00name = $name; $this->setIteratorArray($data["buckets"]); unset($data["buckets"]); $this->set($data); } public function name() { return $this->name; } public function docCountErrorUpperBound() { return $this->get("doc_count_error_upper_bound"); } public function sumOtherDocCount() { return $this->get("sum_other_doc_count"); } /** * Retrieve all buckets * @return Bucket[] */ public function buckets() { if(is_null($this->buckets)) { $buckets = $this->getAll(); array_walk($buckets,function(&$bucket) { $bucket = new Bucket($bucket); $this->buckets[$bucket->key()] = $bucket; }); } return $this->buckets; } /** * Retrieve a bucket by key or index# * @param mixed $keyOrInt * @return Bucket|false */ public function bucket($keyOrInt) { if(is_numeric($keyOrInt)) { return $this->getAt("buckets",$keyOrInt); } else { $this->buckets(); if(isset($this->buckets[$keyOrInt])) { return $this->buckets[$keyOrInt]; } return false; } } public function transformIteratorValue($value) { return new Bucket($value); } public function toArray() { $data = parent::toArray(); $data["buckets"] = $this->getAll(); return $data; } }src/result/BaseResult.php000064400000002344144761607160011452 0ustar00set($data); } public function toArray() { return $this->get(); } public function get($key=null,$default=NULL) { $value = parent::get($key,$default); if(is_array($value)) { if(isset($value["hits"]) && isset($value["hits"]["hits"])) { return new Hits($value); } if(isset($value["buckets"]) && isset($value["buckets"][0])) { $s = explode(".",$key); $name = end($s); return new Aggregation($name,$value); } if(count($value) == 1 && isset($value["value"])) { return $value["value"]; } } return $value; } public static function shorten($array) { static::_shorten($array); return $array; } private static function _shorten(&$array) { if(count($array) == 1 && is_array(array_values($array)[0])) { $array = array_values($array)[0]; static::_shorten($array); } } }src/result/Bucket.php000064400000002246144761607160010617 0ustar00key = $data["key"]; $this->docCount = $data["doc_count"]; unset($data["key"]); unset($data["doc_count"]); if(count($data) == 1 && isset(array_values($data)[0]["value"])) { $data = ["value"=>array_values($data)[0]["value"]]; } else { foreach($data as $name=>$value) { if(is_array($value) && isset($value["value"])) { $data[$name] = $value["value"]; } } } $this->set($data); } public function key() { return $this->key; } public function docCount() { return $this->docCount; } public function toArray() { $data["key"] = $this->key(); $data["doc_count"] = $this->docCount(); $parent = $this->get(); if(is_array($parent)) { $data = array_merge($data,$parent); } else { $data["value"] = $parent; } return $data; } }src/result/BulkResult.php000064400000003622144761607160011475 0ustar00loadResults($resultData); } if(!is_null($numRequests)) { $this->totalSent = $numRequests; } } public function loadResults($resultData,$numRequests=null) { $this->took = $resultData["took"]; $this->errors = $resultData["errors"] ? true : false; if(isset($resultData["items"])) { $this->setIteratorArray($resultData["items"]); unset($resultData["items"]); } $this->set($resultData); if(!is_null($numRequests)) { $this->totalSent = $numRequests; } } public function took() { return $this->took; } public function totalSent() { return $this->totalSent; } public function total() { return count($this->getAll()); } public function errors() { return $this->errors; } /** * @return ResultItem[] */ public function items() { return $this->getAll(); } /** * @param int $i * @return ResultItem */ public function item($i) { return $this->getAt($i); } /** * @return ResultItem */ public function nextItem() { return $this->next(); } public function toArray() { $data = parent::toArray(); $data["items"] = $this->getAll(); return $data; } public function transformIteratorValue($value) { if($value instanceof ResultItem) { return $value; } return new ResultItem($value); } }src/result/Hit.php000064400000004150144761607160010122 0ustar00fullArray = $data; $this->index = $data["_index"]; $this->id = $data["_id"]; $this->score = $data["_score"]; unset($data["_index"]); unset($data["_id"]); unset($data["_score"]); if(isset($data["_source"])) { $this->source = $data["_source"]; unset($data["_source"]); } if(isset($data["fields"])) { $this->fields = $data["fields"]; unset($data["fields"]); } if(isset($data["inner_hits"])) { $this->parseInnerHits($data["inner_hits"]); unset($data["inner_hits"]); } $useData = $data; if(!is_null($this->fields)) { $useData = $this->fields; } elseif(!is_null($this->source)) { $useData = $this->source; } $this->set(BaseResult::shorten($useData)); } public function index() { return $this->index; } public function id() { return $this->id; } public function score() { return $this->score; } public function innerHits() { return $this->innerHits; } public function source() { return $this->source; } public function fields() { return $this->fields; } public function toArray($raw=false) { if($raw) { return $this->fullArray; } $data["_index"] = $this->index(); $data["_id"] = $this->id(); $data["_score"] = $this->score(); $data = array_merge($data,parent::toArray()); return $data; } private function parseInnerHits($innerHits) { foreach($innerHits as $name=>$hitsJson) { $this->innerHits[$name] = new Hits($hitsJson); } } }src/result/Hits.php000064400000002205144761607160010304 0ustar00setIteratorArray($data["hits"]); unset($data["hits"]); $this->set($data); $this->total = $this->get("total"); $this->maxScore = $this->get("max_score",null); } public function total() { return $this->total; } public function maxScore() { return $this->maxScore; } public function hits() { return $this->getAll(); } public function hit($i) { return $this->getAt($i); } /** * @return Hit */ public function nextHit() { return $this->next(); } public function toArray() { $data = parent::toArray(); $data["hits"] = $this->getAll(); return $data; } public function transformIteratorValue($value) { if($value instanceof Hit) { return $value; } return new Hit($value); } }src/result/ResultItem.php000064400000004340144761607160011474 0ustar00action = $action; if(!isset($data["_index"])) { print_r($oData); print_r($data); throw new \Exception("Invalid data for ResultItem "); } } if(isset($data["_index"])) { $this->index = $data["_index"]; } if(isset($data["_id"])) { $this->id = $data["_id"]; } if(isset($data["_version"])) { $this->version = $data["_version"]; } if(isset($data["result"])) { $this->result = $data["result"]; } if(isset($data["_shards"])) { $this->shards = new Shards($data["_shards"]); } if(isset($data["status"])) { $this->status = $data["status"]; } if(isset($data["_seq_no"])) { $this->seqNo = $data["_seq_no"]; } if(isset($data["_primary_term"])) { $this->primaryTerm = $data["_primary_term"]; } if(isset($data["error"])) { $this->error = $data["error"]; } $this->set($data); } public function action() { return $this->action; } public function index() { return $this->index; } public function id() { return $this->id; } public function version() { return $this->version; } public function result() { return $this->result; } public function shards() { return $this->shards; } public function status() { return $this->status; } public function seqNo() { return $this->seqNo; } public function primaryTerm() { return $this->primaryTerm; } public function error() { return $this->error; } }src/result/Results.php000064400000003524144761607160011043 0ustar00loadResults($resultData); } } public function loadResults($resultData) { $this->took = $resultData["took"]; $this->timedOut = $resultData["timed_out"]; $this->shards = new Shards($resultData["_shards"]); $this->hits = new Hits($resultData["hits"]); unset($resultData["took"]); unset($resultData["timed_out"]); unset($resultData["_shards"]); unset($resultData["hits"]); if(isset($resultData["aggregations"])) { foreach($resultData["aggregations"] as $name => $aggregation) { $this->aggregations[$name] = new Aggregation($name,$aggregation); } unset($resultData["aggregations"]); } $this->set($resultData); } public function took() { return $this->took; } /** * Use in Search results to determine if the search timed out * @return bool */ public function timedOut() { return $this->timedOut ? true : false; } public function shards() { return $this->shards; } public function hits() { return $this->hits; } public function aggregations() { return $this->aggregations; } /** * @param string $name * @return Aggregation|bool */ public function aggregation($name) { if(isset($this->aggregations[$name])) { return $this->aggregations[$name]; } return false; } }src/result/Shards.php000064400000000615144761607160010624 0ustar00get("total"); } public function successful() { return $this->get("successful"); } public function skipped() { return $this->get("skipped"); } public function failed() { return $this->get("failed"); } }src/type/ArrayIterator.php000064400000003160144761607160011631 0ustar00arr = $arr; } public function rewind() { return reset($this->arr); } public function current() { if($this->pos == -1) { $this->pos = 0; } $value = current($this->arr); if($value) { return $this->transformIteratorValue($value); } return false; } public function key() { return key($this->arr); } public function next() { if($this->pos == -1) { $returnValue = $this->current(); } else { $returnValue = next($this->arr); } if($returnValue) { return $this->transformIteratorValue($returnValue); } return false; } public function valid() { return key($this->arr) !== null; } public function getAll() { return $this->arr; } public function getAt($i) { if(isset($this->arr[$i])) { return $this->transformIteratorValue($this->arr[$i]); } return false; } public function transformIteratorValue($value) { return $value; } }src/type/BaseType.php000064400000002152144761607160010555 0ustar00data; } return dhGlobal::getVal($this->data,$key,$default,"."); } public function set($key,$value=null) { if(is_array($key)) { $this->data = $key; return $this; } if(strpos($key,".")===false) { $this->data[$key] = $value; return $this; } dhGlobal::dotAssign($this->data,$key,$value,"."); return $this; } public function toArray() { return $this->data; } public function toJson() { return json_encode($this->toArray(),JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); } public function __toString() { return $this->toJson(); } public function jsonSerialize() { return $this->toArray(); } public static function instance() { return new static(); } }src/type/CollapseType.php000064400000001072144761607160011445 0ustar00$field ]; if(!is_null($innerHits)) { $template["inner_hits"] = $innerHits; } $this->set($template); } public function innerHits($innerHits) { $this->set("inner_hits",$innerHits); } public static function instance($field=null,$innerHits=null) { return new static($field,$innerHits); } }src/type/InnerHitsType.php000064400000005327144761607160011615 0ustar00 [ "name" => null, "sort" => null, "collapse" => null, "fields" => null, "size" => null, "_source" => false, ] ]; $this->set($template); if(!is_null($name)) { $this->name($name); } if(!is_null($fields)) { $this->fields($fields); } if(!is_null($size)) { $this->size($size); } if(!is_null($sort)) { $this->sort($sort); } if(!is_null($collapse)) { $this->collapse($collapse); } if(!is_null($source)) { $this->source($source); } } public function toArray() { $name = $this->get("inner_hits.name"); $sort = $this->get("inner_hits.sort"); $collapse = $this->get("inner_hits.collapse"); $fields = $this->get("inner_hits.fields"); $size = $this->get("inner_hits.size"); $source = $this->get("inner_hits._source"); $data = []; if(!is_null($name)) { $data["name"] = $name; } if(!is_null($sort)) { $data["sort"] = $sort; } if(!is_null($collapse)) { $data["collapse"] = $collapse; } if(!is_null($fields)) { $data["fields"] = $fields; } if(!is_null($size)) { $data["size"] = $size; } if(!is_null($source)) { $data["_source"] = $source; } return $data; } public function name($name) { $this->set("inner_hits.name",$name); return $this; } public function sort($sort) { $this->set("inner_hits.sort",$sort); return $this; } public function collapse($collapse) { $this->set("inner_hits.collapse",$collapse); return $this; } public function fields($fields) { $this->set("inner_hits.fields",$fields); return $this; } public function size($size) { $this->set("inner_hits.size",$size); return $this; } public function source($source) { $this->set("inner_hits._source",$source); return $this; } public static function instance($name=NULL,$sort=NULL,$fields=NULL,$size=NULL,$collapse=NULL,$source=NULL) { return new static($name,$sort,$fields,$size,$collapse,$source); } }src/type/SortType.php000064400000000762144761607160010637 0ustar00 ["order"=>$order] ]; $this->set($template); } public function short() { $data = $this->get(); $field = array_keys($data)[0]; $order = $data[$field]["order"]; $template = [ $field => $order ]; $this->set($template); } }