Sequelastic
:warning: This version only works with sequelize-typescript 1.1.x
The first sequelize-typescript and ElasticSearch bridge tool
installation
prerequisites:
in order to install sequelastic on your project just run:
npm install sequelastic
usage
first of all import and instantiate the utility:
import Sequelastic from "sequelastic";
import { model1, model2 } from "./sequelize-typescript-models";
const sequelastic = new Sequelastic({
node: "https://elastiSearchSevice.example:9200",
models: [model1, model2],
});
then sync your database with the elasticSearch service:
sequelastic
.sync()
.then((success) => {
if (success) {
console.log("Database synced correctly");
} else {
console.log("Something went wrong");
}
})
.catch((err) => {
console.error(err);
});
now you are ready to search whatever you want with sequelastic:
sequelastic
.search("foo", "bar", { fuzzy: true, fuzziness: "AUTO" })
.then((results) => {
console.log(results);
})
.catch((err) => {
console.error(err);
});
Sequelastic Functions
Constructor
create new Sequelastic instance
new Sequelastic(config: SequelasticContructorProps) => Sequelastic
Sync
Sync SQL database
this function will sync your database with the elasticSearch service using the following method:
- Deleting all the pre-existing indices
- Recreating all the indices using as index name the plural of the model name
- using bulk insertion to add all the corresponding records
sequelastic.sync() => Promise<boolean>
Search
Search in indices something
this function will search in elasticSearch using the search type query_string
sequelastic.search(query: string, index: string, options:SequelasticSearchOptions) => Promise<[{[key: string]: any}]>
sequelastic.search(query: string, index:string, options: SequelizeSearchOptions) => Promise<elasticSearch.ApiResponse<Record<string, any>, Record<string, unknown>>>
property | type | description | default |
---|
query | string | the elasticSearch query string | none |
index | string | the index where search for something | "*" |
options | SequelasticSearchOptions | search options | undefined |
customSearch
use a custom body for the elasticSearch _search
sequelastic.customSearch(params: elasticSearch.RequestParams.Search) => Promise<
elasticSearch.ApiResponse<Record<string, any>, Record<string, unknown>>
>
Sequelastic Types
SequelasticContructorProps
object
SequelasticModelType
object
key | type | description |
---|
model | Model | sequelize model to be indexed |
attributes (optional) | string[] | {exclude: string[]} | fields to include or exclude in index |
include (optional) | (string | SequelasticModelType)[] | object to eventually specify models to include in index |
SequelasticSyncOptions
object
SequelasticSearchOptions
key | type | description | default |
---|
fuzzy | boolean | use fuzzy search | false |
fuzziness | "AUTO" | number | search fuzziness | "AUTO" |
wholeResponse | boolean | get as return the whole search response or only the hits | false |
from | number | offset from the first result | 0 |
size | number | maximum amount of hits to be returned | 10 |