Skip to content

TagSpaces is an offline, open source, document manager with tagging support

implementing multi location search

Ilian Sapundshievccc3885be8c9
diff --git a/app/components/Search.tsx b/app/components/Search.tsx
index a9a0fbbea..c4f8c8e15 100644
--- a/app/components/Search.tsx
+++ b/app/components/Search.tsx
@@ -74,6 +74,8 @@ interface Props {
classes: any;
style: any;
searchLocationIndex: (searchQuery: SearchQuery) => void;
+ createLocationsIndexes: () => void;
+ searchAllLocations: (searchQuery: SearchQuery) => void;
loadDirectoryContent: (path: string) => void;
openURLExternally: (url: string) => void;
hideDrawer?: () => void;
@@ -317,7 +319,11 @@ class Search extends React.Component<Props, State> {
currentDirectory: this.props.currentDirectory
};
console.log('Search object: ' + JSON.stringify(searchQuery));
- this.props.searchLocationIndex(searchQuery);
+ if (this.state.searchBoxing === 'global') {
+ this.props.searchAllLocations(searchQuery);
+ } else {
+ this.props.searchLocationIndex(searchQuery);
+ }
};
handleSearchMenu = (event: any) => {
@@ -366,6 +372,7 @@ class Search extends React.Component<Props, State> {
anchorEl={this.state.searchMenuAnchorEl}
open={this.state.searchMenuOpened}
onClose={this.handleCloseSearchMenu}
+ createLocationsIndexes={this.props.createLocationsIndexes}
openURLExternally={this.props.openURLExternally}
/>
<div className={classes.searchArea}>
@@ -769,7 +776,9 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
+ searchAllLocations: LocationIndexActions.searchAllLocations,
searchLocationIndex: LocationIndexActions.searchLocationIndex,
+ createLocationsIndexes: LocationIndexActions.createLocationsIndexes,
loadDirectoryContent: AppActions.loadDirectoryContent,
openURLExternally: AppActions.openURLExternally
},
diff --git a/app/components/menus/SearchMenu.tsx b/app/components/menus/SearchMenu.tsx
index 2d91af408..dd5a67071 100644
--- a/app/components/menus/SearchMenu.tsx
+++ b/app/components/menus/SearchMenu.tsx
@@ -31,6 +31,7 @@ interface Props {
anchorEl: Element;
onClose: () => void;
openURLExternally: (url: string) => void;
+ createLocationsIndexes: () => void;
}
const SearchMenu = (props: Props) => (
@@ -48,6 +49,18 @@ const SearchMenu = (props: Props) => (
</ListItemIcon>
<ListItemText primary={i18n.t('core:help')} />
</MenuItem>
+ <MenuItem
+ data-tid="searchMenuHelp"
+ onClick={() => {
+ props.onClose();
+ props.createLocationsIndexes();
+ }}
+ >
+ <ListItemIcon>
+ <HelpIcon />
+ </ListItemIcon>
+ <ListItemText primary={i18n.t('core:Update all location indexes')} />
+ </MenuItem>
</Menu>
</div>
);
diff --git a/app/reducers/app.ts b/app/reducers/app.ts
index b5a390f96..f48b8cc7e 100644
--- a/app/reducers/app.ts
+++ b/app/reducers/app.ts
@@ -53,7 +53,8 @@ export const types = {
LOAD_DIRECTORY_SUCCESS: 'APP/LOAD_DIRECTORY_SUCCESS',
LOAD_DIRECTORY_FAILURE: 'APP/LOAD_DIRECTORY_FAILURE',
CLEAR_DIRECTORY_CONTENT: 'APP/CLEAR_DIRECTORY_CONTENT',
- INDEX_DIRECTORY_SEARCH: 'APP/INDEX_DIRECTORY_SEARCH',
+ SET_SEARCH_RESULTS: 'APP/SET_SEARCH_RESULTS',
+ APPEND_SEARCH_RESULTS: 'APP/APPEND_SEARCH_RESULTS',
OPEN_FILE: 'APP/OPEN_FILE',
TOGGLE_ENTRY_FULLWIDTH: 'APP/TOGGLE_ENTRY_FULLWIDTH',
SET_ENTRY_FULLWIDTH: 'APP/SET_ENTRY_FULLWIDTH',
@@ -278,13 +279,24 @@ export default (state: any = initialState, action: any) => {
selectDirectoryDialogOpened: !state.selectDirectoryDialogOpened
};
}
- case types.INDEX_DIRECTORY_SEARCH: {
+ case types.SET_SEARCH_RESULTS: {
return {
...state,
currentDirectoryEntries: action.searchResults,
isLoading: false
};
}
+ case types.APPEND_SEARCH_RESULTS: {
+ const newDirEntries = [
+ ...state.currentDirectoryEntries,
+ ...action.searchResults
+ ];
+ return {
+ ...state,
+ currentDirectoryEntries: newDirEntries,
+ isLoading: false
+ };
+ }
case types.SET_NOTIFICATION: {
return {
...state,
@@ -925,8 +937,12 @@ export const actions = {
);
});
},
- updateSearchResults: (searchResults: Array<Object> | []) => ({
- type: types.INDEX_DIRECTORY_SEARCH,
+ setSearchResults: (searchResults: Array<Object> | []) => ({
+ type: types.SET_SEARCH_RESULTS,
+ searchResults
+ }),
+ appendSearchResults: (searchResults: Array<Object> | []) => ({
+ type: types.APPEND_SEARCH_RESULTS,
searchResults
}),
setCurrentLocationId: (locationId: string | null) => ({
diff --git a/app/reducers/location-index.ts b/app/reducers/location-index.ts
index a2c495aba..9b78ce517 100644
--- a/app/reducers/location-index.ts
+++ b/app/reducers/location-index.ts
@@ -16,7 +16,7 @@
*
*/
-import { Location, getLocation, locationType } from './locations';
+import { Location, getLocation, getLocations, locationType } from './locations';
import { createDirectoryIndex } from '../services/utils-io';
import { Pro } from '../pro';
import {
@@ -228,6 +228,7 @@ export const actions = {
createDirectoryIndex(directoryPath, extractText)
.then(directoryIndex => {
if (isCurrentLocation) {
+ // Load index only if current location
GlobalSearch.index = directoryIndex;
}
dispatch(actions.indexDirectorySuccess());
@@ -235,7 +236,7 @@ export const actions = {
// && (currentLocation.persistIndex || PlatformIO.haveObjectStoreSupport())) { // always persist on s3 stores
Pro.Indexer.persistIndex(
directoryPath,
- GlobalSearch.index,
+ directoryIndex,
currentLocation.type === locationType.TYPE_CLOUD
? '/'
: AppConfig.dirSeparator
@@ -247,10 +248,56 @@ export const actions = {
dispatch(actions.indexDirectoryFailure(err));
});
},
- loadDirectoryIndex: (directoryPath: string) => (
+ createLocationsIndexes: (extractText: boolean = true) => (
dispatch: (actions: Object) => void,
getState: () => any
) => {
+ const state = getState();
+ const currentLocation: Location = getLocation(
+ state,
+ state.app.currentLocationId
+ );
+ dispatch(actions.startDirectoryIndexing());
+ const allLocations = getLocations(state);
+ const locationPaths = [];
+ allLocations.forEach(location => {
+ locationPaths.push(location.paths[0]);
+ });
+ const result = locationPaths.reduce(
+ (accumulatorPromise, nextPath) =>
+ accumulatorPromise.then(() => createDirectoryIndex(nextPath, extractText)
+ .then(directoryIndex => {
+ if (Pro && Pro.Indexer) {
+ Pro.Indexer.persistIndex(
+ nextPath,
+ directoryIndex,
+ currentLocation.type === locationType.TYPE_CLOUD
+ ? '/'
+ : AppConfig.dirSeparator
+ );
+ }
+ return true;
+ })
+ .catch(err => {
+ dispatch(actions.indexDirectoryFailure(err));
+ })),
+ Promise.resolve()
+ );
+
+ result
+ .then(e => {
+ dispatch(actions.indexDirectorySuccess());
+ console.log('Resolution is complete!', e);
+ return true;
+ })
+ .catch(e => {
+ console.warn('Resolution is faled!', e);
+ });
+ },
+ loadDirectoryIndex: (
+ directoryPath: string,
+ isCurrentLocation: boolean = true
+ ) => (dispatch: (actions: Object) => void, getState: () => any) => {
const state = getState();
const currentLocation: Location = getLocation(
state,
@@ -267,7 +314,11 @@ export const actions = {
? '/'
: AppConfig.dirSeparator
)
- .then(() => {
+ .then(directoryIndex => {
+ if (isCurrentLocation) {
+ // Load index only if current location
+ GlobalSearch.index = directoryIndex;
+ }
dispatch(actions.indexDirectorySuccess());
return true;
})
@@ -287,25 +338,21 @@ export const actions = {
type: types.INDEX_DIRECTORY_CLEAR
}),
searchLocationIndex: (searchQuery: SearchQuery) => (
- dispatch: (actions: Object) => void,
- getState: () => any
+ dispatch: (actions: Object) => void
) => {
dispatch(
AppActions.showNotification(i18n.t('core:searching'), 'default', false)
);
setTimeout(() => {
// Workaround used to show the start search notification
- Search.searchLocationIndex(
- getState().locationIndex.currentDirectoryIndex,
- searchQuery
- )
+ Search.searchLocationIndex(GlobalSearch.index, searchQuery)
.then(searchResults => {
- dispatch(AppActions.updateSearchResults(searchResults));
+ dispatch(AppActions.setSearchResults(searchResults));
dispatch(AppActions.hideNotifications());
return true;
})
.catch(() => {
- dispatch(AppActions.updateSearchResults([]));
+ dispatch(AppActions.setSearchResults([]));
dispatch(AppActions.hideNotifications());
dispatch(
AppActions.showNotification(
@@ -317,6 +364,74 @@ export const actions = {
});
}, 50);
},
+ searchAllLocations: (searchQuery: SearchQuery) => (
+ dispatch: (actions: Object) => void,
+ getState: () => any
+ ) => {
+ const state = getState();
+ const currentLocation: Location = getLocation(
+ state,
+ state.app.currentLocationId
+ );
+ dispatch(actions.startDirectoryIndexing());
+ dispatch(AppActions.setSearchResults([]));
+ const allLocations = getLocations(state);
+ const locationPaths = [];
+ allLocations.forEach(location => {
+ locationPaths.push(location.paths[0]);
+ });
+ const result = locationPaths.reduce(
+ (accumulatorPromise, nextPath) =>
+ accumulatorPromise.then(() =>
+ createDirectoryIndex(nextPath, true)
+ .then(directoryIndex => {
+ AppActions.showNotification(i18n.t('Searching:' + nextPath), 'default', true)
+ console.log('Searching in:' + nextPath)
+ Search.searchLocationIndex(directoryIndex, searchQuery)
+ .then(searchResults => {
+ dispatch(AppActions.appendSearchResults(searchResults));
+ dispatch(AppActions.hideNotifications());
+ return true;
+ })
+ .catch(() => {
+ dispatch(AppActions.setSearchResults([]));
+ dispatch(AppActions.hideNotifications());
+ dispatch(
+ AppActions.showNotification(
+ i18n.t('core:searchingFailed'),
+ 'warning',
+ true
+ )
+ );
+ });
+ if (Pro && Pro.Indexer) {
+ Pro.Indexer.persistIndex(
+ nextPath,
+ directoryIndex,
+ currentLocation.type === locationType.TYPE_CLOUD
+ ? '/'
+ : AppConfig.dirSeparator
+ );
+ }
+ return true;
+ })
+ .catch(err => {
+ dispatch(actions.indexDirectoryFailure(err));
+ })
+ ),
+ Promise.resolve()
+ );
+
+ result
+ .then(e => {
+ dispatch(actions.indexDirectorySuccess());
+ console.log('Resolution is complete!', e);
+ return true;
+ })
+ .catch(e => {
+ console.warn('Resolution is faled!', e);
+ });
+ },
indexDirectorySuccess: () => ({
type: types.INDEX_DIRECTORY_SUCCESS
}),
diff --git a/app/reducers/tagging-actions.ts b/app/reducers/tagging-actions.ts
index 7b0714758..33ea8bea8 100644
--- a/app/reducers/tagging-actions.ts
+++ b/app/reducers/tagging-actions.ts
@@ -37,6 +37,7 @@ import { formatDateTime4Tag, isPlusCode } from '../utils/misc';
import AppConfig from '../config';
import PlatformIO from '../services/platform-io';
import { Pro } from '../pro';
+import GlobalSearch from '../services/search-index';
export const defaultTagLocation = OpenLocationCode.encode(51.48, 0, undefined); // default tag coordinate Greenwich
@@ -647,7 +648,7 @@ const actions = {
return true;
}
- if (locationIndex.currentDirectoryIndex.length < 1) {
+ if (GlobalSearch.index.length < 1) {
dispatch(
AppActions.showNotification(
'Please index location first',
diff --git a/app/services/search.ts b/app/services/search.ts
index 0387db80a..14bf521dd 100644
--- a/app/services/search.ts
+++ b/app/services/search.ts
@@ -20,7 +20,6 @@ import Fuse from 'fuse.js';
import jmespath from 'jmespath';
import { Tag } from '../reducers/taglibrary';
import { Pro } from '../pro';
-import GlobalSearch from './search-index';
// export type FileTypeGroups = 'images' | 'notes' | 'documents' | 'audio' | 'video' | 'archives';
@@ -250,7 +249,7 @@ export default class Search {
new Promise(resolve => {
console.time('searchtime');
const jmespathQuery = constructjmespathQuery(searchQuery);
- let results = prepareIndex(GlobalSearch.index);
+ let results = prepareIndex(locationContent);
let searched = false;
// Limiting the search to current folder only (with sub-folders)