Skip to content

Commit

Permalink
Fixing IN query
Browse files Browse the repository at this point in the history
  • Loading branch information
pwaterz committed Mar 16, 2018
1 parent b0311c5 commit 591342e
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Drupal\elasticentityquery;

use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Query\QueryBase;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Entity\Query\QueryInterface;
use Elasticsearch\Client;

/**
* The SQL storage entity query class.
Expand All @@ -31,15 +29,15 @@ class Query extends QueryBase implements QueryInterface {
*
* @var array
*/
protected $sqlFields = array();
protected $sqlFields = [];

/**
* An array of strings added as to the group by, keyed by the string to avoid
* duplicates.
*
* @var array
*/
protected $sqlGroupBy = array();
protected $sqlGroupBy = [];

/**
* @var \Elasticsearch\Client
Expand All @@ -62,11 +60,11 @@ class Query extends QueryBase implements QueryInterface {
* - AND: all of the conditions on the query need to match.
* - OR: at least one of the conditions on the query need to match.
* @param \Elasticsearch\Client $client
* Elasticsearch client to use
* Elasticsearch client to use.
* @param array $namespaces
* List of potential namespaces of the classes belonging to this query.
*/
public function __construct(EntityTypeInterface $entity_type, $conjunction = 'AND', \Elasticsearch\Client $client, array $namespaces) {
public function __construct(EntityTypeInterface $entity_type, $conjunction = 'AND', Client $client, array $namespaces) {
parent::__construct($entity_type, $conjunction, $namespaces);
$this->client = $client;
}
Expand All @@ -82,14 +80,20 @@ public function execute() {
}
else {
// TODO: support for revisions? See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Query%21QueryInterface.php/function/QueryInterface%3A%3Aexecute/8.2.x
$results = array();
$results = [];
foreach ($result['hits']['hits'] as $hit) {
$results[$hit['_id']] = $this->includeSource() ? $hit['_source'] : $hit['_id'];
}
return $results;
}
}

/**
* Get the results of the elastic query.
*
* @return array
* An array of results from elastic search.
*/
public function getResult() {
$params = $this->buildRequest();

Expand All @@ -103,10 +107,12 @@ public function getResult() {
return $result;
}

public function debug() {
return $this->buildRequest();
}

/**
* Get the elastic indexes to search
*
* @return array
* An array of elastic indexes.
*/
public function getIndex() {
return $this->entityTypeId;
}
Expand All @@ -119,15 +125,22 @@ public function includeSource() {
return FALSE;
}

/**
* Build the elatic index request.
*
* @return array
* A request array that can be sent using the
* elasticsearch client.
*/
protected function buildRequest() {
$params = [
'index' => $this->getIndex(),
];

// For regular (non-count) queries
if (!$this->count) {
// Don't include source
$params['body']['_source'] = $this->includeSource();
// Don't include source.
$params['body']['_source'] = $this->includeSource();
}

// Top-level condition
Expand Down Expand Up @@ -182,13 +195,13 @@ private function getElasticFilterItem(\Drupal\Core\Entity\Query\Sql\Condition $c
$bool['should'][] = ['bool' => $this->getElasticFilterItem($field)];
}
elseif ($operator == "=" && $conjunction == "AND") {
$bool['filter'][] = ['term' => [$field => $value]];
$bool['filter'][] = ['terms' => [$field => $value]];
}
elseif ($operator == "=" && $conjunction == "OR") {
$bool['should'][] = ['term' => [$field => $value]];
$bool['should'][] = ['terms' => [$field => $value]];
}
elseif (($operator == "<>" || $operator == "!=") && $conjunction == "AND") {
$bool['must_not'][] = ['term' => [$field => $value]];
$bool['must_not'][] = ['terms' => [$field => $value]];
}
elseif (($operator == "<>" || $operator == "!=") && $conjunction == "OR") {
$bool['should'][] = ['bool' => ['must_not' => ['term' => [$field => $value]]]];
Expand Down Expand Up @@ -290,7 +303,7 @@ public function addType($type) {

public function hasCondition($field, $operator = NULL) {
foreach ($this->condition->conditions() as $i => $subcondition) {
if ($subcondition['field'] == $field) {
if ($subcondition['field'] == $field ) {
return TRUE;
}
}
Expand Down

0 comments on commit 591342e

Please sign in to comment.