This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +86
-0
lines changed Expand file tree Collapse file tree 1 file changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php namespace Quince \Pelastic \SqlMonkey ;
2+
3+ use Quince \Pelastic \Exceptions \PelasticInvalidArgumentException ;
4+
5+ class QueryFactory extends \Quince \Pelastic \QueryFactory {
6+
7+ /**
8+ * Mapper for sql to elasticsearch queries
9+ *
10+ * @var array
11+ */
12+ private $ mapper = [
13+ '= ' => 'term ' ,
14+ 'like ' => 'wildcard ' ,
15+ 'in ' => 'terms ' ,
16+ 'not in ' => 'terms ' ,
17+ '<> ' => 'term ' ,
18+ '!= ' => 'term ' ,
19+ 'is null ' => 'exists ' ,
20+ 'is not null ' => 'exists ' ,
21+ 'between ' => 'range ' ,
22+ ];
23+
24+ /**
25+ * Operators with negative impact
26+ *
27+ * @var array
28+ */
29+ private $ negativeImpact = [
30+ '<> ' ,
31+ '!= ' ,
32+ 'not in ' ,
33+ 'is not null '
34+ ];
35+
36+ /**
37+ * Map sql queries to elasticsearch queries
38+ *
39+ * @param $sqlQueryCondition
40+ * @param array $args
41+ * @return \Quince\Pelastic\Contracts\Queries\QueryInterface
42+ */
43+ public function mapSqlToElastic ($ sqlQueryCondition , array $ args = [])
44+ {
45+ $ mapper = $ this ->getMapper ();
46+
47+ if (!isset ($ mapper [$ sqlQueryCondition ])) {
48+
49+ throw new PelasticInvalidArgumentException ("No sql-to-elasticsearch handler found for [ {$ sqlQueryCondition }]. " );
50+
51+ }
52+
53+ return $ this ->create ($ mapper [$ sqlQueryCondition ], $ args );
54+ }
55+
56+ /**
57+ * Given operator has negative impact
58+ *
59+ * @param $operator
60+ * @return bool
61+ */
62+ public function operatorIsNegative ($ operator )
63+ {
64+ return isset ($ this ->getNegativeImpact ()[$ operator ]);
65+ }
66+
67+ /**
68+ * Get sql mapper
69+ *
70+ * @return array
71+ */
72+ private function getMapper ()
73+ {
74+ return $ this ->mapper ;
75+ }
76+
77+ /**
78+ * Operators that have negative impact
79+ *
80+ * @return array
81+ */
82+ private function getNegativeImpact ()
83+ {
84+ return $ this ->negativeImpact ;
85+ }
86+ }
You can’t perform that action at this time.
0 commit comments