Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 556219d

Browse files
author
Reza Shadman
committed
Merge branch 'develop' of github.com:QuincePHP/Pelastic into develop
Conflicts: src/SqlMonkey/QueryFactory.php
2 parents 5ec1fde + 15fc0c8 commit 556219d

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/SqlMonkey/QueryFactory.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

0 commit comments

Comments
 (0)