TagSpaces is an offline, open source, document manager with tagging support
search optimizations
Ilian Sapundshievb50d84bf657e
diff --git a/app/components/Search.js b/app/components/Search.jsindex 64fc6dffa..986972713 100644--- a/app/components/Search.js+++ b/app/components/Search.js@@ -44,6 +44,7 @@ import { actions as AppActions, getIndexedEntriesCount, isIndexing, getDirectoryimport styles from './SidePanels.css';import i18n from '../services/i18n';import { FileTypeGroups, type SearchQuery } from '../services/search';+import { Pro } from '../pro';type Props = {classes: Object,@@ -144,6 +145,7 @@ class Search extends React.Component<Props, State> {value={this.state.textQuery}onChange={this.handleInputChange}onKeyDown={this.startSearch}+ autoFocusendAdornment={(this.state.textQuery && this.state.textQuery.length > 0) &&<InputAdornment position="end">@@ -172,7 +174,7 @@ class Search extends React.Component<Props, State> {</FormControl><FormControlclassName={classes.formControl}- disabled={indexing}+ disabled={indexing || !Pro}title={i18n.t('core:thisFunctionalityIsAvailableInPro')}><InputLabel htmlFor="file-type">{i18n.t('core:fileType')}</InputLabel>@@ -190,6 +192,18 @@ class Search extends React.Component<Props, State> {</IconButton>{i18n.t('core:searchFolders')}</MenuItem>+ <MenuItem value={FileTypeGroups.files}>+ <IconButton>+ <FolderIcon />+ </IconButton>+ {i18n.t('core:searchFiles')}+ </MenuItem>+ <MenuItem value={FileTypeGroups.untagged}>+ <IconButton>+ <FolderIcon />+ </IconButton>+ {i18n.t('core:searchUntaggedEntries')}+ </MenuItem><MenuItemvalue={FileTypeGroups.images}title={FileTypeGroups.images.toString()}@@ -247,7 +261,7 @@ class Search extends React.Component<Props, State> {</Select>{/* <FormHelperText>{i18n.t('core:searchFileTypes')}</FormHelperText> */}</FormControl>- <FormControl+ {/* <FormControlclassName={classes.formControl}disabled={true}title={i18n.t('core:thisFunctionalityIsAvailableInPro')}@@ -280,17 +294,17 @@ class Search extends React.Component<Props, State> {{i18n.t('core:anYearAgo')}</MenuItem></Select>- {/* <FormHelperText>{i18n.t('')}</FormHelperText> */}- </FormControl>- <FormControl+ <FormHelperText>{i18n.t('')}</FormHelperText>+ </FormControl> */ }+ { /* <FormControlclassName={classes.formControl}disabled={true}title={i18n.t('core:thisFunctionalityIsAvailableInPro')}><InputLabel htmlFor="searchHistory">{i18n.t('core:searchHistory')}</InputLabel><Input id="searchHistory" />- {/* <FormHelperText>{i18n.t('core:chooseSearchQuery')}</FormHelperText> */}- </FormControl>+ <FormHelperText>{i18n.t('core:chooseSearchQuery')}</FormHelperText>+ </FormControl> */}<FormControl className={classes.formControl}><Buttondisabled={indexing}diff --git a/app/locales/en/core.json b/app/locales/en/core.jsonindex f7c80c71e..7695349cb 100644--- a/app/locales/en/core.json+++ b/app/locales/en/core.json@@ -219,6 +219,8 @@"searchVideoFiles": "Videos","searchArchives": "Archives","searchFolders": "Folders",+ "searchFiles": "Files",+ "searchUntaggedEntries": "Untagged files or folders","searchSubFolder": "Search in subfolders","file": "File","newFileNote": "New File / Note",diff --git a/app/services/search.js b/app/services/search.jsindex d20313c81..81db0d2d1 100644--- a/app/services/search.js+++ b/app/services/search.js@@ -38,6 +38,8 @@ export const FileTypeGroups = {video: ['ogv', 'mp4', 'avi', 'webm', 'mkv'],archives: ['zip', 'rar', 'gz', 'tgz', 'arc', '7z'],folders: ['folders'],+ files: ['files'],+ untagged: ['untagged'],};export type SearchQuery = {@@ -99,6 +101,9 @@ const fuseOptions = {}, {name: 'tags',weight: 0.3+ }, {+ name: 'path', // TODO ignore .ts folder, should not be in the index+ weight: 0.1}]};@@ -107,7 +112,7 @@ function constructTagQuery(searchQuery: SearchQuery): string {if (searchQuery.tags && searchQuery.tags.length >= 1) {tagQuery = 'tags[? (';searchQuery.tags.map(tag => {- const cleanedTag = tag.trim();+ const cleanedTag = tag.trim(); // .toLowerCase();if (cleanedTag.length > 0) {tagQuery += 'title==\'' + cleanedTag + '\' || ';}@@ -120,41 +125,54 @@ function constructTagQuery(searchQuery: SearchQuery): string {return tagQuery;}+function constructFuseQuery(searchQuery: SearchQuery): string {+ if (!searchQuery) return '';+ let fuseQuery = (searchQuery.textQuery) ? searchQuery.textQuery : '';+ if (searchQuery.tags && searchQuery.tags.length >= 1) {+ searchQuery.tags.map(tag => {+ const cleanedTag = tag.trim().toLowerCase();+ fuseQuery = fuseQuery + ' ' + cleanedTag;+ return true;+ });+ }+ return fuseQuery;+}+export default class Search {static searchLocationIndex(locationContent: Array<Object>, searchQuery: SearchQuery) {// let result = jmespath.search({ index: locationContent }, "index[?contains(name, '" + searchQuery.textQuery + "')]");// ?tags[?title=='todo' || title=='high'// index[?extension=='png' || extension=='jpg']+ let results;+const extensionQuery = Pro ? Pro.Search.constructFileTypeQuery(searchQuery) : '';const tagQuery = Pro ? Pro.Search.constructTagQuery(searchQuery) : constructTagQuery(searchQuery);-- let jmespathResults = locationContent;- if (extensionQuery.length > 1 && tagQuery.length > 1) {- const jmespathQuery = 'index[? ' + extensionQuery + ' && ' + tagQuery + ']';- console.log('jmespathQuery: ' + jmespathQuery);- jmespathResults = jmespath.search({ index: locationContent }, jmespathQuery);- } else if (extensionQuery.length <= 1 && tagQuery.length > 1) {- const jmespathQuery = 'index[? ' + tagQuery + ']';- console.log('jmespathQuery: ' + jmespathQuery);- jmespathResults = jmespath.search({ index: locationContent }, jmespathQuery);- } else if (extensionQuery.length > 1 && tagQuery.length <= 1) {- const jmespathQuery = 'index[? ' + extensionQuery + ']';- console.log('jmespathQuery: ' + jmespathQuery);- jmespathResults = jmespath.search({ index: locationContent }, jmespathQuery);+ let jmespathQuery;++ if (extensionQuery.length > 1 && tagQuery.length > 1) { // extension query and tags available+ jmespathQuery = 'index[? ' + extensionQuery + ' && ' + tagQuery + ']';+ } else if (extensionQuery.length <= 1 && tagQuery.length > 1) { // only tags+ jmespathQuery = 'index[? ' + tagQuery + ']';+ } else if (extensionQuery.length > 1 && tagQuery.length <= 1) { // only extension query+ jmespathQuery = 'index[? ' + extensionQuery + ']';+ }+ if (jmespathQuery) {+ console.log('jmespath query: ' + jmespathQuery);+ console.time('jmespath');+ results = jmespath.search({ index: results || locationContent }, jmespathQuery);+ console.timeEnd('jmespath');}- // console.log('JMES found: ' + jmespathResults.length);+ const fuseQuery = constructFuseQuery(searchQuery);+ console.log('fuse query: ' + fuseQuery);+ console.time('fuse');+ const fuse = new Fuse(results || locationContent, fuseOptions);+ results = fuse.search(fuseQuery);+ console.timeEnd('fuse');- let results;- if (searchQuery.textQuery && searchQuery.textQuery.length > 0) { // and fuse enabled- const fuse = new Fuse(jmespathResults, fuseOptions);- results = fuse.search(searchQuery.textQuery);- } else {- results = jmespathResults;- }- // console.log('Fuse found: ' + results.length);+ console.log('Results found: ' + results.length);if (results.length >= AppConfig.maxSearchResult) {results = results.slice(0, AppConfig.maxSearchResult);}