From 752a57f1fe497c38ffe0f16effcb155fad553f32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Mon, 16 Mar 2015 15:01:41 +0200
Subject: [PATCH] DSL Span queries

---
 Query/Span/SpanFirstQuery.php     | 67 +++++++++++++++++++++++++++++
 Query/Span/SpanMultiTermQuery.php | 61 +++++++++++++++++++++++++++
 Query/Span/SpanNearQuery.php      | 70 +++++++++++++++++++++++++++++++
 Query/Span/SpanNotQuery.php       | 65 ++++++++++++++++++++++++++++
 Query/Span/SpanOrQuery.php        | 62 +++++++++++++++++++++++++++
 Query/Span/SpanQueryInterface.php | 21 ++++++++++
 Query/Span/SpanTermQuery.php      | 28 +++++++++++++
 Query/SpanTermQuery.php           | 69 ------------------------------
 8 files changed, 374 insertions(+), 69 deletions(-)
 create mode 100644 Query/Span/SpanFirstQuery.php
 create mode 100644 Query/Span/SpanMultiTermQuery.php
 create mode 100644 Query/Span/SpanNearQuery.php
 create mode 100644 Query/Span/SpanNotQuery.php
 create mode 100644 Query/Span/SpanOrQuery.php
 create mode 100644 Query/Span/SpanQueryInterface.php
 create mode 100644 Query/Span/SpanTermQuery.php
 delete mode 100644 Query/SpanTermQuery.php

