Skip to content

Commit 8b8889e

Browse files
committed
fix(QueryDSL): Beautifying and small refactoring
1 parent 9cc9e57 commit 8b8889e

File tree

9 files changed

+218
-86
lines changed

9 files changed

+218
-86
lines changed

src/ElasticDSL/Query/Bool.js

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,70 @@
22

33
import { InputTypeComposer } from 'graphql-compose';
44
import { getQueryITC } from './Query';
5-
import { getTypeName, getOrSetType } from "../../utils";
5+
import { getTypeName, getOrSetType, desc } from "../../utils";
66

7-
export function getBoolITC(opts = {}): InputTypeComposer {
8-
const typeName = getTypeName('QueryBool', opts);
7+
export function getBoolITC(opts: mixed = {}): InputTypeComposer {
8+
const name = getTypeName('QueryBool', opts);
9+
const description = desc(`
10+
A query that matches documents matching boolean combinations
11+
of other queries. The bool query maps to Lucene BooleanQuery.
12+
It is built using one or more boolean clauses, each clause
13+
with a typed occurrence.
14+
`);
915

10-
return getOrSetType(typeName, () => {
11-
const BoolITC = InputTypeComposer.create(typeName);
12-
BoolITC.setDescription('A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence. ');
13-
BoolITC.setFields({
14-
must: {
15-
type: () => getQueryITC(opts),
16-
description: 'The clause (query) must appear in matching documents and will contribute to the score.',
16+
return getOrSetType(name, () =>
17+
// $FlowFixMe
18+
InputTypeComposer.create({
19+
name,
20+
description,
21+
fields: {
22+
must: {
23+
type: () => getQueryITC(opts),
24+
description: desc(`
25+
The clause (query) must appear in matching documents
26+
and will contribute to the score.
27+
`),
28+
},
29+
filter: {
30+
type: () => getQueryITC(opts),
31+
description: desc(`
32+
The clause (query) must appear in matching documents.
33+
However unlike must the score of the query will be ignored.
34+
Filter clauses are executed in filter context, meaning
35+
that scoring is ignored and clauses are considered for caching.
36+
`),
37+
},
38+
should: {
39+
type: () => getQueryITC(opts),
40+
description: desc(`
41+
The clause (query) should appear in the matching document.
42+
In a boolean query with no must or filter clauses,
43+
one or more should clauses must match a document.
44+
The minimum number of should clauses to match can be set
45+
using the minimum_should_match parameter.
46+
`),
47+
},
48+
minimum_should_match: {
49+
type: 'Int',
50+
description: desc(`
51+
The minimum number of should clauses to match can be set using
52+
the minimum_should_match parameter.
53+
`),
54+
},
55+
must_not: {
56+
type: () => getQueryITC(opts),
57+
description: desc(`
58+
The clause (query) must not appear in the matching documents.
59+
Clauses are executed in filter context meaning that scoring
60+
is ignored and clauses are considered for caching.
61+
Because scoring is ignored, a score of 0 for all documents
62+
is returned.
63+
`),
64+
},
65+
boost: {
66+
type: 'Float',
67+
},
1768
},
18-
filter: {
19-
type: () => getQueryITC(opts),
20-
description: 'The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.',
21-
},
22-
should: {
23-
type: () => getQueryITC(opts),
24-
description: 'The clause (query) should appear in the matching document. In a boolean query with no must or filter clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.',
25-
},
26-
minimum_should_match: {
27-
type: 'Int',
28-
description: 'The minimum number of should clauses to match can be set using the minimum_should_match parameter.',
29-
},
30-
must_not: {
31-
type: () => getQueryITC(opts),
32-
description: 'The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.',
33-
},
34-
boost: {
35-
type: 'Float',
36-
},
37-
});
38-
39-
return BoolITC;
40-
});
69+
})
70+
);
4171
}

src/ElasticDSL/Query/Match.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* @flow */
2+
3+
import { InputTypeComposer } from 'graphql-compose';
4+
import { getTypeName, getOrSetType, desc } from '../../utils';
5+
6+
export function getMatchITC(opts: mixed = {}): InputTypeComposer {
7+
const name = getTypeName('QueryTerm', opts);
8+
const description = desc(`
9+
Match Query accept text/numerics/dates, analyzes them, and constructs a query.
10+
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)
11+
`);
12+
13+
if (false) {
14+
return getOrSetType(name, () =>
15+
InputTypeComposer.create({
16+
name,
17+
description,
18+
fields: {},
19+
}));
20+
}
21+
22+
// $FlowFixMe
23+
return {
24+
type: 'JSON',
25+
description,
26+
};
27+
}

src/ElasticDSL/Query/MatchAll.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
/* @flow */
22

33
import { InputTypeComposer } from 'graphql-compose';
4-
import { getTypeName, getOrSetType } from '../../utils';
4+
import { getTypeName, getOrSetType, desc } from '../../utils';
55

6-
export function getMatchAllITC(opts = {}): InputTypeComposer {
7-
const typeName = getTypeName('QueryMatchAll', opts);
6+
export function getMatchAllITC(opts: mixed = {}): InputTypeComposer {
7+
const name = getTypeName('QueryMatchAll', opts);
8+
const description = desc(`
9+
The most simple query, which matches all documents,
10+
giving them all a _score of 1.0.
11+
`);
812

9-
return getOrSetType(typeName, () => {
10-
const MatchAllITC = InputTypeComposer.create(typeName);
11-
MatchAllITC.setDescription('The most simple query, which matches all documents, giving them all a _score of 1.0.');
12-
MatchAllITC.setFields({
13-
boost: {
14-
type: 'Float',
13+
return getOrSetType(name, () =>
14+
// $FlowFixMe
15+
InputTypeComposer.create({
16+
name,
17+
description,
18+
fields: {
19+
boost: {
20+
type: 'Float',
21+
},
1522
},
16-
});
17-
18-
return MatchAllITC;
19-
});
23+
})
24+
);
2025
}

src/ElasticDSL/Query/Query.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
import { InputTypeComposer } from 'graphql-compose';
44
import { getBoolITC } from './Bool';
55
import { getMatchAllITC } from './MatchAll';
6-
import { getTypeName, getOrSetType } from '../../utils';
6+
import { getTermITC } from './Term';
7+
import { getTermsITC } from './Terms';
8+
import { getMatchITC } from './Match';
9+
import { getTypeName, getOrSetType, desc } from '../../utils';
710

8-
export function getQueryITC(opts = {}): InputTypeComposer {
9-
const typeName = getTypeName('Query', opts);
11+
export function getQueryITC(opts: mixed = {}): InputTypeComposer {
12+
const name = getTypeName('Query', opts);
13+
const description = desc(`
14+
Elasticsearch provides a full Query DSL based on JSON to define queries.
15+
[Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html)
16+
`);
1017

11-
return getOrSetType(typeName, () => {
12-
const QueryITC = InputTypeComposer.create(typeName);
13-
QueryITC.setFields({
14-
bool: () => getBoolITC(opts),
15-
match_all: () => getMatchAllITC(opts),
16-
term: {
17-
type: 'JSON',
18-
description: 'The term query finds documents that contain the exact term specified in the inverted index. { fieldName: value } or { fieldName: { value: value, boost: 2.0 } }',
18+
return getOrSetType(name, () =>
19+
// $FlowFixMe
20+
InputTypeComposer.create({
21+
name,
22+
description,
23+
fields: {
24+
bool: () => getBoolITC(opts),
25+
match_all: () => getMatchAllITC(opts),
26+
term: () => getTermITC(opts),
27+
terms: () => getTermsITC(opts),
28+
match: () => getMatchITC(opts),
1929
},
20-
terms: {
21-
type: 'JSON',
22-
description: 'Filters documents that have fields that match any of the provided terms (not analyzed). { fieldName: [values] }',
23-
},
24-
match: {
25-
type: 'JSON',
26-
description: '',
27-
},
28-
});
29-
30-
return QueryITC;
31-
});
30+
})
31+
);
3232
}

src/ElasticDSL/Query/Term.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* @flow */
2+
3+
import { InputTypeComposer } from 'graphql-compose';
4+
import { getTypeName, getOrSetType, desc } from '../../utils';
5+
6+
export function getTermITC(opts: mixed = {}): InputTypeComposer {
7+
const name = getTypeName('QueryTerm', opts);
8+
const description = desc(`
9+
Find documents which contain the exact term specified
10+
in the field specified.
11+
{ fieldName: value } or { fieldName: { value: value, boost: 2.0 } }
12+
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html)
13+
`);
14+
15+
if (false) {
16+
return getOrSetType(name, () =>
17+
InputTypeComposer.create({
18+
name,
19+
description,
20+
fields: {},
21+
}));
22+
}
23+
24+
// $FlowFixMe
25+
return {
26+
type: 'JSON',
27+
description,
28+
};
29+
}

src/ElasticDSL/Query/Terms.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* @flow */
2+
3+
import { InputTypeComposer } from 'graphql-compose';
4+
import { getTypeName, getOrSetType, desc } from '../../utils';
5+
6+
export function getTermsITC(opts: mixed = {}): InputTypeComposer {
7+
const name = getTypeName('QueryTerms', opts);
8+
const description = desc(`
9+
Filters documents that have fields that match any of
10+
the provided terms (not analyzed). { fieldName: [values] }
11+
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html)
12+
`);
13+
14+
if (false) {
15+
return getOrSetType(name, () =>
16+
InputTypeComposer.create({
17+
name,
18+
description,
19+
fields: {},
20+
}));
21+
}
22+
23+
// $FlowFixMe
24+
return {
25+
type: 'JSON',
26+
description,
27+
};
28+
}

src/ElasticDSL/SearchBody.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
import { InputTypeComposer } from 'graphql-compose';
44
import { getQueryITC } from './Query/Query';
5-
import { getTypeName, getOrSetType } from '../utils';
5+
import { getTypeName, getOrSetType, desc } from '../utils';
66

7-
export function getSearchBodyITC(opts = {}): InputTypeComposer {
8-
const typeName = getTypeName('SearchBody', opts);
7+
export function getSearchBodyITC(opts: mixed = {}): InputTypeComposer {
8+
const name = getTypeName('SearchBody', opts);
9+
const description = desc(`
10+
The search request can be executed with a search DSL, which includes
11+
the [Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html),
12+
within its body.
13+
`);
914

10-
return getOrSetType(typeName, () => {
11-
const SearchBodyITC = InputTypeComposer.create(typeName);
12-
SearchBodyITC.setDescription(
13-
'A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence. '
14-
);
15-
SearchBodyITC.setFields({
16-
query: () => getQueryITC(opts),
17-
});
18-
19-
return SearchBodyITC;
20-
});
15+
return getOrSetType(name, () =>
16+
// $FlowFixMe
17+
InputTypeComposer.create({
18+
name,
19+
description,
20+
fields: {
21+
query: () => getQueryITC(opts),
22+
},
23+
}));
2124
}

src/typeStorage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* @flow */
2+
23
import { isFunction } from 'graphql-compose';
34

45
class TypeStorage extends Map {

src/utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
/* @flow */
2+
13
import typeStorage from './typeStorage';
24

35
export function getTypeName(name: string, opts: any): string {
4-
return `${opts && opts.prefix || 'Elastic'}${name}${opts && opts.postfix || ''}`;
6+
return `${(opts && opts.prefix) || 'Elastic'}${name}${(opts && opts.postfix) || ''}`;
7+
}
8+
9+
export function getOrSetType<T>(typeName: string, typeOrThunk: (() => T) | T): T {
10+
// $FlowFixMe
11+
const type: T = typeStorage.getOrSet(typeName, typeOrThunk);
12+
return type;
513
}
614

7-
export function getOrSetType<T>(typeName: string, typeOrThunk: T | () => T): T {
8-
return typeStorage.getOrSet(typeName, typeOrThunk);
15+
// Remove newline multiline in descriptions
16+
export function desc(str: string): string {
17+
return str.replace(/\n\s+/, ' ');
918
}

0 commit comments

Comments
 (0)