Skip to content

Commit

Permalink
Patch: Updated sorting to allow specifying unmapped_type option. (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
seeruk authored Jun 23, 2017
1 parent b7fb30a commit 32d2949
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/Byng/Pimcore/Elasticsearch/Gateway/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ protected function processQuery(QueryInterface $query)

case "sort":
$result = [];
foreach ($query->getCriteria() as $column => $order) {
$result[$column]["order"] = $order;
foreach ($query->getCriteria() as $sorting) {
$result[] = $sorting;
}
break;

Expand Down
68 changes: 34 additions & 34 deletions lib/Byng/Pimcore/Elasticsearch/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Byng\Pimcore\Elasticsearch\Query\QueryInterface;

/**
*
*
* QueryBuilder defines the query to send to elasticsearch.
*
* @author Asim Liaquat <[email protected]>
Expand All @@ -27,12 +27,12 @@ class QueryBuilder
* @var Query
*/
private $query;

/**
* @var Filter
*/
private $filter;

/**
* @var int
*/
Expand All @@ -42,15 +42,15 @@ class QueryBuilder
* @var int
*/
private $from;

/**
* @var Sort
*/
private $sort;

/**
* QueryBuilder constructor
*
*
* @param Query $query
* @param Filter $filter
*/
Expand All @@ -62,7 +62,7 @@ public function __construct(Query $query = null, Filter $filter = null)

/**
* Get query
*
*
* @return Query
*/
public function getQuery()
Expand All @@ -72,7 +72,7 @@ public function getQuery()

/**
* Get filter
*
*
* @return Filter
*/
public function getFilter()
Expand All @@ -82,7 +82,7 @@ public function getFilter()

/**
* Set query
*
*
* @param Query $query
*/
public function setQuery(Query $query)
Expand All @@ -92,7 +92,7 @@ public function setQuery(Query $query)

/**
* Set filter
*
*
* @param Filter $filter
*/
public function setFilter(Filter $filter)
Expand All @@ -102,7 +102,7 @@ public function setFilter(Filter $filter)

/**
* Get the number or results to return
*
*
* @return int
*/
public function getSize()
Expand All @@ -112,7 +112,7 @@ public function getSize()

/**
* Set the number of results to return
*
*
* @param int $size
*/
public function setSize($size)
Expand All @@ -122,7 +122,7 @@ public function setSize($size)

/**
* Get the offset to fetch the results from
*
*
* @return int|null
*/
public function getFrom()
Expand All @@ -132,7 +132,7 @@ public function getFrom()

/**
* Set the offset to fetch the results from
*
*
* @param int $from
*/
public function setFrom($from)
Expand All @@ -142,7 +142,7 @@ public function setFrom($from)

/**
* Get sort criteria
*
*
* @return Sort
*/
public function getSort()
Expand All @@ -152,7 +152,7 @@ public function getSort()

/**
* Set sort criteria
*
*
* @param Sort $sort
*/
public function setSort(Sort $sort)
Expand All @@ -168,27 +168,27 @@ public function setSort(Sort $sort)
public function toArray()
{
$body = [];

if ($query = $this->getQuery()) {
$body = $this->processQuery($query);
}

if ($filter = $this->getFilter()) {
$body = array_merge($body, $this->processQuery($filter));
}

if ($from = $this->getFrom()) {
$body["from"] = $from;
}

if ($size = $this->getSize()) {
$body["size"] = $size;
}

if ($sort = $this->getSort()) {
$body["sort"] = $this->processQuery($sort);
}

return $body;
}

Expand All @@ -202,15 +202,15 @@ public function toArray()
protected function processQuery(QueryInterface $query)
{
switch ($query->getType()) {

case "query":
$result["query"] = $this->processQuery($query->getBoolQuery());
break;

case "filter":
$result["filter"] = $this->processQuery($query->getQuery());
break;

case "bool":
$boolResult = [];

Expand All @@ -230,10 +230,10 @@ protected function processQuery(QueryInterface $query)
$result["bool"] = $boolResult;

break;

case "match":
$result = [];

if ($operator = $query->getOperator()) {
$result["match"][$query->getField()] = [
"query" => $query->getQuery(),
Expand All @@ -243,16 +243,16 @@ protected function processQuery(QueryInterface $query)
$result["match"][$query->getField()] = $query->getQuery();
}
break;

case "range":
$result = [];
$result["range"][$query->getField()] = $query->getRanges();
break;

case "sort":
$result = [];
foreach ($query->getCriteria() as $column => $order) {
$result[$column]["order"] = $order;
foreach ($query->getCriteria() as $sorting) {
$result[] = $sorting;
}
break;

Expand All @@ -261,7 +261,7 @@ protected function processQuery(QueryInterface $query)
$query->getField() => $query->getTerms()
];
break;

case "prefix":
$result["prefix"] = [
$query->getField() => $query->getQuery()
Expand All @@ -279,18 +279,18 @@ protected function processQuery(QueryInterface $query)
$query->getField() => $query->getQuery()
];
break;

case "constant_score":
$result["constant_score"] = $this->processQuery($query->getFilter());
break;

case "nested":
$result["nested"] = [
"path" => $query->getPath(),
"query" => $this->processQuery($query->getQuery())
];
break;

default:
throw new \InvalidArgumentException(sprintf(
"Unknown query type '%s' given.",
Expand Down
25 changes: 18 additions & 7 deletions lib/Byng/Pimcore/Elasticsearch/Query/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,44 @@ public function __construct()
/**
* Add a sorting criteria. Can be called multiple times to sort by more than
* one column.
*
*
* @param string $column
* @param string $order
* @param mixed $unmappedType
*/
public function addCriteria($column, $order)
public function addCriteria($column, $order, $unmappedType = false)
{
$this->data[$column] = $order;
$sort = [
"order" => $order
];

if ($unmappedType) {
$sort["unmapped_type"] = $unmappedType;
}

$this->data[] = [
$column => $sort
];
}

/**
* Get sort criteria
*
*
* @return array
*/
public function getCriteria()
{
return $this->data;
}

/**
* Resets the previously added criteria
*/
public function clear()
{
$this->data = [];
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 32d2949

Please sign in to comment.