Skip to content

Commit ff95446

Browse files
committed
Implement proper patern match translation between PG and ES
1 parent 2499030 commit ff95446

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pg_es_fdw/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def _handle_aggregation_response(self, query, response, aggs, group_clauses):
321321
for agg_name in aggs:
322322
if agg_name == "count.*":
323323
# COUNT(*) is a special case, since it doesn't have a
324-
# corresponding aggregation primitice in ES
324+
# corresponding aggregation primitive in ES
325325
result[agg_name] = response["hits"]["total"]["value"]
326326
continue
327327

pg_es_fdw/_es_query.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
try:
23
from multicorn import ANY
34
except ImportError:
@@ -22,6 +23,20 @@
2223
}
2324

2425

26+
def _convert_pattern_match_to_es(expr):
27+
def _pg_es_pattern_map(matchobj):
28+
if matchobj.group(0) == "%":
29+
return "*"
30+
elif matchobj.group(0) == "_":
31+
return "?"
32+
elif matchobj.group(0) == "\%":
33+
return "%"
34+
elif matchobj.group(0) == "\_":
35+
return "_"
36+
37+
return re.sub(r'\\?%|\\?_', _pg_es_pattern_map, expr)
38+
39+
2540
def _base_qual_to_es(col, op, value, column_map=None):
2641
if column_map:
2742
col = column_map.get(col, col)
@@ -44,7 +59,7 @@ def _base_qual_to_es(col, op, value, column_map=None):
4459
return {"bool": {"must_not": {"term": {col: value}}}}
4560

4661
if op == "~~":
47-
return {"wildcard": {col: value.replace("%", "*")}}
62+
return {"wildcard": {col: _convert_pattern_match_to_es(value)}}
4863

4964
# For unknown operators, get everything
5065
return {"match_all": {}}

0 commit comments

Comments
 (0)