diff --git a/Query/Span/SpanFirstQuery.php b/Query/Span/SpanFirstQuery.php
new file mode 100644
index 0000000..66a9ff8
--- /dev/null
+++ b/Query/Span/SpanFirstQuery.php
@@ -0,0 +1,67 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
+
+/**
+ * Elasticsearch span first query.
+ */
+class SpanFirstQuery implements SpanQueryInterface
+{
+    use ParametersTrait;
+
+    /**
+     * @var SpanQueryInterface
+     */
+    private $query;
+
+    /**
+     * @var int
+     */
+    private $end;
+
+    /**
+     * @param SpanQueryInterface $query
+     * @param int                $end
+     * @param array              $parameters
+     *
+     * @throws \LogicException
+     */
+    public function __construct(SpanQueryInterface $query, $end, array $parameters = [])
+    {
+        $this->query = $query;
+        $this->end = $end;
+        $this->setParameters($parameters);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'span_first';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        $query = [];
+        $query['match'] = [$this->query->getType() => $this->query->toArray()];
+        $query['end'] = $this->end;
+        $output = $this->processArray($query);
+
+        return $output;
+    }
+}
diff --git a/Query/Span/SpanMultiTermQuery.php b/Query/Span/SpanMultiTermQuery.php
new file mode 100644
index 0000000..ff47353
--- /dev/null
+++ b/Query/Span/SpanMultiTermQuery.php
@@ -0,0 +1,61 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
+use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
+
+/**
+ * Elasticsearch span multi term query.
+ */
+class SpanMultiTermQuery implements SpanQueryInterface
+{
+    use ParametersTrait;
+
+    /**
+     * @var BuilderInterface
+     */
+    private $query;
+
+    /**
+     * Accepts one of fuzzy, prefix, term range, wildcard, regexp query.
+     *
+     * @param BuilderInterface $query
+     * @param array            $parameters
+     */
+    public function __construct(BuilderInterface $query, array $parameters = [])
+    {
+        $this->query = $query;
+        $this->setParameters($parameters);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'span_multi';
+    }
+
+    /**
+     * {@inheritdoc}
+     *
+     * @throws \InvalidArgumentException
+     */
+    public function toArray()
+    {
+        $query['match'] = [$this->query->getType() => $this->query->toArray()];
+        $output = $this->processArray($query);
+
+        return $output;
+    }
+}
diff --git a/Query/Span/SpanNearQuery.php b/Query/Span/SpanNearQuery.php
new file mode 100644
index 0000000..4804764
--- /dev/null
+++ b/Query/Span/SpanNearQuery.php
@@ -0,0 +1,70 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
+
+/**
+ * Elasticsearch span near query.
+ */
+class SpanNearQuery implements SpanQueryInterface
+{
+    use ParametersTrait;
+
+    /**
+     * @var int
+     */
+    private $slop;
+
+    /**
+     * @var SpanQueryInterface[]
+     */
+    private $queries = [];
+
+    /**
+     * @param int                  $slop
+     * @param SpanQueryInterface[] $queries
+     * @param array                $parameters
+     *
+     * @throws \LogicException
+     */
+    public function __construct($slop, array $queries = [], array $parameters = [])
+    {
+        $this->slop = $slop;
+        $this->queries = $queries;
+        $this->setParameters($parameters);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'span_near';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        $query = [];
+        foreach ($this->queries as $type) {
+            $data = [$type->getType() => $type->toArray()];
+            $query['clauses'][] = $data;
+        }
+        $query['slop'] = $this->slop;
+        $output = $this->processArray($query);
+
+        return $output;
+    }
+}
diff --git a/Query/Span/SpanNotQuery.php b/Query/Span/SpanNotQuery.php
new file mode 100644
index 0000000..9ce85c0
--- /dev/null
+++ b/Query/Span/SpanNotQuery.php
@@ -0,0 +1,65 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
+
+/**
+ * Elasticsearch Span not query.
+ */
+class SpanNotQuery implements SpanQueryInterface
+{
+    use ParametersTrait;
+
+    /**
+     * @var SpanQueryInterface
+     */
+    private $include;
+
+    /**
+     * @var SpanQueryInterface
+     */
+    private $exclude;
+
+    /**
+     * @param SpanQueryInterface $include
+     * @param SpanQueryInterface $exclude
+     * @param array              $parameters
+     */
+    public function __construct(SpanQueryInterface $include, SpanQueryInterface $exclude, array $parameters = [])
+    {
+        $this->include = $include;
+        $this->exclude = $exclude;
+        $this->setParameters($parameters);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'span_not';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        $query = [
+            'include' => [$this->include->getType() => $this->include->toArray()],
+            'exclude' => [$this->exclude->getType() => $this->exclude->toArray()],
+        ];
+
+        return $query;
+    }
+}
diff --git a/Query/Span/SpanOrQuery.php b/Query/Span/SpanOrQuery.php
new file mode 100644
index 0000000..a18df02
--- /dev/null
+++ b/Query/Span/SpanOrQuery.php
@@ -0,0 +1,62 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
+
+/**
+ * Elasticsearch span or query.
+ */
+class SpanOrQuery implements SpanQueryInterface
+{
+    use ParametersTrait;
+
+    /**
+     * @var SpanQueryInterface[]
+     */
+    private $queries = [];
+
+    /**
+     * @param SpanQueryInterface[] $queries
+     * @param array                $parameters
+     */
+    public function __construct(array $queries = [], array $parameters = [])
+    {
+        foreach ($queries as $query) {
+            $this->queries[] = $query;
+        }
+        $this->setParameters($parameters);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'span_or';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        $query = [];
+        foreach ($this->queries as $type) {
+            $data = [$type->getType() => $type->toArray()];
+            $query['clauses'][] = $data;
+        }
+        $output = $this->processArray($query);
+
+        return $output;
+    }
+}
diff --git a/Query/Span/SpanQueryInterface.php b/Query/Span/SpanQueryInterface.php
new file mode 100644
index 0000000..45fdea9
--- /dev/null
+++ b/Query/Span/SpanQueryInterface.php
@@ -0,0 +1,21 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
+
+/**
+ * Interface SpanQueryInterface to recognise span queries.
+ */
+interface SpanQueryInterface extends BuilderInterface
+{
+}
diff --git a/Query/Span/SpanTermQuery.php b/Query/Span/SpanTermQuery.php
new file mode 100644
index 0000000..7ccde21
--- /dev/null
+++ b/Query/Span/SpanTermQuery.php
@@ -0,0 +1,28 @@
+<?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\Span;
+
+use ONGR\ElasticsearchBundle\DSL\Query\TermQuery;
+
+/**
+ * Elasticsearch span_term query class.
+ */
+class SpanTermQuery extends TermQuery implements SpanQueryInterface
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'span_term';
+    }
+}
diff --git a/Query/SpanTermQuery.php b/Query/SpanTermQuery.php
deleted file mode 100644
index d88d460..0000000
--- a/Query/SpanTermQuery.php
+++ /dev/null
@@ -1,69 +0,0 @@
-<?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\BuilderInterface;
-use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
-
-/**
- * Elasticsearch span_term query class.
- */
-class SpanTermQuery implements BuilderInterface
-{
-    use ParametersTrait;
-
-    /**
-     * @var string
-     */
-    private $field;
-
-    /**
-     * @var string
-     */
-    private $value;
-
-    /**
-     * @param string $field
-     * @param string $value
-     * @param array  $parameters
-     */
-    public function __construct($field, $value, array $parameters = [])
-    {
-        $this->field = $field;
-        $this->value = $value;
-        $this->setParameters($parameters);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function getType()
-    {
-        return 'span_term';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function toArray()
-    {
-        $query = [
-            'value' => $this->value,
-        ];
-
-        $output = [
-            $this->field => $this->processArray($query),
-        ];
-
-        return $output;
-    }
-}
-- 
GitLab