Skip to content

Commit

Permalink
fix settings meilisearch
Browse files Browse the repository at this point in the history
  • Loading branch information
wulff007 committed Oct 28, 2024
1 parent 897dbb1 commit 6a2eef7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions src/data-indexer/data-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class DataIndexer {
filterBy = this.convertFilterToSearch(params.filter, allFields)
}

await this.validateFieldIsSortable(index, params.sort)
await this.validateFieldIsSortable(index, params.sort, params.searchBy)


let searchParams:SearchParams = {
Expand Down Expand Up @@ -196,13 +196,14 @@ export class DataIndexer {
if (!index) {
await this.createIndex(indexUid)
index = await this.getIndex(indexUid)
await this.updateIndexSettings(index, params.dataSourceConfig.fields)
await this.updateIndexSettings(index/*, params.dataSourceConfig.fields*/)
} else if (!params.ids || !params.ids.length){
newIndexUid = `${indexUid}__new__`
await this.searchClient.deleteIndexIfExists(newIndexUid)
await this.createIndex(newIndexUid)
index = await this.getIndex(newIndexUid)
await this.updateIndexSettings(index, params.dataSourceConfig.fields)
await this.updateIndexSettings(index, /*params.dataSourceConfig.fields*/)

}

let docs = await adapter.getData(params.dataSourceConfig, context, params.ids)
Expand Down Expand Up @@ -474,30 +475,34 @@ export class DataIndexer {
throw task.error
}

async updateIndexSettings(index: Index, fields: DatasourceField[]) {
let sort = fields.filter(f=>f.sortable).map(f=>f.alias)
let filter = fields.filter(f=>f.filterable).map(f=>f.alias)
let search = fields.filter(f=>f.searchable).map(f=>f.alias)
async updateIndexSettings(index: Index/*, fields: DatasourceField[]*/) {
//let sort = fields.filter(f=>f.sortable).map(f=>f.alias)
//let filter = fields.filter(f=>f.filterable).map(f=>f.alias)
//let search = fields.filter(f=>f.searchable).map(f=>f.alias)

// Need add filterable link field id to index linked data after update linked item
fields.filter(f => f.type === 'link' || f.type === 'enum').forEach(i => {
filter.push(`${i.alias}.id`)
})
// fields.filter(f => f.type === 'link' || f.type === 'enum').forEach(i => {
// filter.push(`${i.alias}.id`)
// })

let taskUid = (await index.updateSettings({
sortableAttributes: sort,
sortableAttributes: ['*'],
displayedAttributes: ['*'],
filterableAttributes: filter,
searchableAttributes: search.length ? search : ['*']
filterableAttributes: ['*'],
searchableAttributes: ['*']
})).taskUid

let task = await this.searchClient.waitForTask(taskUid)
if (task.status !== 'succeeded')
throw task.error
}

async validateFieldIsSortable(index: Index, sort: string[]) : Promise<boolean> {
let sortable = await index.getSortableAttributes()
async validateFieldIsSortable(index: Index, sort: string[], searchBy: string[]) : Promise<boolean> {
let settings = await index.getSettings()
let sortable = settings.sortableAttributes

console.log(settings)

let needUpdate = false

for(const i in sort) {
Expand All @@ -510,6 +515,10 @@ export class DataIndexer {
}
}

if (searchBy) {

}

if (needUpdate) {
let taskUid = (await index.updateSortableAttributes(sortable)).taskUid
let task = await this.searchClient.waitForTask(taskUid)
Expand Down
2 changes: 1 addition & 1 deletion src/datasources/datasourceV2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class DataSourceV2Service {
await this.indexer.createIndex(indexUid)
index = await this.indexer.getIndex(indexUid)
}
await this.indexer.updateIndexSettings(index, ds.fields)
await this.indexer.updateIndexSettings(index, /*ds.fields*/)
await this.indexer.dataReindex({dataSourceConfig: ds}, context)
}

Expand Down

0 comments on commit 6a2eef7

Please sign in to comment.