sequelastic
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -25,3 +25,3 @@ import "colors"; | ||
fuzzy?: boolean; | ||
fuzziness: "AUTO" | number; | ||
fuzziness?: "AUTO" | number; | ||
wholeResponse?: false; | ||
@@ -31,2 +31,13 @@ from?: number; | ||
}; | ||
declare type SequelasticMultipleSearchIndex = { | ||
index: string; | ||
options: Omit<SequelasticSearchOptions, "wholeResponse">; | ||
}; | ||
declare type SequelasticMultipleSearchOptions = { | ||
indices?: (SequelasticMultipleSearchIndex | string)[]; | ||
from?: number; | ||
size?: number; | ||
fuzzy?: boolean; | ||
fuzziness?: "AUTO" | number; | ||
}; | ||
declare type SequelasticSyncOptions = { | ||
@@ -46,2 +57,4 @@ refresh?: boolean; | ||
customSearch(params: elasticSearch.RequestParams.Search): Promise<elasticSearch.ApiResponse<Record<string, any>, Record<string, unknown>>>; | ||
searchInIndices(query: string, options?: SequelasticMultipleSearchOptions): Promise<elasticSearch.ApiResponse<Record<string, any>, Record<string, unknown>>>; | ||
allIndices(): Promise<any>; | ||
private handleErrors; | ||
@@ -48,0 +61,0 @@ } |
@@ -192,2 +192,47 @@ "use strict"; | ||
} | ||
searchInIndices(query, options) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const existingIndices = (yield this.elastic.cat.indices({ format: "json" })).body.map((x) => x.index); | ||
const validIndices = (options === null || options === void 0 ? void 0 : options.indices) | ||
? options === null || options === void 0 ? void 0 : options.indices.filter((index) => { | ||
if (typeof index === "string") { | ||
return existingIndices.findIndex((x) => x === index) !== -1; | ||
} | ||
else { | ||
return existingIndices.findIndex((x) => x === index.index) !== -1; | ||
} | ||
}) | ||
: existingIndices; | ||
const payload = []; | ||
validIndices.forEach((index) => { | ||
var _a, _b, _c, _d; | ||
const indexName = typeof index === "string" ? index : index.index; | ||
const from = typeof index === "string" ? options === null || options === void 0 ? void 0 : options.from : (_a = index.options) === null || _a === void 0 ? void 0 : _a.from; | ||
const size = typeof index === "string" ? options === null || options === void 0 ? void 0 : options.size : (_b = index.options) === null || _b === void 0 ? void 0 : _b.size; | ||
const fuzzy = typeof index === "string" ? options === null || options === void 0 ? void 0 : options.fuzzy : (_c = index.options) === null || _c === void 0 ? void 0 : _c.fuzzy; | ||
const fuzziness = typeof index === "string" | ||
? options === null || options === void 0 ? void 0 : options.fuzziness | ||
: (_d = index.options) === null || _d === void 0 ? void 0 : _d.fuzziness; | ||
payload.push({ index: indexName }, { | ||
from, | ||
size, | ||
query: { | ||
query_string: { query: fuzzy ? `${query}~` : query, fuzziness }, | ||
}, | ||
}); | ||
}); | ||
try { | ||
return yield this.elastic.msearch({ body: payload }); | ||
} | ||
catch (err) { | ||
this.handleErrors(err); | ||
} | ||
}); | ||
} | ||
allIndices() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const allIndices = yield (yield this.elastic.cat.indices({ format: "json" })).body.map((x) => x.index); | ||
return allIndices; | ||
}); | ||
} | ||
handleErrors(error) { | ||
@@ -218,9 +263,2 @@ var _a, _b, _c; | ||
exports.checkIndex = checkIndex; | ||
// type SqlType = "INTEGER" | "STRING" | "DATE" | "VIRTUAL" | "TEXT" | "JSON"; | ||
// type SqlAttributesType = { | ||
// [key: string]: { | ||
// type: SqlType; | ||
// field: string; | ||
// }; | ||
// }; | ||
function parseSqlAttributesToElastic(rawAttributes) { | ||
@@ -227,0 +265,0 @@ const newProps = {}; |
{ | ||
"name": "sequelastic", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "bridge utility between sequelize-typescript and ElasticSearch", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -119,7 +119,7 @@ # Sequelastic | ||
| property | type | description | default | | ||
| :------: | :---------------------------------------------------: | :----------------------------------: | :-------: | | ||
| query | string | the elasticSearch query string | none | | ||
| index | string | the index where search for something | "\*" | | ||
| options | [SequelasticSearchOptions](#sequelasticsearchoptions) | search options | undefined | | ||
| property | type | description | default | | ||
| :------: | :----------------------------------------------------------------------------------------------------------------: | :----------------------------------: | :-------: | | ||
| query | [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?retiredLocale=it) | the elasticSearch query string | none | | ||
| index | [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?retiredLocale=it) | the index where search for something | "\*" | | ||
| options | [SequelasticSearchOptions](#sequelasticsearchoptions) | search options | undefined | | ||
@@ -144,4 +144,35 @@ </br> | ||
### searchInIndices | ||
_Make a search on different indices with a single request_ | ||
this function makes a multiple search on elasticSearch adding using the same query, you can choose, for each index, the fuzziness and the pagination | ||
```typescript | ||
sequelastic.searchInIndices(query: string, options: SequelasticMultipleSearchOptions) => Promise< | ||
elasticSearch.ApiResponse<Record<string, any>, Record<string, unknown>> | ||
> | ||
``` | ||
| property | type | description | default | | ||
| :------: | :----------------------------------------------------------------------------------------------------------------: | :------------------------------------------: | :-------: | | ||
| query | [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?retiredLocale=it) | the search query, you can also use operators | none | | ||
| options | [SequelasticMultipleSearchOptions](#sequelasticmultiplesearchoptions) | the search options | undefined | | ||
</br> | ||
### allIndices | ||
_Get all indices in the cluster_ | ||
```typescript | ||
sequelastic.allIndices() => Promise<string[]> | ||
``` | ||
</br> | ||
--- | ||
</br> | ||
## Sequelastic Types | ||
@@ -151,4 +182,2 @@ | ||
_object_ | ||
| key | type | description | default | | ||
@@ -164,4 +193,2 @@ | :------------------: | :----------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------: | :-------------------: | | ||
_object_ | ||
| key | type | description | | ||
@@ -177,4 +204,2 @@ | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | | ||
_object_ | ||
| key | type | description | default | | ||
@@ -188,10 +213,32 @@ | :-----: | :------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------: | :-----: | | ||
| key | type | description | default | | ||
| :-----------: | :--------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------: | :-----: | | ||
| fuzzy | [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean?retiredLocale=it) | use [fuzzy search](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html) | false | | ||
| fuzziness | "AUTO" \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | search [fuzziness](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) | "AUTO" | | ||
| wholeResponse | [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean?retiredLocale=it) | get as return the whole search response or only the hits | false | | ||
| from | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | offset from the first result | 0 | | ||
| size | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | maximum amount of hits to be returned | 10 | | ||
| key | type | description | default | | ||
| :------------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------: | :-----: | | ||
| fuzzy _(optional)_ | [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean?retiredLocale=it) | use [fuzzy search](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html) | false | | ||
| fuzziness _(optional)_ | "AUTO" \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | search [fuzziness](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) | "AUTO" | | ||
| wholeResponse _(optional)_ | [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean?retiredLocale=it) | get as return the whole search response or only the hits | false | | ||
| from _(optional)_ | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | offset from the first result | 0 | | ||
| size _(optional)_ | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | maximum amount of hits to be returned | 10 | | ||
## SequelasticMultipleSearchOptions | ||
| key | type | description | default | | ||
| :--------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------: | :-------------: | | ||
| indices _(optional)_ | (string \| [SequelasticMultipleSearchIndex](#sequelasticmultiplesearchindex) )[] | the indices where search for the query | all the indices | | ||
| fuzzy _(optional)_ | [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean?retiredLocale=it) | use [fuzzy search](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html) | false | | ||
| fuzziness _(optional)_ | "AUTO" \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | search [fuzziness](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) | "AUTO" | | ||
| from _(optional)_ | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | offset from the first result | 0 | | ||
| size _(optional)_ | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | maximum amount of hits to be returned | 10 | | ||
</br> | ||
## SequelasticMultipleSearchIndex | ||
| key | type | description | default | | ||
| :--------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------: | :-----: | | ||
| index | string | index where search for the query | none | | ||
| fuzzy _(optional)_ | [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean?retiredLocale=it) | use [fuzzy search](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html) | false | | ||
| fuzziness _(optional)_ | "AUTO" \| [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | search [fuzziness](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) | "AUTO" | | ||
| from _(optional)_ | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | offset from the first result | 0 | | ||
| size _(optional)_ | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number?retiredLocale=it) | maximum amount of hits to be returned | 10 | | ||
--- |
@@ -26,3 +26,3 @@ import "colors"; | ||
fuzzy?: boolean; | ||
fuzziness: "AUTO" | number; | ||
fuzziness?: "AUTO" | number; | ||
wholeResponse?: false; | ||
@@ -33,2 +33,15 @@ from?: number; | ||
type SequelasticMultipleSearchIndex = { | ||
index: string; | ||
options: Omit<SequelasticSearchOptions, "wholeResponse">; | ||
}; | ||
type SequelasticMultipleSearchOptions = { | ||
indices?: (SequelasticMultipleSearchIndex | string)[]; | ||
from?: number; | ||
size?: number; | ||
fuzzy?: boolean; | ||
fuzziness?: "AUTO" | number; | ||
}; | ||
type SequelasticSyncOptions = { refresh?: boolean }; | ||
@@ -262,2 +275,60 @@ | ||
public async searchInIndices( | ||
query: string, | ||
options?: SequelasticMultipleSearchOptions | ||
) { | ||
const existingIndices: string[] = ( | ||
await this.elastic.cat.indices({ format: "json" }) | ||
).body.map((x: any) => x.index); | ||
const validIndices = options?.indices | ||
? options?.indices.filter((index) => { | ||
if (typeof index === "string") { | ||
return existingIndices.findIndex((x) => x === index) !== -1; | ||
} else { | ||
return existingIndices.findIndex((x) => x === index.index) !== -1; | ||
} | ||
}) | ||
: existingIndices; | ||
const payload: any[] = []; | ||
validIndices.forEach((index) => { | ||
const indexName = typeof index === "string" ? index : index.index; | ||
const from = | ||
typeof index === "string" ? options?.from : index.options?.from; | ||
const size = | ||
typeof index === "string" ? options?.size : index.options?.size; | ||
const fuzzy = | ||
typeof index === "string" ? options?.fuzzy : index.options?.fuzzy; | ||
const fuzziness = | ||
typeof index === "string" | ||
? options?.fuzziness | ||
: index.options?.fuzziness; | ||
payload.push( | ||
{ index: indexName }, | ||
{ | ||
from, | ||
size, | ||
query: { | ||
query_string: { query: fuzzy ? `${query}~` : query, fuzziness }, | ||
}, | ||
} | ||
); | ||
}); | ||
try { | ||
return await this.elastic.msearch({ body: payload }); | ||
} catch (err) { | ||
this.handleErrors(err); | ||
} | ||
} | ||
public async allIndices() { | ||
const allIndices = await ( | ||
await this.elastic.cat.indices({ format: "json" }) | ||
).body.map((x: any) => x.index); | ||
return allIndices; | ||
} | ||
private handleErrors(error: any): never { | ||
@@ -287,9 +358,2 @@ if (error.body?.error) { | ||
// type SqlType = "INTEGER" | "STRING" | "DATE" | "VIRTUAL" | "TEXT" | "JSON"; | ||
// type SqlAttributesType = { | ||
// [key: string]: { | ||
// type: SqlType; | ||
// field: string; | ||
// }; | ||
// }; | ||
function parseSqlAttributesToElastic(rawAttributes: any) { | ||
@@ -296,0 +360,0 @@ const newProps: any = {}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51771
719
239
1