Skip to content

Commit

Permalink
Merge pull request #5 from Blynskyniki/text-search
Browse files Browse the repository at this point in the history
feat: publish elastic-dsl-typescript@1.0.5 and bug fixes
  • Loading branch information
Blynskyniki authored Nov 25, 2020
2 parents 0ef18d2 + 271b046 commit cfce18e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 40 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "elastic-dsl-typescript",
"version": "1.0.1",
"version": "1.0.5",
"description": "Node.js ElasticSearch search query builder",
"main": "./",
"scripts": {
"test": "./node_modules/.bin/jest -i --coverage --forceExit",
"copyData": "cp package.json ./dist/package.json && cp ./README.md ./dist/README.md && cp ./LICENSE ./dist/LICENSE && cp -r ./img ./dist/img ",
"copyData": "cp package.json ./dist/package.json && cp ./README.md ./dist/README.md && cp ./LICENSE ./dist/LICENSE && cp -r ./img ./dist/img && cp -r ./src ./dist/ ",
"build": "rm -rf ./dist && ./node_modules/.bin/tsc",
"prepublish": "npm run build && npm run copyData && cd ./dist "
"pub": "npm run build && npm run copyData && cd ./dist && npm publish"
},
"repository": {
"type": "git",
Expand Down
15 changes: 11 additions & 4 deletions src/Builders/Bool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BoolSchema } from './types';

type BoolQueryData = Partial<Record<BoolFields, object[]>>;

