1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Mobile: Added button to fix search engine index

This commit is contained in:
Laurent Cozic 2019-06-28 00:48:52 +01:00
parent ffc311d7bd
commit 4d92187327
2 changed files with 36 additions and 2 deletions

View File

@ -1,5 +1,5 @@
const React = require('react'); const Component = React.Component; const React = require('react'); const Component = React.Component;
const { Platform, TouchableOpacity, Linking, View, Switch, StyleSheet, Text, Button, ScrollView, TextInput } = require('react-native'); const { Platform, TouchableOpacity, Linking, View, Switch, StyleSheet, Text, Button, ScrollView, TextInput, Alert } = require('react-native');
const { connect } = require('react-redux'); const { connect } = require('react-redux');
const { ScreenHeader } = require('lib/components/screen-header.js'); const { ScreenHeader } = require('lib/components/screen-header.js');
const { _, setLocale } = require('lib/locale.js'); const { _, setLocale } = require('lib/locale.js');
@ -14,6 +14,7 @@ const NavService = require('lib/services/NavService.js');
const VersionInfo = require('react-native-version-info').default; const VersionInfo = require('react-native-version-info').default;
const { ReportService } = require('lib/services/report.js'); const { ReportService } = require('lib/services/report.js');
const { time } = require('lib/time-utils'); const { time } = require('lib/time-utils');
const SearchEngine = require('lib/services/SearchEngine');
const RNFS = require('react-native-fs'); const RNFS = require('react-native-fs');
import Slider from '@react-native-community/slider'; import Slider from '@react-native-community/slider';
@ -78,6 +79,12 @@ class ConfigScreenComponent extends BaseScreenComponent {
this.setState({ creatingReport: false }); this.setState({ creatingReport: false });
} }
this.fixSearchEngineIndexButtonPress_ = async () => {
this.setState({ fixingSearchIndex: true });
await SearchEngine.instance().rebuildIndex();
this.setState({ fixingSearchIndex: false });
}
this.logButtonPress_ = () => { this.logButtonPress_ = () => {
NavService.go('Log'); NavService.go('Log');
} }
@ -191,6 +198,15 @@ class ConfigScreenComponent extends BaseScreenComponent {
renderButton(key, title, clickHandler, options = null) { renderButton(key, title, clickHandler, options = null) {
if (!options) options = {}; if (!options) options = {};
let descriptionComp = null;
if (options.description) {
descriptionComp = (
<View style={{flex:1, marginTop: 10}}>
<Text style={this.styles().descriptionText}>{options.description}</Text>
</View>
);
}
return ( return (
<View key={key} style={this.styles().settingContainer}> <View key={key} style={this.styles().settingContainer}>
<View style={{flex:1, flexDirection: 'column'}}> <View style={{flex:1, flexDirection: 'column'}}>
@ -198,6 +214,7 @@ class ConfigScreenComponent extends BaseScreenComponent {
<Button title={title} onPress={clickHandler} disabled={!!options.disabled}/> <Button title={title} onPress={clickHandler} disabled={!!options.disabled}/>
</View> </View>
{ options.statusComp } { options.statusComp }
{ descriptionComp }
</View> </View>
</View> </View>
); );
@ -338,6 +355,7 @@ class ConfigScreenComponent extends BaseScreenComponent {
settingComps.push(this.renderButton('status_button', _('Sync Status'), this.syncStatusButtonPress_)); settingComps.push(this.renderButton('status_button', _('Sync Status'), this.syncStatusButtonPress_));
settingComps.push(this.renderButton('log_button', _('Log'), this.logButtonPress_)); settingComps.push(this.renderButton('log_button', _('Log'), this.logButtonPress_));
settingComps.push(this.renderButton('export_report_button', this.state.creatingReport ? _('Creating report...') : _('Export Debug Report'), this.exportDebugButtonPress_, { disabled: this.state.creatingReport })); settingComps.push(this.renderButton('export_report_button', this.state.creatingReport ? _('Creating report...') : _('Export Debug Report'), this.exportDebugButtonPress_, { disabled: this.state.creatingReport }));
settingComps.push(this.renderButton('fix_search_engine_index', this.state.fixingSearchIndex ? _('Fixing search index...') : _('Fix search index'), this.fixSearchEngineIndexButtonPress_, { disabled: this.state.fixingSearchIndex, description: _('Use this to rebuild the search index if there is a problem with search. It may take some times depending on the number of notes.') }));
settingComps.push(this.renderHeader('moreInfo', _('More information'))); settingComps.push(this.renderHeader('moreInfo', _('More information')));

View File

@ -7,6 +7,7 @@ const BaseModel = require('lib/BaseModel.js');
const ItemChangeUtils = require('lib/services/ItemChangeUtils'); const ItemChangeUtils = require('lib/services/ItemChangeUtils');
const { pregQuote, scriptType } = require('lib/string-utils.js'); const { pregQuote, scriptType } = require('lib/string-utils.js');
const removeDiacritics = require('diacritics').remove; const removeDiacritics = require('diacritics').remove;
const { sprintf } = require('sprintf-js');
class SearchEngine { class SearchEngine {
@ -91,6 +92,12 @@ class SearchEngine {
}, 10000); }, 10000);
} }
async rebuildIndex() {
Setting.setValue('searchEngine.lastProcessedChangeId', 0)
Setting.setValue('searchEngine.initialIndexingDone', false);
return this.syncTables();
}
async syncTables() { async syncTables() {
if (this.isIndexing_) return; if (this.isIndexing_) return;
@ -109,6 +116,11 @@ class SearchEngine {
const startTime = Date.now(); const startTime = Date.now();
const report = {
inserted: 0,
deleted: 0,
};
let lastChangeId = Setting.value('searchEngine.lastProcessedChangeId'); let lastChangeId = Setting.value('searchEngine.lastProcessedChangeId');
try { try {
@ -122,6 +134,8 @@ class SearchEngine {
LIMIT 100 LIMIT 100
`, [BaseModel.TYPE_NOTE, lastChangeId]); `, [BaseModel.TYPE_NOTE, lastChangeId]);
const maxRow = await ItemChange.db().selectOne('SELECT max(id) FROM item_changes');
if (!changes.length) break; if (!changes.length) break;
const noteIds = changes.map(a => a.item_id); const noteIds = changes.map(a => a.item_id);
@ -137,9 +151,11 @@ class SearchEngine {
if (note) { if (note) {
const n = this.normalizeNote_(note); const n = this.normalizeNote_(note);
queries.push({ sql: 'INSERT INTO notes_normalized(id, title, body) VALUES (?, ?, ?)', params: [change.item_id, n.title, n.body] }); queries.push({ sql: 'INSERT INTO notes_normalized(id, title, body) VALUES (?, ?, ?)', params: [change.item_id, n.title, n.body] });
report.inserted++;
} }
} else if (change.type === ItemChange.TYPE_DELETE) { } else if (change.type === ItemChange.TYPE_DELETE) {
queries.push({ sql: 'DELETE FROM notes_normalized WHERE id = ?', params: [change.item_id] }); queries.push({ sql: 'DELETE FROM notes_normalized WHERE id = ?', params: [change.item_id] });
report.deleted++;
} else { } else {
throw new Error('Invalid change type: ' + change.type); throw new Error('Invalid change type: ' + change.type);
} }
@ -157,7 +173,7 @@ class SearchEngine {
await ItemChangeUtils.deleteProcessedChanges(); await ItemChangeUtils.deleteProcessedChanges();
this.logger().info('SearchEngine: Updated FTS table in ' + (Date.now() - startTime) + 'ms'); this.logger().info(sprintf('SearchEngine: Updated FTS table in %dms. Inserted: %d. Deleted: %d', Date.now() - startTime, report.inserted, report.deleted));
this.isIndexing_ = false; this.isIndexing_ = false;
} }