2021SC@SDUSC
Today's analysis is the search function
File at
Web page initialization
Use a template library search_ sort ASC in model renders this web page
Set the title of the web page as "full site search"
Then check the configuration of full-text search first
Search the mysql database for variables and assign values to the variables variable here
Then search for variables whose name matches' variable '_ name', 'ft_ min_ word_ Value of len '
async indexAction() { const list = await this.model('search_model').order('sort ASC').select(); this.assign('list', list); this.meta_title = 'Total station search'; const variables = await this.model('mysql').query(`show variables`); const ft_min_word_len = think._.find(variables, ['Variable_name', 'ft_min_word_len']).Value; this.assign('ft_min_word_len', ft_min_word_len); return this.display(); }
Add search category
First, receive the name of the category to be added and assign it to the data variable
For security reasons, get the extend column attribute in the database cmswing/model
Add the modified data of extend attribute to search_model database
And respond to whether the addition was successful
async addAction() { if (this.isPost) { const data = this.post(); const extend = await this.model('cmswing/model').get_model(data.mod, 'extend'); data.extend = extend; const add = await this.model('search_model').add(data); if (add) { return this.success({name: 'Added successfully!'}); } else { return this.fail('Add failed!'); } } else { const modlist = await this.model('model').where({status: 1, id: ['>', 1]}).select(); this.assign('modlist', modlist); this.meta_title = 'Add search category'; return this.display(); } }
If the name of the category to be added is not received
Return to a page that adds a search category
Edit, delete and sort
Editing is the same as adding search categories. You can only change the add in the fifth line to update
async editAction() { if (this.isPost) { const data = this.post(); const extend = await this.model('cmswing/model').get_model(data.mod, 'extend'); data.extend = extend; const up = await this.model('search_model').update(data); if (up) { return this.success({name: 'Edit succeeded!'}); } else { return this.fail('Edit failed!'); } } else { const info = await this.model('search_model').find(this.get('id')); this.assign('info', info); const modlist = await this.model('model').where({status: 1, id: ['>', 1]}).select(); this.assign('modlist', modlist); this.meta_title = 'Edit search category'; return this.display(); } }
Deletion is even simpler than the first two
Just get the ID of the category you want to delete
You don't have to get a bunch of lists like before
async delAction() { const id = this.get('id'); const del = await this.model('search_model').where({id: id}).delete(); if (del) { return this.success({name: 'Delete succeeded!'}); } else { return this.fail('Deletion failed!'); } }
A simpler sort
It just uses the constructor of the parent class
async sortAction() { await super.sortAction('search_model'); }
Rebuild index
First you need to get a list of all the categories
And check the configuration
async createindexAction() { const paths = think.resource + '/backup/'; const lock = paths + 'createindex.lock'; const variables = await this.model('mysql').query(`show variables`); const ft_min_word_len = think._.find(variables, ['Variable_name', 'ft_min_word_len']).Value;
Then, when the obtained target is not empty, try to create a new lock file
When creating, check whether there are tasks in progress. If not, you can create them
Then search_ The tables in model are assigned to the tables array
And modify the extend value of each object in tables
The modification needs to be divided into different situations. When it is zero, it is modified to the name of the mod column
Otherwise, change to 'doucument'
Save the tables as the "create index_index" table again
if (this.isAjax('post') && !think.isEmpty(this.post())) { think.mkdir(paths); if (think.isFile(lock)) { return this.fail(20, 'A reconstruction task is detected. Please try again later!'); } else { fs.writeFileSync(lock, new Date()); } const tables = await this.model('search_model').select(); for (const v of tables) { if (v.extend == 0) { v.table = await this.model('cmswing/model').get_model(v.mod, 'name'); } else { v.table = 'document'; } } await this.session('createindex_tables', tables);
Clear the cache list and start index update
await this.model('search').where('1=1').delete(); const page = {'id': 0, 'page': 1, 'pagesize': this.post('pagesize')}; return this.json({ 'msg': {progress: 0, info: 'Start index update'}, 'page': page, 'status': 1 });
Otherwise, a "re index" page is returned
You also need to get the original index list
else { const variables = await this.model('mysql').query(`show variables`); const ft_min_word_len = think._.find(variables, ['Variable_name', 'ft_min_word_len']).Value; this.assign('ft_min_word_len', ft_min_word_len); this.meta_title = 'Rebuild index'; this.active = 'admin/search/index'; return this.display(); }
Delete lock file
As at creation, you need to check whether there are tasks in progress
You can unlock if you have
No, you can respond directly without this operation
unlockAction() { const paths = think.resource + '/backup/'; const lock = paths + 'createindex.lock'; if (think.isFile(lock)) { fs.unlinkSync(lock); return this.success({name: 'Unlock succeeded!'}); } else { // Create lock file return this.success({name: 'No unlocking required!'}); } }