diff --git a/ReactNativeClient/lib/components/screens/config.js b/ReactNativeClient/lib/components/screens/config.js
index 24b580064..dba492f1b 100644
--- a/ReactNativeClient/lib/components/screens/config.js
+++ b/ReactNativeClient/lib/components/screens/config.js
@@ -1,5 +1,5 @@
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 { ScreenHeader } = require('lib/components/screen-header.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 { ReportService } = require('lib/services/report.js');
const { time } = require('lib/time-utils');
+const SearchEngine = require('lib/services/SearchEngine');
const RNFS = require('react-native-fs');
import Slider from '@react-native-community/slider';
@@ -78,6 +79,12 @@ class ConfigScreenComponent extends BaseScreenComponent {
this.setState({ creatingReport: false });
}
+ this.fixSearchEngineIndexButtonPress_ = async () => {
+ this.setState({ fixingSearchIndex: true });
+ await SearchEngine.instance().rebuildIndex();
+ this.setState({ fixingSearchIndex: false });
+ }
+
this.logButtonPress_ = () => {
NavService.go('Log');
}
@@ -191,6 +198,15 @@ class ConfigScreenComponent extends BaseScreenComponent {
renderButton(key, title, clickHandler, options = null) {
if (!options) options = {};
+ let descriptionComp = null;
+ if (options.description) {
+ descriptionComp = (
+
+ {options.description}
+
+ );
+ }
+
return (
@@ -198,6 +214,7 @@ class ConfigScreenComponent extends BaseScreenComponent {
{ options.statusComp }
+ { descriptionComp }
);
@@ -338,6 +355,7 @@ class ConfigScreenComponent extends BaseScreenComponent {
settingComps.push(this.renderButton('status_button', _('Sync Status'), this.syncStatusButtonPress_));
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('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')));
diff --git a/ReactNativeClient/lib/services/SearchEngine.js b/ReactNativeClient/lib/services/SearchEngine.js
index ec5db23a0..05f7cf931 100644
--- a/ReactNativeClient/lib/services/SearchEngine.js
+++ b/ReactNativeClient/lib/services/SearchEngine.js
@@ -7,6 +7,7 @@ const BaseModel = require('lib/BaseModel.js');
const ItemChangeUtils = require('lib/services/ItemChangeUtils');
const { pregQuote, scriptType } = require('lib/string-utils.js');
const removeDiacritics = require('diacritics').remove;
+const { sprintf } = require('sprintf-js');
class SearchEngine {
@@ -91,6 +92,12 @@ class SearchEngine {
}, 10000);
}
+ async rebuildIndex() {
+ Setting.setValue('searchEngine.lastProcessedChangeId', 0)
+ Setting.setValue('searchEngine.initialIndexingDone', false);
+ return this.syncTables();
+ }
+
async syncTables() {
if (this.isIndexing_) return;
@@ -109,6 +116,11 @@ class SearchEngine {
const startTime = Date.now();
+ const report = {
+ inserted: 0,
+ deleted: 0,
+ };
+
let lastChangeId = Setting.value('searchEngine.lastProcessedChangeId');
try {
@@ -122,6 +134,8 @@ class SearchEngine {
LIMIT 100
`, [BaseModel.TYPE_NOTE, lastChangeId]);
+ const maxRow = await ItemChange.db().selectOne('SELECT max(id) FROM item_changes');
+
if (!changes.length) break;
const noteIds = changes.map(a => a.item_id);
@@ -137,9 +151,11 @@ class SearchEngine {
if (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] });
+ report.inserted++;
}
} else if (change.type === ItemChange.TYPE_DELETE) {
queries.push({ sql: 'DELETE FROM notes_normalized WHERE id = ?', params: [change.item_id] });
+ report.deleted++;
} else {
throw new Error('Invalid change type: ' + change.type);
}
@@ -157,7 +173,7 @@ class SearchEngine {
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;
}