From 633fb12050e681c090d6d67381964586d17b6b46 Mon Sep 17 00:00:00 2001 From: Nicolas Ruflin Date: Wed, 30 Mar 2011 18:11:52 +0200 Subject: [PATCH] Basic facet implementation added License file added Query and search refactoring Filtered query added Plase check changes file Some old functions removed --- LICENSE.txt | 202 +++++++++++++++++++ changes.txt | 9 + lib/Elastica/Facet/Abstract.php | 34 ++++ lib/Elastica/Facet/Terms.php | 49 +++++ lib/Elastica/Facets.php | 45 +++++ lib/Elastica/Filter.php | 18 +- lib/Elastica/Query.php | 121 ++++------- lib/Elastica/Query/Abstract.php | 2 +- lib/Elastica/Query/Filtered.php | 35 ++++ lib/Elastica/Request.php | 18 +- lib/Elastica/ResultSet.php | 5 + test/lib/Elastica/Facet/TermsTest.php | 44 ++++ test/lib/Elastica/FacetsTest.php | 32 +++ test/lib/Elastica/Filter/GeoDistanceTest.php | 57 +++--- test/lib/Elastica/Index/SettingsTest.php | 3 + test/lib/Elastica/Query/Filtered.php | 44 ++++ 16 files changed, 582 insertions(+), 136 deletions(-) create mode 100644 LICENSE.txt create mode 100644 lib/Elastica/Facet/Abstract.php create mode 100644 lib/Elastica/Facet/Terms.php create mode 100644 lib/Elastica/Facets.php create mode 100644 lib/Elastica/Query/Filtered.php create mode 100644 test/lib/Elastica/Facet/TermsTest.php create mode 100644 test/lib/Elastica/FacetsTest.php create mode 100644 test/lib/Elastica/Query/Filtered.php diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/changes.txt b/changes.txt index f2f2fe6dba..0cf135244d 100644 --- a/changes.txt +++ b/changes.txt @@ -1,4 +1,13 @@ CHANGES +2011-03-30 +- Filtered query introduced +- setRawArguments in Elastica_Query is now setParam + +2011-03-29 +- Renaming Elastica_Filter->addQuery, addFilter to setQuery, setFilter +- Add parts of Facets API +- Add facet Terms +- Renaming Elastica_Query->addFilter to setFilter 2011-03-24 - Renaming of Elastica_Status_Index to Elastica_Index_Status => API Change! diff --git a/lib/Elastica/Facet/Abstract.php b/lib/Elastica/Facet/Abstract.php new file mode 100644 index 0000000000..94f42c2529 --- /dev/null +++ b/lib/Elastica/Facet/Abstract.php @@ -0,0 +1,34 @@ + + */ +abstract class Elastica_Facet_Abstract +{ + protected $_global = false; + protected $_name = ''; + + public function __construct($name) { + $this->setName($name); + } + + public function setName($name) { + if (empty($name)) { + throw new Elastica_Exception_Invalid('Facet name has to be set'); + } + $this->_name = $name; + return $this; + } + + public function getName() { + return $this->_name; + } + + public function setFilter(Elastica_Filter_Abstract $filter) { + } + + abstract function toArray(); +} diff --git a/lib/Elastica/Facet/Terms.php b/lib/Elastica/Facet/Terms.php new file mode 100644 index 0000000000..da241a418b --- /dev/null +++ b/lib/Elastica/Facet/Terms.php @@ -0,0 +1,49 @@ + + */ +class Elastica_Facet_Terms extends Elastica_Facet_Abstract +{ + protected $_params = array(); + + public function setField($field) { + return $this->setParam('field', $field); + } + + public function setFields(array $fields) { + return $this->setParam('field', $fields); + } + + public function setAllTerms($allTerms) { + $allTerms = (bool) $allTerms; + return $this->setParam('all_terms', $allTerms); + } + + public function setExclude(array $exclude) { + return $this->setParam('exclude', $exclude); + } + + /** + * Sets a general parameter for this Facet by key and value + * + * @param string $key Key to set + * @param mixed $value Value + */ + public function setParam($key, $value) { + $this->_params[$key] = $value; + return $this; + } + + public function setSize($size) { + $size = (int) $size; + return $this->setParam('size', $size); + } + + public function toArray() { + return array('terms' => $this->_params); + } +} diff --git a/lib/Elastica/Facets.php b/lib/Elastica/Facets.php new file mode 100644 index 0000000000..3fc91271cf --- /dev/null +++ b/lib/Elastica/Facets.php @@ -0,0 +1,45 @@ + + */ +class Elastica_Facets +{ + protected $_facets = array(); + + public function __construct() { + + } + + public function setGlobal($global = true) { + $this->_global = $global; + } + + public function setScope($scope) { + $this->_scope = $scope; + } + + public function addFacet(Elastica_Facet_Abstract $facet) { + $this->_facets[] = $facet; + } + + public function setFilter(Elastica_Filter_Abstract $filter) { + } + + public function set(array $facets) { + $this->_facets = $facets; + } + + public function toArray() { + $params = array(); + + foreach ($this->_facets as $facet) { + $params[$facet->getName()] = $facet->toArray(); + } + + return array('facets' => $params); + } +} diff --git a/lib/Elastica/Filter.php b/lib/Elastica/Filter.php index 04d5567bf8..786da802e6 100755 --- a/lib/Elastica/Filter.php +++ b/lib/Elastica/Filter.php @@ -5,30 +5,22 @@ * @category Xodoa * @package Elastica * @author Nicolas Ruflin - * @todo Not implemented yet */ class Elastica_Filter { protected $_query; protected $_filter; - public function __construct(Elastica_Filter_Abstract $filter = null, Elastica_Query_Abstract $query = null) { - $this->_query = $query; - $this->_filter = $filter; + public function __construct(Elastica_Filter_Abstract $filter) { + $this->setFilter($filter); } - public function addFilter(Elastica_Filter_Abstract $filter) { + public function setFilter(Elastica_Filter_Abstract $filter) { $this->_filter = $filter; - } - - public function addQuery(Elastica_Query_Abstract $query) { - $this->_query = $query; + return $this; } public function toArray() { - return array('filtered' => array( - 'query' => $this->_query->toArray(), - 'filter' => $this->_filter->toArray() - )); + return array('filter' => $this->_filter->toArray()); } } diff --git a/lib/Elastica/Query.php b/lib/Elastica/Query.php index 52339f5fc8..1e94624fc3 100755 --- a/lib/Elastica/Query.php +++ b/lib/Elastica/Query.php @@ -11,20 +11,7 @@ */ class Elastica_Query { - const TERM = 'term'; - const RANGE = 'range'; - const WILDCARD = 'wildcard'; - const QUERY_STRING = 'query_string'; - - protected $_rawArguments = array(); - protected $_from = 0; - protected $_limit = 10; - protected $_sortArgs = array(); - protected $_explain = false; - protected $_fields = array(); - protected $_scriptFields = array(); protected $_query = array(); - protected $_filters = array(); /** * Creates a query object @@ -40,7 +27,7 @@ public function __construct($query = null) { } /** - * Adds query as raw array + * Sets query as raw array. Will overwrite all already set arguments * * @param array $query Query array * @return Elastica_Query Query object @@ -50,6 +37,17 @@ public function setRawQuery(array $query) { return $this; } + /** + * Sets a single param for the query + * + * @param string $key Key to set + * @param mixed $value Value + */ + public function setParam($key, $value) { + $this->_query[$key] = $value; + return $this; + } + /** * Sets the query * @@ -57,8 +55,7 @@ public function setRawQuery(array $query) { * @return Elastica_Query Query object */ public function setQuery(Elastica_Query_Abstract $query) { - $this->_query = $query->toArray(); - return $this; + return $this->setParam('query', $query->toArray()); } /** @@ -69,15 +66,16 @@ public function addQuery(Elastica_Query_Abstract $query) { $this->setQuery($query); } - public function addFilter(Elastica_Filter $filter) { - $this->_filters = $filter->toArray(); - return $this; + /** + * @param Elastica_Filter $filter Filter object + * @return Elastica_Query Current object + */ + public function setFilter(Elastica_Filter_Abstract $filter) { + return $this->setParam('filter', $filter->toArray()); } - public function setFrom($from) { - $this->_from = $from; - return $this; + return $this->setParam('from', $from); } /** @@ -89,8 +87,7 @@ public function setFrom($from) { * @return Elastica_Query Query object */ public function setSort(array $sortArgs) { - $this->_sortArgs = $sortArgs; - return $this; + return $this->setParam('sort', $sortArgs); } /** @@ -101,7 +98,7 @@ public function setSort(array $sortArgs) { * @return Elastica_Query Query object */ public function addSort($sort) { - $this->_sortArgs[] = $sort; + $this->_query['sort'][] = $sort; return $this; } @@ -113,8 +110,7 @@ public function addSort($sort) { * @return Elastica_Query Query object */ public function setHighlight(array $highlightArgs) { - $this->_highlightArgs = $highlightArgs; - return $this; + return $this->setParam('highlight', $highlightArgs); } /** @@ -125,7 +121,7 @@ public function setHighlight(array $highlightArgs) { * @return Elastica_Query Query object */ public function addHighlight($highlight) { - $this->_highlightArgs[] = $highlight; + $this->_query['highligth'][] = $highlight; return $this; } @@ -137,7 +133,6 @@ public function addHighlight($highlight) { */ public function setSize($limit = 10) { return $this->setLimit($limit); - return $this; } /** @@ -147,8 +142,7 @@ public function setSize($limit = 10) { * @return Elastica_Query Query object */ public function setLimit($limit = 10) { - $this->_limit = $limit; - return $this; + return $this->setParam('limit', $limit); } /** @@ -158,8 +152,7 @@ public function setLimit($limit = 10) { * @param bool $explain OPTIONAL Enabled or disable explain (default = true) */ public function setExplain($explain = true) { - $this->_explain = $explain; - return $this; + return $this->setParam('explain', $explain); } /** @@ -169,8 +162,7 @@ public function setExplain($explain = true) { * @param array $fields Fields to be returne */ public function setFields(array $fields) { - $this->_fields = $fields; - return $this; + return $this->setParam('fields', $fields); } /** @@ -180,68 +172,27 @@ public function setFields(array $fields) { * @param array $scriptFields Script fields */ public function setScriptFields(array $scriptFields) { - $this->_scriptFields = $scriptFields; - return $this; + return $this->setParam('script_field', $scriptFields); } /** - * Allows to set raw arguments that can't be set over the - * provided method. Field name has also to be set in given array. - * Values set here are overrided by values set - * over the specific methods - * - * @param array $args Argument array + * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/search/facets */ - public function setRawArguments(array $args) { - $this->_rawArgumens = $args; - return $this; + public function setFacets(Elastica_Facets $facets) { + return $this->setParam('facets', $facets->toArray()); } - /** - * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/search/facets - */ - public function setFacets(array $args) { - throw new Elastica_Exception('not implemented yet'); + public function addFacet(Elastica_Facet_Abstract $facet) { + $this->_query['facets'][$facet->getName()] = $facet->toArray(); return $this; } public function toArray() { - $query = $this->_rawArguments; - - $query['query'] = $this->_query; - - if (!empty($this->_limit)) { - $query['size'] = $this->_limit; - } - - $query['from'] = $this->_from; - - if (!empty($this->_sortArgs)) { - $query['sort'] = $this->_sortArgs; - } - - if (!empty($this->_highlightArgs)) { - $query['highlight'] = $this->_highlightArgs; - } - - if ($this->_explain) { - $query['explain'] = $this->_explain; - } - - if (!empty($this->_fields)) { - $query['fields'] = $this->_fields; - } - - if (!empty($this->_scriptFields)) { - $query['script_fields'] = $this->_scriptFields; - } - - if (!empty($this->_filters)) { - // TODO: should query really be overwritten? - $query['query'] = $this->_filters; + if (!isset($this->_query['query'])) { + $this->setQuery(new Elastica_Query_MatchAll()); } - return $query; + return $this->_query; } } \ No newline at end of file diff --git a/lib/Elastica/Query/Abstract.php b/lib/Elastica/Query/Abstract.php index 6908d4234c..ff896868bc 100755 --- a/lib/Elastica/Query/Abstract.php +++ b/lib/Elastica/Query/Abstract.php @@ -6,7 +6,7 @@ * @package Elastica * @author Nicolas Ruflin */ -abstract class Elastica_Query_Abstract +abstract class Elastica_Query_Abstract { abstract public function toArray(); } diff --git a/lib/Elastica/Query/Filtered.php b/lib/Elastica/Query/Filtered.php new file mode 100644 index 0000000000..a8737e412c --- /dev/null +++ b/lib/Elastica/Query/Filtered.php @@ -0,0 +1,35 @@ + + */ +class Elastica_Query_Filtered extends Elastica_Query_Abstract +{ + protected $_query = null; + protected $_filter = null; + + public function __construct(Elastica_Query_Abstract $query, Elastica_Filter_Abstract $filter) { + $this->setQuery($query); + $this->setFilter($filter); + } + + public function setQuery(Elastica_Query_Abstract $query) { + $this->_query = $query; + } + + public function setFilter(Elastica_Filter_Abstract $filter) { + $this->_filter = $filter; + } + + public function toArray() { + return array('filtered' => array( + 'query' => $this->_query->toArray(), + 'filter' => $this->_filter->toArray() + )); + } +} diff --git a/lib/Elastica/Request.php b/lib/Elastica/Request.php index dee09d608c..24805590ea 100755 --- a/lib/Elastica/Request.php +++ b/lib/Elastica/Request.php @@ -3,43 +3,43 @@ // Stores query time, and result array -> is given to resultset, returned by ... class Elastica_Request { - + const POST = 'POST'; const PUT = 'PUT'; const GET = 'GET'; const DELETE = 'DELETE'; - + protected $_path; // TODO: set default method? protected $_method; protected $_data; - + public function __construct($path, $method, $data = array()) { $this->_path = $path; $this->_method = $method; $this->_data = $data; } - + public function setMethod($method) { $this->_method = $method; } - + public function getMethod() { return $this->_method; } - + public function setData($data) { $this->_data = $data; } - + public function getData() { return $this->_data; } - + public function setPath($path) { $this->_path = $path; } - + public function getPath() { return $this->_path; } diff --git a/lib/Elastica/ResultSet.php b/lib/Elastica/ResultSet.php index c7fdf43d6c..478fce3629 100755 --- a/lib/Elastica/ResultSet.php +++ b/lib/Elastica/ResultSet.php @@ -36,6 +36,11 @@ public function getResults() { return $this->_results; } + public function getFacets() { + $data = $this->_response->getData(); + return $data['facets']; + } + public function getTotalHits() { return intval($this->_totalHits); } diff --git a/test/lib/Elastica/Facet/TermsTest.php b/test/lib/Elastica/Facet/TermsTest.php new file mode 100644 index 0000000000..b768d7b8d8 --- /dev/null +++ b/test/lib/Elastica/Facet/TermsTest.php @@ -0,0 +1,44 @@ +getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Elastica_Document(1, array('name' => 'nicolas ruflin')); + $type->addDocument($doc); + $doc = new Elastica_Document(2, array('name' => 'ruflin test')); + $type->addDocument($doc); + $doc = new Elastica_Document(2, array('name' => 'nicolas helloworld')); + $type->addDocument($doc); + + + $facet = new Elastica_Facet_Terms('test'); + $facet->setField('name'); + + $query = new Elastica_Query(); + $query->addFacet($facet); + $query->setQuery(new Elastica_Query_MatchAll()); + + $index->refresh(); + + $response = $type->search($query); + $facets = $response->getFacets(); + + $this->assertEquals(3, count($facets['test']['terms'])); + } +} \ No newline at end of file diff --git a/test/lib/Elastica/FacetsTest.php b/test/lib/Elastica/FacetsTest.php new file mode 100644 index 0000000000..60abe689e0 --- /dev/null +++ b/test/lib/Elastica/FacetsTest.php @@ -0,0 +1,32 @@ +markTestIncomplete(); + + $termFacet = new Elastica_Facet_Terms('test'); + $termFacet->setField('hello'); + $termFacet->setSize(10); + print_r($termFacet->toArray()); + + $facet = new Elastica_Facets(); + $facet->addFacet($termFacet); + + print_r($facet->toArray()); + + + + } +} \ No newline at end of file diff --git a/test/lib/Elastica/Filter/GeoDistanceTest.php b/test/lib/Elastica/Filter/GeoDistanceTest.php index bfc6999886..990a30fed8 100755 --- a/test/lib/Elastica/Filter/GeoDistanceTest.php +++ b/test/lib/Elastica/Filter/GeoDistanceTest.php @@ -5,59 +5,60 @@ class Elastica_Filter_GeoDistanceTest extends PHPUnit_Framework_TestCase { - public function setUp() { - } - - public function tearDown() { - } - - public function testGeoPoint() { - $client = new Elastica_Client(); - $index = $client->getIndex('test'); + public function setUp() { + } + + public function tearDown() { + } + + public function testGeoPoint() { + $client = new Elastica_Client(); + $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); - + // Set mapping $type->setMapping(array('point' => array('type' => 'geo_point'))); - + // Add doc 1 - $doc1 = new Elastica_Document(1, + $doc1 = new Elastica_Document(1, array( 'name' => 'ruflin', ) ); - - $doc1->addGeoPoint('point', 17, 19); - $type->addDocument($doc1); + + $doc1->addGeoPoint('point', 17, 19); + $type->addDocument($doc1); // Add doc 2 - $doc2 = new Elastica_Document(2, + $doc2 = new Elastica_Document(2, array( 'name' => 'ruflin', ) ); - - $doc2->addGeoPoint('point', 30, 40); - $type->addDocument($doc2); + $doc2->addGeoPoint('point', 30, 40); + $type->addDocument($doc2); + + + $index->optimize(); + $index->refresh(); - $index->optimize(); - $index->refresh(); - // Only one point should be in radius $query = new Elastica_Query(); $geoFilter = new Elastica_Filter_GeoDistance('point', 30, 40, '1km'); - $filter = new Elastica_Filter($geoFilter, new Elastica_Query_MatchAll()); - $query->addFilter($filter); + + $query = new Elastica_Query(new Elastica_Query_MatchAll()); + $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); - + // Both points should be inside $query = new Elastica_Query(); $geoFilter = new Elastica_Filter_GeoDistance('point', 30, 40, '40000km'); - $filter = new Elastica_Filter($geoFilter, new Elastica_Query_MatchAll()); - $query->addFilter($filter); + $query = new Elastica_Query(new Elastica_Query_MatchAll()); + $query->setFilter($geoFilter); $this->assertEquals(2, $type->search($query)->count()); - } + } } diff --git a/test/lib/Elastica/Index/SettingsTest.php b/test/lib/Elastica/Index/SettingsTest.php index 069e9f1fcb..7c849913a3 100644 --- a/test/lib/Elastica/Index/SettingsTest.php +++ b/test/lib/Elastica/Index/SettingsTest.php @@ -39,6 +39,7 @@ public function testSetNumberOfReplicas() { } public function testSetNumberOfShards() { + $this->markTestIncomplete(); $indexName = 'test'; $client = new Elastica_Client(); @@ -56,6 +57,8 @@ public function testSetNumberOfShards() { } public function testSetRefreshInterval() { + $this->markTestIncomplete(); + $indexName = 'test'; $client = new Elastica_Client(); diff --git a/test/lib/Elastica/Query/Filtered.php b/test/lib/Elastica/Query/Filtered.php new file mode 100644 index 0000000000..e5351986b3 --- /dev/null +++ b/test/lib/Elastica/Query/Filtered.php @@ -0,0 +1,44 @@ +getIndex('test'); + $index->create(array(), true); + $type = $index->getType('helloworld'); + + $doc = new Elastica_Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $doc = new Elastica_Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + + $queryString = new Elastica_Query_QueryString('test*'); + + $filter1 = new Elastica_Filter_Term(); + $filter1->addTerm('username', 'peter'); + + $filter2 = new Elastica_Filter_Term(); + $filter2->addTerm('username', 'qwerqwer'); + + $query1 = new Elastica_Query_Filtered($queryString, $filter1); + $query2 = new Elastica_Query_Filtered($queryString, $filter2); + $index->refresh(); + + $resultSet = $type->search($queryString); + $this->assertEquals(2, $resultSet->count()); + + $resultSet = $type->search($query1); + $this->assertEquals(1, $resultSet->count()); + + $resultSet = $type->search($query2); + $this->assertEquals(0, $resultSet->count()); + } +}