/
helpers.ts
214 lines (185 loc) · 6.07 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {
AnyObject,
parseGeoPoint,
startsWith,
isGeoJSON,
isGeoShapePolygon,
isGeoShapeMultiPolygon
} from '@terascope/utils';
import {
GeoPointInput,
xLuceneTypeConfig,
xLuceneVariables,
xLuceneFieldType,
GeoShapeRelation
} from '@terascope/types';
import { isGeoShapePoint } from '../validations/field-validator';
// TODO: move these
export type JoinBy = 'AND'|'OR';
export interface xLuceneQueryResult {
query: string;
variables: xLuceneVariables;
}
export type CreateJoinQueryOptions = {
typeConfig?: xLuceneTypeConfig;
fieldParams?: Record<string, string>;
joinBy?: JoinBy;
variables?: AnyObject;
};
function isGeoQuery(type: xLuceneFieldType) {
return isGeoPointType(type) || isGeoJSONType(type);
}
function isGeoPointType(type: xLuceneFieldType) {
return type === xLuceneFieldType.Geo || type === xLuceneFieldType.GeoPoint;
}
function isGeoJSONType(type: xLuceneFieldType) {
return type === xLuceneFieldType.GeoJSON;
}
const relationList = Object.values(GeoShapeRelation);
function makeXluceneGeoDistanceQuery(
variableState: VariableState,
field: string,
value: GeoPointInput,
fieldParam?: string
) {
const dValue = fieldParam || '100m';
const results = parseGeoPoint(value, false);
if (!results) return '';
const vDistance = variableState.createVariable('distance', dValue);
const vPoint = variableState.createVariable('point', `${results.lat},${results.lon}`);
return `${field}:geoDistance(point: ${vPoint}, distance: ${vDistance})`;
}
function makeXlucenePolyContainsPoint(
variableState: VariableState,
field: string,
value: GeoPointInput
) {
const results = parseGeoPoint(value, false);
if (!results) return '';
const vPoint = variableState.createVariable('point', `${results.lat},${results.lon}`);
return `${field}:geoContainsPoint(point: ${vPoint})`;
}
function geoPolyQuery(field: string, polyVariableName: string, vParam?: string) {
if (vParam) {
return `${field}:geoPolygon(points: ${polyVariableName}, relation: ${vParam})`;
}
return `${field}:geoPolygon(points: ${polyVariableName})`;
}
function makeXlucenePolyQuery(
variableState: VariableState,
field: string,
polyVariableName: string,
fieldParam?: string
) {
let vParam: string | undefined;
if (fieldParam && relationList.includes(fieldParam as GeoShapeRelation)) {
vParam = variableState.createVariable('relation', fieldParam) as string;
}
return geoPolyQuery(field, polyVariableName, vParam);
}
function createGeoQuery(
variableState: VariableState,
field: string,
value: any,
targetType: xLuceneFieldType,
fieldParam?: string
) {
if (isGeoJSON(value)) {
if (isGeoShapePolygon(value) || isGeoShapeMultiPolygon(value)) {
// geoPolygon internally takes care of poly/multi-polygon.
// need to preserve original poly in variables
const polyVariableName = variableState.createVariable('points', value);
return makeXlucenePolyQuery(variableState, field, polyVariableName, fieldParam);
}
if (isGeoShapePoint(value)) {
if (isGeoPointType(targetType)) {
return makeXluceneGeoDistanceQuery(
variableState,
field,
value.coordinates,
fieldParam
);
}
return makeXlucenePolyContainsPoint(variableState, field, value.coordinates);
}
// We do not support any other geoJSON types;
return '';
}
// incoming value is a geo-point and we compare to another geo-point by geoDistance query
if (isGeoPointType(targetType)) {
return makeXluceneGeoDistanceQuery(variableState, field, value, fieldParam);
}
if (isGeoJSONType(targetType)) return makeXlucenePolyContainsPoint(variableState, field, value);
// if here then return a noop
return '';
}
export function toXluceneQuery(
input: AnyObject,
options: CreateJoinQueryOptions = {}
): xLuceneQueryResult {
const {
fieldParams = {},
joinBy = 'AND',
typeConfig = {},
variables
} = options;
const variableState = new VariableState(variables);
const query = Object.entries(input)
.map(([field, val]) => {
if (val == null) return '';
const config = typeConfig[field];
if (isGeoQuery(config)) {
return createGeoQuery(
variableState,
field,
val,
config,
fieldParams[field]
);
}
const value = variableState.createVariable(field, val);
return `${field}: ${value}`;
})
.filter(Boolean)
.join(` ${joinBy} `)
.trim();
const finalVariables = variableState.getVariables();
return { query, variables: finalVariables };
}
export class VariableState {
private variables: AnyObject;
constructor(variables?: AnyObject) {
this.variables = { ...variables };
}
private _makeKey(field: string) {
let num = 1;
let name = `${field}_${num}`;
while (this.variables[name] !== undefined) {
num += 1;
name = `${field}_${num}`;
}
return name;
}
createVariable(field: string, value: unknown): string {
if (typeof value === 'string' && startsWith(value, '$')) {
const vField = value.slice(1);
if (this.variables[vField] === undefined) {
throw new Error(`Must provide variable "${vField}" in the variables config`);
}
return value;
}
const key = this._makeKey(field);
this.variables[key] = value;
return `$${key}`;
}
/**
* Shallow clones and sorts the keys
*/
getVariables(): AnyObject {
const result: AnyObject = {};
for (const key of Object.keys(this.variables).sort()) {
result[key] = this.variables[key];
}
return result;
}
}