Skip to content

Commit

Permalink
feat(QueryDSL): Defined all InputTypes for search.body
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Mar 13, 2017
1 parent 8b8889e commit 444d64d
Show file tree
Hide file tree
Showing 39 changed files with 1,376 additions and 39 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"graphql-compose": "^1.14.0"
},
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-cli": "^6.24.0",
"babel-eslint": "^7.1.1",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
Expand All @@ -48,7 +48,7 @@
"express-graphql": "^0.6.3",
"flow-bin": "^0.41.0",
"graphql": "^0.9.1",
"graphql-compose": "^1.15.0",
"graphql-compose": "^1.17.1",
"jest": "^19.0.2",
"jest-babel": "^1.0.1",
"npm-run-all": "^4.0.1",
Expand All @@ -70,7 +70,7 @@
"build": "npm-run-all build:*",
"build:lib": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
"build:flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
"example": "nodemon -e js --ignore *test* --exec ./node_modules/.bin/babel-node ./examples/elastic50/index.js",
"dev": "nodemon -e js --ignore *test* --exec ./node_modules/.bin/babel-node ./examples/elastic50/index.js",
"coverage": "jest --coverage",
"lint": "eslint --ext .js ./src",
"test": "jest",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* @flow */

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

export function getBoolITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryBool', opts);
Expand All @@ -11,6 +11,7 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
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.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)
`);

return getOrSetType(name, () =>
Expand Down Expand Up @@ -62,9 +63,7 @@ export function getBoolITC(opts: mixed = {}): InputTypeComposer {
is returned.
`),
},
boost: {
type: 'Float',
},
boost: 'Float',
},
})
);
Expand Down
26 changes: 26 additions & 0 deletions src/ElasticDSL/Query/Compound/Boosting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */

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

export function getBoostingITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryBoosting', opts);
const description = desc(`
The boosting query can be used to effectively demote results that match a given query.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html)
`);

return getOrSetType(name, () =>
// $FlowFixMe
InputTypeComposer.create({
name,
description,
fields: {
positive: () => getQueryITC(opts),
negative: () => getQueryITC(opts),
negative_boost: 'Float',
},
})
);
}
26 changes: 26 additions & 0 deletions src/ElasticDSL/Query/Compound/ConstantScore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */

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

export function getConstantScoreITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryConstantScore', opts);
const description = desc(`
A query that wraps another query and simply returns a constant score equal
to the query boost for every document in the filter.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html)
`);

return getOrSetType(name, () =>
// $FlowFixMe
InputTypeComposer.create({
name,
description,
fields: {
filter: () => getQueryITC(opts).getTypeAsRequired(),
boost: 'Float!',
},
})
);
}
29 changes: 29 additions & 0 deletions src/ElasticDSL/Query/Compound/DisMax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* @flow */

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

export function getDisMaxITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryDisMax', opts);
const description = desc(`
A query that generates the union of documents produced by its subqueries,
and that scores each document with the maximum score for that document
as produced by any subquery, plus a tie breaking increment
for any additional matching subqueries.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html)
`);

return getOrSetType(name, () =>
// $FlowFixMe
InputTypeComposer.create({
name,
description,
fields: {
queries: () => [getQueryITC(opts)],
boost: 'Float',
tie_breaker: 'Float',
},
})
);
}
61 changes: 61 additions & 0 deletions src/ElasticDSL/Query/Compound/FunctionScore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* @flow */

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

export function getFunctionScoreITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryFunctionScore', opts);
const description = desc(`
The function_score allows you to modify the score of documents that
are retrieved by a query. This can be useful if, for example,
a score function is computationally expensive and it is sufficient
to compute the score on a filtered set of documents.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)
`);

// $FlowFixMe
const RandomScoreType = InputTypeComposer.create({
name: getTypeName('QueryFunctionScoreRandom', opts),
fields: {
seed: 'Float',
},
});

return getOrSetType(name, () =>
// $FlowFixMe
InputTypeComposer.create({
name,
description,
fields: {
query: () => getQueryITC(opts),
boost: 'String',
boost_mode: {
type: 'String',
description: 'Can be: `multiply`, `replace`, `sum`, `avg`, `max`, `min`.',
},
random_score: RandomScoreType,
// $FlowFixMe
functions: [InputTypeComposer.create({
name: getTypeName('QueryFunctionScoreFunction', opts),
fields: {
filter: () => getQueryITC(opts),
random_score: RandomScoreType,
weight: 'Float',
script_score: 'JSON',
field_value_factor: 'JSON',
gauss: 'JSON',
linear: 'JSON',
exp: 'JSON',
},
})],
max_boost: 'Float',
score_mode: {
type: 'String',
description: 'Can be: `multiply`, `sum`, `avg`, `first`, `max`, `min`.',
},
min_score: 'Float',
},
})
);
}
29 changes: 29 additions & 0 deletions src/ElasticDSL/Query/FullText/Common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* @flow */

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

