Skip to content

Commit

Permalink
In ES6 is not possible to use in QueryString fields parameters in con…
Browse files Browse the repository at this point in the history
…junction with default_field
  • Loading branch information
p365labs committed Aug 17, 2017
1 parent c46aad9 commit 37aa08b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -11,7 +11,8 @@ All notable changes to this project will be documented in this file based on the
- [store](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/mapping-store.html) field only accepts boolean
- 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)

- 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)

Expand Down
2 changes: 2 additions & 0 deletions lib/Elastica/Query/QueryString.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
39 changes: 36 additions & 3 deletions test/Elastica/Query/QueryStringTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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');

Expand All @@ -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
*/
Expand Down

0 comments on commit 37aa08b

Please sign in to comment.