Skip to content
Snippets Groups Projects
Commit 9a1f1889 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas Committed by Martynas Sudintas
Browse files

introduced query aware trait, simplified query building

from now, if there is only one query set, there will be no more generated bool query. Also Query class from now on is absolutely not necessary. There is no BC breaks.
parent e420e65d
No related branches found
No related tags found
No related merge requests found
...@@ -15,12 +15,16 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface; ...@@ -15,12 +15,16 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
/** /**
* Class Bool. * Bool operator. Can be used for filters and queries.
*/ */
class Bool implements BuilderInterface class Bool implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
const MUST = 'must';
const MUST_NOT = 'must_not';
const SHOULD = 'should';
/** /**
* @var array * @var array
*/ */
...@@ -37,14 +41,20 @@ class Bool implements BuilderInterface ...@@ -37,14 +41,20 @@ class Bool implements BuilderInterface
} }
/** /**
* Add BuilderInterface object ot bool filter. * Add BuilderInterface object to bool operator.
* *
* @param BuilderInterface $bool * @param BuilderInterface $bool
* @param string $type * @param string $type
*
* @throws \UnexpectedValueException
*/ */
public function addToBool(BuilderInterface $bool, $type = 'must') public function addToBool(BuilderInterface $bool, $type = self::MUST)
{ {
$this->container[$type][] = $bool; if (in_array($type, [ self::MUST, self::MUST_NOT, self::SHOULD ])) {
$this->container[$type][] = $bool;
} else {
throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
}
} }
/** /**
......
...@@ -16,6 +16,8 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface; ...@@ -16,6 +16,8 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
/** /**
* Query class. * Query class.
*
* @deprecated will remove in 1.0 Query container now is implemented directly in search.
*/ */
class Query implements BuilderInterface class Query implements BuilderInterface
{ {
......
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\DSL\Query;
use ONGR\ElasticsearchBundle\DSL\Bool\Bool;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
/**
* Provides query container functionality to any class.
*/
trait QueryAwareTrait
{
/**
* @var BuilderInterface[]
*/
private $queries = [];
/**
* @var \ONGR\ElasticsearchBundle\DSL\Bool\Bool
*/
private $boolQuery;
/**
* @param BuilderInterface $query
* @param string $boolType
*
* @return $this
*/
public function addQuery(BuilderInterface $query, $boolType = Bool::MUST)
{
if ($boolType !== Bool::MUST || $this->boolQuery !== null) {
$this->getBoolQuery()->addToBool($query, $boolType);
} else {
$this->queries[$query->getType()] = $query;
}
return $this;
}
/**
* Returns Bool query. Creates new instance if there is not initiated.
*
* @return \ONGR\ElasticsearchBundle\DSL\Bool\Bool
*/
public function getBoolQuery()
{
if (!$this->boolQuery) {
$this->boolQuery = new Bool();
}
return $this->boolQuery;
}
/**
* @param array $params Example values:
* - minimum_should_match => 1
* - boost => 1.
*/
public function setBoolQueryParameters(array $params)
{
$this->getBoolQuery()->setParameters($params);
}
/**
* Checks if there is added specific query.
*
* @param string $key
* @param string $boolType
*
* @return bool
*/
public function hasQuery($key, $boolType = Bool::MUST)
{
return array_key_exists($key, $this->queries[$boolType]);
}
/**
* Removes specific query.
*
* @param string $key
*/
public function removeQuery($key)
{
if ($this->hasQuery($key)) {
unset($this->queries[$key]);
}
}
/**
* Completely resets query.
*/
public function destroyQuery()
{
$this->queries = [];
$this->boolQuery = null;
}
/**
* Return all queries.
*
* @return array
*/
public function getQueries()
{
return $this->queries;
}
/**
* Aggregates all queries to array.
*
* @return array
*/
public function processQueries()
{
if ($this->boolQuery || count($this->getQueries()) > 1) {
$bool = $this->getBoolQuery();
foreach ($this->getQueries() as $query) {
$bool->addToBool($query);
}
return ['query' => [$bool->getType() => $bool->toArray()]];
} elseif (count($this->getQueries()) == 1) {
$query = array_values($this->getQueries())[0];
return ['query' => [$query->getType() => $query->toArray()]];
}
return [];
}
}
...@@ -17,6 +17,7 @@ use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter; ...@@ -17,6 +17,7 @@ use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter;
use ONGR\ElasticsearchBundle\DSL\Highlight\Highlight; use ONGR\ElasticsearchBundle\DSL\Highlight\Highlight;
use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery; use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery;
use ONGR\ElasticsearchBundle\DSL\Query\Query; use ONGR\ElasticsearchBundle\DSL\Query\Query;
use ONGR\ElasticsearchBundle\DSL\Query\QueryAwareTrait;
use ONGR\ElasticsearchBundle\DSL\Sort\AbstractSort; use ONGR\ElasticsearchBundle\DSL\Sort\AbstractSort;
use ONGR\ElasticsearchBundle\DSL\Sort\Sorts; use ONGR\ElasticsearchBundle\DSL\Sort\Sorts;
use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester; use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester;
...@@ -26,12 +27,9 @@ use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester; ...@@ -26,12 +27,9 @@ use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester;
*/ */
class Search class Search
{ {
const SCROLL_DURATION = '5m'; use QueryAwareTrait;
/** const SCROLL_DURATION = '5m';
* @var Query
*/
private $query;
/** /**
* @var array * @var array
...@@ -439,44 +437,7 @@ class Search ...@@ -439,44 +437,7 @@ class Search
} }
/** /**
* @param BuilderInterface $query Query. * Returns query url parameters.
* @param string $boolType Possible boolType values:
* - must
* - must_not
* - should.
*
* @return $this
*/
public function addQuery(BuilderInterface $query, $boolType = 'must')
{
if ($this->query === null) {
$this->query = new Query;
}
$this->query->addQuery($query, $boolType);
return $this;
}
/**
* Unset the query.
*/
public function destroyQuery()
{
$this->query = null;
}
/**
* @param array $params Example values:
* - minimum_should_match => 1
* - boost => 1.
*/
public function setBoolQueryParameters(array $params)
{
$this->query->setBoolParameters($params);
}
/**
* Returns query string parameters.
* *
* @return array * @return array
*/ */
...@@ -571,14 +532,6 @@ class Search ...@@ -571,14 +532,6 @@ class Search
return $this->postFilters; return $this->postFilters;
} }
/**
* @return BuilderInterface[]
*/
public function getQueries()
{
return $this->query;
}
/** /**
* @return array * @return array
*/ */
...@@ -648,25 +601,18 @@ class Search ...@@ -648,25 +601,18 @@ class Search
*/ */
public function toArray() public function toArray()
{ {
$output = []; /*
* First we check if there are some filter to add to filtered query.
$query = $this->query; * For now we use match all, but its gonna be changed.
*/
if ($this->filters !== null) { if ($this->filters !== null) {
$filteredQuery = new FilteredQuery($query === null ? null : $query); $filteredQuery = new FilteredQuery();
$filteredQuery->setFilter($this->filters); $filteredQuery->setFilter($this->filters);
$this->addQuery($filteredQuery);
if ($this->boolFilterParams) {
$filteredQuery->setBoolParameters($this->boolFilterParams);
}
$query = new Query();
$query->addQuery($filteredQuery);
} }
if ($query !== null) { $output = $this->processQueries();
$output[$query->getType()] = $query->toArray();
}
if ($this->postFilters !== null) { if ($this->postFilters !== null) {
$postFilter = new PostFilter(); $postFilter = new PostFilter();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment