Skip to content

Commit bf22933

Browse files
committed
fix(ElasticDSL): Small fixes in Elastic DSL types
1 parent 61633a6 commit bf22933

File tree

11 files changed

+77
-28
lines changed

11 files changed

+77
-28
lines changed

src/elasticDSL/Aggs/AggRules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { getValueCountITC } from './Metrics/ValueCount';
2121

2222
import { getChildrenITC } from './Bucket/Children';
2323
import { getDateHistogramITC } from './Bucket/DateHistogram';
24-
import { getDateRangeITC } from './Bucket/DateRange';
24+
import { getAggsDateRangeITC } from './Bucket/DateRange';
2525
import { getDiversifiedSamplerITC } from './Bucket/DiversifiedSampler';
2626
import { getFilterITC } from './Bucket/Filter';
2727
import { getFiltersITC } from './Bucket/Filters';
@@ -86,7 +86,7 @@ export function getAggRulesITC(opts: mixed = {}): InputTypeComposer {
8686

8787
children: () => getChildrenITC(opts),
8888
date_histogram: () => getDateHistogramITC(opts),
89-
date_range: () => getDateRangeITC(opts),
89+
date_range: () => getAggsDateRangeITC(opts),
9090
diversified_sampler: () => getDiversifiedSamplerITC(opts),
9191
filter: () => getFilterITC(opts),
9292
filters: () => getFiltersITC(opts),

src/elasticDSL/Aggs/Bucket/DateRange.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import { InputTypeComposer } from 'graphql-compose';
44
import { getTypeName, getOrSetType, desc } from '../../../utils';
5-
import { getDateFormatFC, getDateTimeZoneFC } from '../../Commons/Date';
5+
import {
6+
getDateFormatFC,
7+
getDateTimeZoneFC,
8+
getDateRangeITC,
9+
} from '../../Commons/Date';
610
import { getDateFields } from '../../Commons/FieldNames';
711

8-
export function getDateRangeITC(opts: mixed = {}): InputTypeComposer {
12+
export function getAggsDateRangeITC(opts: mixed = {}): InputTypeComposer {
913
const name = getTypeName('AggsDateRange', opts);
1014
const description = desc(
1115
`

src/elasticDSL/Aggs/Bucket/Terms.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { InputTypeComposer } from 'graphql-compose';
44
import { getTypeName, getOrSetType, desc } from '../../../utils';
55
import { getCommonsScriptITC } from '../../Commons/Script';
6+
import { getTermFields } from '../../Commons/FieldNames';
67

78
export function getTermsITC(opts: mixed = {}): InputTypeComposer {
89
const name = getTypeName('AggsTerms', opts);
@@ -20,7 +21,7 @@ export function getTermsITC(opts: mixed = {}): InputTypeComposer {
2021
name,
2122
description,
2223
fields: {
23-
field: 'String',
24+
field: () => getTermFields(opts),
2425
size: {
2526
type: 'Int',
2627
defaultValue: 10,

src/elasticDSL/Aggs/__tests__/__snapshots__/Aggs-test.js.snap

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ input Elastic_AggsDateRange_50 {
171171
172172
# Date Format/Patter. Eg \\"MM-yyy\\" returns \\"08-2012\\". [JodaDate](http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html)
173173
format: String
174-
ranges: [Elastic_AggsDateRange_50]
174+
ranges: [Elastic_DateRange_50]
175175
176176
# Time zones may either be specified as an ISO 8601 UTC offset (e.g. +01:00 or
177177
# -08:00) or as one of the time zone ids from the [TZ
@@ -546,6 +546,21 @@ input Elastic_CommonsScript_50 {
546546
file: String
547547
}
548548
549+
# Date range where \`from\` value includes and \`to\` value excludes.
550+
input Elastic_DateRange_50 {
551+
# The expression starts with an anchor date, which can either be now, or a date
552+
# string ending with ||. Eg \`2015-01-01||+1M/d\` means 2015-01-01 plus one month,
553+
# rounded down to the nearest day. Or \`now+1h+1m\`. The supported time units: y,
554+
# M, w, d, h, m, s
555+
from: String
556+
557+
# The expression starts with an anchor date, which can either be now, or a date
558+
# string ending with ||. Eg \`2015-01-01||+1M/d\` means 2015-01-01 plus one month,
559+
# rounded down to the nearest day. Or \`now+1h+1m\`. The supported time units: y,
560+
# M, w, d, h, m, s
561+
to: String
562+
}
563+
549564
# Float range where \`from\` value includes and \`to\` value excludes.
550565
input Elastic_FloatRange_50 {
551566
from: Float
@@ -652,18 +667,18 @@ input Elastic_Query_50 {
652667
# boolean clauses, each clause with a typed occurrence. [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)
653668
input Elastic_QueryBool_50 {
654669
# The clause (query) must appear in matching documents and will contribute to the score.
655-
must: Elastic_Query_50
670+
must: [Elastic_Query_50]
656671
657672
# The clause (query) must appear in matching documents. However unlike must the
658673
# score of the query will be ignored. Filter clauses are executed in filter
659674
# context, meaning that scoring is ignored and clauses are considered for caching.
660-
filter: Elastic_Query_50
675+
filter: [Elastic_Query_50]
661676
662677
# The clause (query) should appear in the matching document. In a boolean query
663678
# with no must or filter clauses, one or more should clauses must match a
664679
# document. The minimum number of should clauses to match can be set using the
665680
# minimum_should_match parameter.
666-
should: Elastic_Query_50
681+
should: [Elastic_Query_50]
667682
668683
# The minimum number of should clauses to match.
669684
minimum_should_match: String
@@ -672,7 +687,7 @@ input Elastic_QueryBool_50 {
672687
# executed in filter context meaning that scoring is ignored and clauses are
673688
# considered for caching. Because scoring is ignored, a score of 0 for all
674689
# documents is returned.
675-
must_not: Elastic_Query_50
690+
must_not: [Elastic_Query_50]
676691
boost: Float
677692
}
678693

src/elasticDSL/Commons/FieldNames.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ export function getPercolatorFields(opts: mixed) {
8181
return getFieldNamesType(opts, ['percolator'], 'Percolator');
8282
}
8383

84+
export function getTermFields(opts: mixed) {
85+
return getFieldNamesType(
86+
opts,
87+
[
88+
'keyword',
89+
'date',
90+
'boolean',
91+
'ip',
92+
'byte',
93+
'short',
94+
'integer',
95+
'long',
96+
'double',
97+
'float',
98+
'half_float',
99+
'scaled_float',
100+
'token_count',
101+
],
102+
'Term'
103+
);
104+
}
105+
84106
export function getAllFields(opts: mixed) {
85107
return getFieldNamesType(opts, ['_all'], 'All');
86108
}
@@ -156,7 +178,7 @@ function getEnumValues(
156178
}
157179
getFieldNamesByElasticType(fieldMap, types).forEach(fieldName => {
158180
values[fieldName] = {
159-
value: fieldName.replace('__', '.'),
181+
value: fieldName.replace(/__/g, '.'),
160182
};
161183
});
162184
return values;

src/elasticDSL/Query/Compound/Bool.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
2323
description,
2424
fields: {
2525
must: {
26-
type: () => getQueryITC(opts),
26+
type: () => [getQueryITC(opts)],
2727
description: desc(
2828
`
2929
The clause (query) must appear in matching documents
@@ -32,7 +32,7 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
3232
),
3333
},
3434
filter: {
35-
type: () => getQueryITC(opts),
35+
type: () => [getQueryITC(opts)],
3636
description: desc(
3737
`
3838
The clause (query) must appear in matching documents.
@@ -43,7 +43,7 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
4343
),
4444
},
4545
should: {
46-
type: () => getQueryITC(opts),
46+
type: () => [getQueryITC(opts)],
4747
description: desc(
4848
`
4949
The clause (query) should appear in the matching document.
@@ -63,7 +63,7 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
6363
),
6464
},
6565
must_not: {
66-
type: () => getQueryITC(opts),
66+
type: () => [getQueryITC(opts)],
6767
description: desc(
6868
`
6969
The clause (query) must not appear in the matching documents.
@@ -79,22 +79,29 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
7979
}));
8080
}
8181

82+
function prepareQueryMayBeArray(vals, fieldMap) {
83+
if (Array.isArray(vals)) {
84+
return vals.map(val => prepareQueryInResolve(val, fieldMap));
85+
}
86+
return prepareQueryInResolve(vals, fieldMap);
87+
}
88+
8289
export function prepareBoolInResolve(
8390
bool: any,
8491
fieldMap: mixed
8592
): { [argName: string]: any } {
8693
/* eslint-disable no-param-reassign */
8794
if (bool.must) {
88-
bool.must = prepareQueryInResolve(bool.must, fieldMap);
95+
bool.must = prepareQueryMayBeArray(bool.must, fieldMap);
8996
}
9097
if (bool.filter) {
91-
bool.filter = prepareQueryInResolve(bool.filter, fieldMap);
98+
bool.filter = prepareQueryMayBeArray(bool.filter, fieldMap);
9299
}
93100
if (bool.should) {
94-
bool.should = prepareQueryInResolve(bool.should, fieldMap);
101+
bool.should = prepareQueryMayBeArray(bool.should, fieldMap);
95102
}
96103
if (bool.must_not) {
97-
bool.must_not = prepareQueryInResolve(bool.must_not, fieldMap);
104+
bool.must_not = prepareQueryMayBeArray(bool.must_not, fieldMap);
98105
}
99106

100107
return bool;

src/elasticDSL/Query/FullText/Match.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { InputTypeComposer } from 'graphql-compose';
44
import { getTypeName, getOrSetType, desc } from '../../../utils';
5-
import { getAllAsFieldConfigMap } from '../../Commons/FieldNames';
5+
import { getAnalyzedAsFieldConfigMap } from '../../Commons/FieldNames';
66

77
export function getMatchITC(opts: mixed = {}): InputTypeComposer {
88
const name = getTypeName('QueryMatch', opts);
@@ -14,7 +14,7 @@ export function getMatchITC(opts: mixed = {}): InputTypeComposer {
1414
);
1515

1616
const subName = getTypeName('QueryMatchSettings', opts);
17-
const fields = getAllAsFieldConfigMap(
17+
const fields = getAnalyzedAsFieldConfigMap(
1818
opts,
1919
getOrSetType(subName, () =>
2020
// $FlowFixMe

src/elasticDSL/Query/Query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function prepareQueryInResolve(
215215
export function renameUndescoredToDots(obj: any, fieldMap: mixed) {
216216
const result = {};
217217
Object.keys(obj).forEach(o => {
218-
result[o.replace('__', '.')] = obj[o];
218+
result[o.replace(/__/g, '.')] = obj[o];
219219
});
220220
return result;
221221
}

src/elasticDSL/Query/TermLevel/Terms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function getTermsITC(opts: mixed = {}): InputTypeComposer {
1414
`
1515
);
1616

17-
const fields = getAllAsFieldConfigMap(opts, 'JSON');
17+
const fields = getAllAsFieldConfigMap(opts, '[JSON]');
1818

1919
if (typeof fields === 'object') {
2020
return getOrSetType(name, () =>

src/elasticDSL/Query/__tests__/__snapshots__/Query-test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ input Elastic_Query_50 {
9797
# boolean clauses, each clause with a typed occurrence. [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)
9898
input Elastic_QueryBool_50 {
9999
# The clause (query) must appear in matching documents and will contribute to the score.
100-
must: Elastic_Query_50
100+
must: [Elastic_Query_50]
101101
102102
# The clause (query) must appear in matching documents. However unlike must the
103103
# score of the query will be ignored. Filter clauses are executed in filter
104104
# context, meaning that scoring is ignored and clauses are considered for caching.
105-
filter: Elastic_Query_50
105+
filter: [Elastic_Query_50]
106106
107107
# The clause (query) should appear in the matching document. In a boolean query
108108
# with no must or filter clauses, one or more should clauses must match a
109109
# document. The minimum number of should clauses to match can be set using the
110110
# minimum_should_match parameter.
111-
should: Elastic_Query_50
111+
should: [Elastic_Query_50]
112112
113113
# The minimum number of should clauses to match.
114114
minimum_should_match: String
@@ -117,7 +117,7 @@ input Elastic_QueryBool_50 {
117117
# executed in filter context meaning that scoring is ignored and clauses are
118118
# considered for caching. Because scoring is ignored, a score of 0 for all
119119
# documents is returned.
120-
must_not: Elastic_Query_50
120+
must_not: [Elastic_Query_50]
121121
boost: Float
122122
}
123123

src/elasticDSL/Sort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function getSortITC(opts: any = {}): InputTypeComposer | string {
4444
},
4545
};
4646
sortableFields.forEach(fieldName => {
47-
const dottedName = fieldName.replace('__', '.');
47+
const dottedName = fieldName.replace(/__/g, '.');
4848
values[`${fieldName}__asc`] = {
4949
value: { [dottedName]: 'asc' },
5050
};

0 commit comments

Comments
 (0)