export class Bool<BASE_SCHEMA extends BoolSchema> extends AbstractBulder {
export class Bool<BASE_SCHEMA extends BoolSchema = BoolSchema> extends AbstractBulder {
private _query: BoolQueryData = {};

private checkField(type: BoolFields) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class Bool<BASE_SCHEMA extends BoolSchema> extends AbstractBulder {
* @return {boolean}
*/
public isNotEmty(): boolean {
return Object.keys(this._query).filter((field) => field.length).length > 0;
return Object.keys(this._query).filter(field => field.length).length > 0;
}

/**
Expand All @@ -64,9 +64,16 @@ export class Bool<BASE_SCHEMA extends BoolSchema> extends AbstractBulder {
this.checkField(type);

switch (filter) {
case 'exists': {
case "exists": {
this._query[type]?.push({
[filter]: { field: (data.params as BASE_SCHEMA['exists']['params']).fieldName },
[filter]: { field: (data.params as BASE_SCHEMA["exists"]["params"]).fieldName }
});
break;
}
case "term":
case "terms": {
this._query[type]?.push({
[filter]: { [data["field"]!]: (data.params as object)["value"], ...(data.opts as object) }
});
break;
}
Expand Down
33 changes: 30 additions & 3 deletions src/Builders/Bool/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
Relation,
TimeZone,
Transpositions,
Rewrite
Rewrite,
Analyzer,
AutoGenerateSynonymsPhraseQuery,
FuzzyTranspositions,
Lenient,
Operator,
MinimumShouldMatch,
ZeroTermsQuery
} from '../../Types/QueryOptions';

/**
Expand Down Expand Up @@ -39,17 +46,37 @@ export interface BoolSchema extends Schema {
fieldName: string;
};
};
match: {
field: string;
params: {
/**
* (Required) Text, number, boolean value or date you wish to find in the provided <field>.
*/
query: string | number | boolean;
};
opts?:
| Analyzer
| AutoGenerateSynonymsPhraseQuery
| Fuzziness
| MaxExpansions
| PrefixLength
| FuzzyTranspositions
| Lenient
| Operator
| MinimumShouldMatch
| ZeroTermsQuery;
};
term: {
field: string;
params: {
value: string;
value: string | number | boolean;
};
opts?: Boost;
};
terms: {
field: string;
params: {
value: string[];
value: string[] | number[] | boolean[];
};
opts?: Boost;
};
Expand Down
2 changes: 1 addition & 1 deletion src/Builders/Text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TEXT<BASE_SCHEMA extends TextSchema> extends AbstractBulder {
* Add conditions for Bool
* @param filter
* @param data
* @returns {Omit<TEXT<BASE_SCHEMA>, "add">}
* @returns {this<BASE_SCHEMA>}
*/
public add<K extends keyof BASE_SCHEMA>(filter: K, data: BASE_SCHEMA[K]): Omit<this, "add"> {
if (data.field) {
Expand Down
22 changes: 11 additions & 11 deletions src/Builders/Text/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ export interface TextSchema extends Schema {
query: string | Date | number;
};

opts?:
| Fuzziness
| MaxExpansions
| PrefixLength
| MinimumShouldMatch
| FuzzyTranspositions
| Lenient
| Operator
| Analyzer
| ZeroTermsQuery
| AutoGenerateSynonymsPhraseQuery;
opts?: Fuzziness &
MaxExpansions &
PrefixLength &
MinimumShouldMatch &
FuzzyTranspositions &
Lenient &
Operator &
Analyzer &
ZeroTermsQuery &
AutoGenerateSynonymsPhraseQuery;
};
match_phrase: {
field: string;
Expand Down Expand Up @@ -77,3 +76,4 @@ export interface TextSchema extends Schema {
| AnalyzeWildcard;
};
}

File renamed without changes.
5 changes: 2 additions & 3 deletions src/Query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export class Query<BOOL_SCHEMA extends BoolSchema> extends AbstractBulder {
const obj = {};
for (const [prop, val] of Object.entries(this._props)) {
switch (prop) {
case 'query': {
case "query": {
break;
}
case 'aggs': {
case "aggs": {
obj[prop] = (val as AbstractBulder).build();
break;
}
Expand All @@ -70,7 +70,6 @@ export class Query<BOOL_SCHEMA extends BoolSchema> extends AbstractBulder {
}
let query = {};
for (const [prop, val] of Object.entries(this._query)) {
console.log(prop);
if (val instanceof AbstractBulder) {
query = { ...query, ...(val as AbstractBulder).build() };
continue;
Expand Down
13 changes: 7 additions & 6 deletions src/__test__/Bool/BoolBulder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('BoolBulder tests', () => {
},
);


expect(b.build()).toHaveProperty(
'bool',
expect.objectContaining({
Expand Down Expand Up @@ -76,8 +77,8 @@ describe('BoolBulder tests', () => {
});

expect(b.build()).toHaveProperty(
'bool',
expect.objectContaining({ filter: [{ terms: { articul: { boost: 2, value: ['00001851'] } } }] }),
"bool",
expect.objectContaining({ filter: [{ terms: { articul: ["00001851"], boost: 2 } }] })
);
});

Expand Down Expand Up @@ -117,7 +118,7 @@ describe('BoolBulder tests', () => {
price: {
boost: 120,
gte: 0,
lte: 1.7976931348623157e308,
lte: 1.7976931348623157e308
},
},
},
Expand All @@ -130,14 +131,14 @@ describe('BoolBulder tests', () => {
}
}
}
],
]
},
});
});

test('Create mutlti query', async () => {
test("Create mutlti query", async () => {
const b = new Bool()
.add('must', 'fuzzy', {
.add("must", "fuzzy", {
field: "f",
params: {
value: "some text"
Expand Down
21 changes: 12 additions & 9 deletions src/__test__/Text/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@ import { TEXT } from '../../Builders/Text';
describe('TEXT tests', () => {
test('Create TEXT query ', async () => {
const t = new TEXT()

.add('match', {
field: 'testField',
params: {
query: 'search text'
query: "search text"
},
opts: {
analyzer: 'russian',
max_expansions: 1,
analyzer: "russian",
auto_generate_synonyms_phrase_query: true,
fuzziness: '6',
operator: 'AND',
fuzziness: "6",
operator: "AND",
prefix_length: 15,
fuzzy_transpositions: true
}
})

.build();

expect(t).toHaveProperty(
'match',
"match",
expect.objectContaining({
testField: {
analyzer: 'russian',
analyzer: "russian",
auto_generate_synonyms_phrase_query: true,
fuzziness: '6',
fuzziness: "6",
fuzzy_transpositions: true,
operator: 'AND',
operator: "AND",
prefix_length: 15,
query: 'search text'
query: "search text"
}
})
);
Expand Down

0 comments on commit cfce18e

Please sign in to comment.