- MeiliSearch
- ElasticSearch
- Algolia
In your Directus installation root
npm install dimitrov-adrian/directus-extension-searchsync
or yarn
yarn add https://github.com/dimitrov-adrian/directus-extension-searchsync
Restart directus
Usage: npx directus extension:searchsync <subdommand>
Subcommands:
index
- Reindex all documents from configuration
The extension uses cosmiconfig for configuration loader with
searchsync
block or if EXTENSION_SEARCHSYNC_CONFIG_PATH
is set will try to use the file.
So, configuration should comes from one of next files:
- package.json
"searchsync":{...}
- .searchsyncrc
- .searchsyncrc.json
- .searchsyncrc.yaml
- .searchsyncrc.yml
- .searchsyncrc.js
- .searchsyncrc.cjs
- searchsync.config.js
- searchsync.config.cjs
server: object
Holds configuration for the search enginebatchLimit: number
Batch limit when performing index/reindex (defaults to 100)reindexOnStart: boolean
Performs full reindex of all documents upon Directus startscollections: object
Indexing data definitioncollections.<collection>.filter: object
The filter query in format like Directus on which item must match to be indexed (check Filter Rules )collections.<collection>.fields: array<string>
Fields that will be indexed in Directus formatcollections.<collection>.transform: function
(Could be defined only if config file is .js) A callback to return transformed/formatted data for indexing.collections.<collection>.indexName: string
Force collection name when storing in search indexcollections.<collection>.collectionField: string
If set, such field with value of the collection name will be added to the indexed document. Useful with conjuction with the indexName optioncollections.<collection>.settings: object
Used to pass additional indexing settings for the collection to the search engine (Currently only supported for meilisearch.)collections.<collection>.computePk: function
(Can only be defined in .js config file) A callback which can be used to calculate a custom primary key value used for an indexed document. This is useful for composite indexes where multiple collections are stored in the same index and might have overlapping primary keys.
{
"server": {
"type": "meilisearch",
"host": "http://search:7700/myindex",
"key": "the-private-key"
},
"batchLimit": 100,
"reindexOnStart": false,
"collections": {
"products": {
"filter": {
"status": "published",
"stock": "inStock"
},
"fields": ["title", "image.id", "category.title", "brand.title", "tags", "description", "price", "rating"],
"settings": {
"searchableAttributes": ["title", "description", "tags", "category.title", "brand.title"],
"displayedAttributes": ["title", "image.id", "description"],
"sortableAttributes": ["category.title", "price", "rating", "brand.title"]
}
},
"posts": {
"indexName": "blog_posts",
"collectionField": "_collection",
"filter": {
"status": "published"
},
"fields": ["title", "teaser", "body", "thumbnail.id"]
}
}
}
const config = {
server: {
type: 'meilisearch',
host: 'http://search:7700',
key: 'the-private-key',
},
reindexOnStart: false,
batchLimit: 100,
collections: {
pages: {
filter: {
status: 'published',
},
fields: ['title', 'teaser', 'body', 'thumbnail.id'],
transform: (item, { flattenObject, striptags }) => {
return {
...flattenObject(item),
body: striptags(item.body),
someCustomValue: 'Hello World!',
};
},
settings: {
searchableAttributes: ['title', 'description', 'tags', 'category.title', 'brand.title'],
displayedAttributes: ['title', 'image.id', 'description'],
sortableAttributes: ['category.title', 'price', 'rating', 'brand.title'],
},
},
},
};
// Use as object.
module.exports = config;
/**
* @param {Object} item
* @param {{striptags, flattenObject, objectMap}} utils
* @param {String} collectionName
* @returns {Object}
*/
function (item, { striptags, flattenObject, objectMap }, collectionName) {
return item
}
{
"type": "meilisearch",
"host": "http://search:7700",
"key": "the-private-key"
}
{
"type": "algolia",
"appId": "Application-Id",
"key": "secret-api-key"
}
New typeless behaviour, use collection names as index name.
{
"type": "elasticsearch",
"host": "http://search:9200/"
}
Use Authentification.
{
"type": "elasticsearch",
"host": "http://search:9200/",
"username": "elastic",
"password": "somepassword"
}
Ignore ssl-certificate-error.
{
"type": "elasticsearch",
"host": "http://search:9200/",
"ignore_cert": true
}
Old type behaviour, use collection names as types.
{
"type": "elasticsearch_legacy",
"host": "http://search:9200/projectindex"
}