export function getCommonITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryCommon', opts);
const description = desc(`
The common terms query is a modern alternative to stopwords which improves
the precision and recall of search results (by taking stopwords into account),
without sacrificing performance.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html)
`);

if (false) {
return getOrSetType(name, () =>
InputTypeComposer.create({
name,
description,
fields: {},
}));
}

// $FlowFixMe
return {
type: 'JSON',
description,
};
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* @flow */

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

export function getMatchITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryTerm', opts);
const description = desc(`
Match Query accept text/numerics/dates, analyzes them, and constructs a query.
Match Query accept text/numerics/dates, analyzes them, and constructs a query.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)
`);

Expand Down
28 changes: 28 additions & 0 deletions src/ElasticDSL/Query/FullText/MatchPhrase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @flow */

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

export function getMatchPhraseITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryMatchPhrase', opts);
const description = desc(`
The match_phrase query analyzes the text and creates a phrase query out
of the analyzed text.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html)
`);

if (false) {
return getOrSetType(name, () =>
InputTypeComposer.create({
name,
description,
fields: {},
}));
}

// $FlowFixMe
return {
type: 'JSON',
description,
};
}
28 changes: 28 additions & 0 deletions src/ElasticDSL/Query/FullText/MatchPhrasePrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @flow */

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

export function getMatchPhrasePrefixITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryMatchPhrasePrefix', opts);
const description = desc(`
The match_phrase_prefix is the same as match_phrase, except that it allows
for prefix matches on the last term in the text. Eg "quick brown f"
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html)
`);

if (false) {
return getOrSetType(name, () =>
InputTypeComposer.create({
name,
description,
fields: {},
}));
}

// $FlowFixMe
return {
type: 'JSON',
description,
};
}
42 changes: 42 additions & 0 deletions src/ElasticDSL/Query/FullText/MultiMatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* @flow */

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

export function getMultiMatchITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryMultiMatch', opts);
const description = desc(`
The multi_match query builds on the match query to allow multi-field queries.
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html)
`);

return getOrSetType(name, () =>
// $FlowFixMe
InputTypeComposer.create({
name,
description,
fields: {
query: 'String!',
fields: {
type: '[String]!',
description: desc(`
Array of fields [ "title", "*_name", "subject^3" ].
You may use wildcards and boosting field.
`),
},
type: `enum ${getTypeName('QueryMultiMatchTypeEnum', opts)} {
best_fields
most_fields
cross_fields
phrase
phrase_prefix
}`,
operator: `enum ${getTypeName('QueryMultiMatchOperatorEnum', opts)} {
and
or
}`,
minimum_should_match: 'String',
analyzer: 'String',
},
}));
}
47 changes: 47 additions & 0 deletions src/ElasticDSL/Query/FullText/QueryString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* @flow */

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

export function getQueryStringITC(opts: mixed = {}): InputTypeComposer {
const name = getTypeName('QueryQueryString', opts);
const description = desc(`
A query that uses a query parser in order to parse its content.
Eg. "this AND that OR thus" or "(content:this OR name:this) AND (content:that OR name:that)"
[Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)
`);

return getOrSetType(name, () =>
// $FlowFixMe
InputTypeComposer.create({
name,
description,
fields: {
query: 'String!',
fields: '[String]',
default_field: 'String',
default_operator: `enum ${getTypeName('QueryQueryStringOperatorEnum', opts)} {
and
or
}`,
analyzer: 'String',
allow_leading_wildcard: 'Boolean',
enable_position_increments: 'Boolean',
fuzzy_max_expansions: 'Int',
fuzziness: 'String',
fuzzy_prefix_length: 'Int',
phrase_slop: 'Int',
boost: 'Float',
auto_generate_phrase_queries: 'Boolean',
analyze_wildcard: 'Boolean',
max_determinized_states: 'Int',
minimum_should_match: 'String',
lenient: 'Boolean',
time_zone: 'String',
quote_field_suffix: 'String',
split_on_whitespace: 'Boolean',
use_dis_max: 'Boolean',
tie_breaker: 'Int',
},
}));
}
Loading

0 comments on commit 444d64d

Please sign in to comment.