From 4fe418a79c04f7c84a107625184caca6ef1ed21f Mon Sep 17 00:00:00 2001 From: Federico Panini Date: Thu, 17 Aug 2017 16:26:56 +0200 Subject: [PATCH] In ES6 is not possible to use in QueryString fields parameters in conjunction with default_field --- CHANGELOG.md | 3 +- lib/Elastica/Query/QueryString.php | 2 ++ test/Elastica/Query/QueryStringTest.php | 39 +++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8b0abf17a..b83f9e027a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,8 @@ All notable changes to this project will be documented in this file based on the - Replace IndexAlreadyExistsException with [ResourceAlreadyExistsException](https://github.com/elastic/elasticsearch/pull/21494) [#1350](https://github.com/ruflin/Elastica/pull/1350) - in order to delete an index you should not delete by its alias now you should delete using the [concrete index name](https://github.com/elastic/elasticsearch/blob/6.0/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java#L445) [#1348](https://github.com/ruflin/Elastica/pull/1348) - Removed ```optimize``` from Index class as it has been deprecated in ES 2.1 and removed in [ES 5.x+](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-optimize.html) use forcemerge [#1351](https://github.com/ruflin/Elastica/pull/1350) - +- In QueryString is not allowed to use fields parameters in conjunction with default_field parameter. This is not well documented, it's possibile to understand from [Elasticsearch tests : QueryStringQueryBuilderTests.java](https://github.com/elastic/elasticsearch/blob/6.0/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java#L917) [#1352](https://github.com/ruflin/Elastica/pull/1352) + ### Bugfixes - Enforce [Content-Type requirement on the layer Rest](https://github.com/elastic/elasticsearch/pull/23146), a [PR on Elastica #1301](https://github.com/ruflin/Elastica/issues/1301) solved it (it has been implemented only in the HTTP Transport), but it was not implemented in the Guzzle Transport. [#1349](https://github.com/ruflin/Elastica/pull/1349) diff --git a/lib/Elastica/Query/QueryString.php b/lib/Elastica/Query/QueryString.php index eaba20b495..b7dc5b63fc 100644 --- a/lib/Elastica/Query/QueryString.php +++ b/lib/Elastica/Query/QueryString.php @@ -49,6 +49,7 @@ public function setQuery($query = '') /** * Sets the default field. + * You cannot set fields and default_field * * If no field is set, _all is chosen * @@ -202,6 +203,7 @@ public function setAutoGeneratePhraseQueries($autoGenerate = true) /** * Sets the fields. If no fields are set, _all is chosen. + * You cannot set fields and default_field * * @param array $fields Fields * diff --git a/test/Elastica/Query/QueryStringTest.php b/test/Elastica/Query/QueryStringTest.php index 3f15153b7e..ec088bc2b6 100644 --- a/test/Elastica/Query/QueryStringTest.php +++ b/test/Elastica/Query/QueryStringTest.php @@ -2,6 +2,7 @@ namespace Elastica\Test\Query; use Elastica\Document; +use Elastica\Exception\ResponseException; use Elastica\Query\QueryString; use Elastica\Test\Base as BaseTest; @@ -65,8 +66,6 @@ public function testSearch() */ public function testSearchFields() { - $this->markTestSkipped('ES6 update: failed to create query'); - $index = $this->_createIndex(); $type = $index->getType('test'); @@ -76,13 +75,47 @@ public function testSearchFields() $query = new QueryString(); $query = $query->setQuery('ruf*'); - $query = $query->setDefaultField('title'); $query = $query->setFields(['title', 'firstname', 'lastname', 'price', 'year']); $resultSet = $type->search($query); $this->assertEquals(1, $resultSet->count()); } + /** + * Tests if search in multiple fields is possible. + * + * @group functional + */ + public function testSearchFieldsValidationException() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, ['title' => 'hello world', 'firstname' => 'nicolas', 'lastname' => 'ruflin', 'price' => '102', 'year' => '2012']); + $type->addDocument($doc); + $index->refresh(); + + $query = new QueryString(); + $query = $query->setQuery('ruf*'); + $query = $query->setDefaultField('title'); + $query = $query->setFields(['title', 'firstname', 'lastname', 'price', 'year']); + + try { + $resultSet = $type->search($query); + } catch (ResponseException $ex) { + $error = $ex->getResponse()->getFullError(); + + $this->assertContains('query_shard_exception', $error['root_cause'][0]['type']); + $this->assertContains('failed to create query', $error['root_cause'][0]['reason']); + + + $this->assertContains('query_validation_exception', $error); + $this->assertContains('[fields] parameter in conjunction with [default_field]', $error['failed_shards'][0]['reason']['caused_by']['reason']); + + $this->assertEquals(400, $ex->getResponse()->getStatus()); + } + } + /** * @group unit */