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

Mobile: Fixes #8050: Fix complex queries that contain quotes or filters

This commit is contained in:
Laurent Cozic 2023-09-24 23:22:36 +01:00
parent d14b694b6c
commit 9f14e61aff
4 changed files with 45 additions and 7 deletions

View File

@ -6,7 +6,6 @@ import ScreenHeader from '../ScreenHeader';
const Icon = require('react-native-vector-icons/Ionicons').default;
import { _ } from '@joplin/lib/locale';
import Note from '@joplin/lib/models/Note';
import gotoAnythingStyleQuery from '@joplin/lib/services/searchengine/gotoAnythingStyleQuery';
const { NoteItem } = require('../note-item.js');
const { BaseScreenComponent } = require('../base-screen.js');
const { themeStyle } = require('../global-style.js');
@ -95,8 +94,6 @@ class SearchScreenComponent extends BaseScreenComponent {
public async refreshSearch(query: string = null) {
if (!this.props.visible) return;
query = gotoAnythingStyleQuery(query);
let notes = [];
if (query) {

View File

@ -501,4 +501,14 @@ describe('services_SearchEngine', () => {
expect((await engine.search('л')).length).toBe(1);
expect((await engine.search('л'))[0].id).toBe(n2.id);
}));
it('should automatically add wildcards', (async () => {
const n1 = await Note.save({ title: 'hello1' });
const n2 = await Note.save({ title: 'hello2' });
await engine.syncTables();
expect((await engine.search('hello')).length).toBe(0);
expect((await engine.search('hello', { appendWildCards: true })).length).toBe(2);
}));
});

View File

@ -20,6 +20,12 @@ enum SearchType {
interface SearchOptions {
searchType: SearchType;
// When this is on, the search engine automatically appends "*" to each word
// of the query. So "hello world" is turned into "hello* world*". This
// allows returning results quickly, in particular on mobile, and it seems
// to be what users generally expect.
appendWildCards?: boolean;
}
export interface ComplexTerm {
@ -598,6 +604,7 @@ export default class SearchEngine {
options = {
searchType: SearchEngine.SEARCH_TYPE_AUTO,
appendWildCards: false,
...options,
};
@ -618,6 +625,19 @@ export default class SearchEngine {
// when searching.
// https://github.com/laurent22/joplin/issues/1075#issuecomment-459258856
if (options.appendWildCards) {
parsedQuery.allTerms = parsedQuery.allTerms.map(t => {
if (t.name === 'text' && !t.wildcard) {
t = {
...t,
wildcard: true,
value: t.value.endsWith('"') ? `${t.value.substring(0, t.value.length - 1)}*"` : `${t.value}*`,
};
}
return t;
});
}
const useFts = searchType === SearchEngine.SEARCH_TYPE_FTS;
try {
const { query, params } = queryBuilder(parsedQuery.allTerms, useFts);

View File

@ -2,9 +2,17 @@ import SearchEngine from './SearchEngine';
import Note from '../../models/Note';
import Setting from '../../models/Setting';
export interface NotesForQueryOptions {
fields?: string[];
appendWildCards?: boolean;
}
export default class SearchEngineUtils {
public static async notesForQuery(query: string, applyUserSettings: boolean, options: any = null, searchEngine: SearchEngine = null) {
if (!options) options = {};
public static async notesForQuery(query: string, applyUserSettings: boolean, options: NotesForQueryOptions = null, searchEngine: SearchEngine = null) {
options = {
appendWildCards: false,
...options,
};
if (!searchEngine) {
searchEngine = SearchEngine.instance();
@ -16,7 +24,10 @@ export default class SearchEngineUtils {
searchType = SearchEngine.SEARCH_TYPE_BASIC;
}
const results = await searchEngine.search(query, { searchType });
const results = await searchEngine.search(query, {
searchType,
appendWildCards: options.appendWildCards,
});
const noteIds = results.map((n: any) => n.id);
@ -44,7 +55,7 @@ export default class SearchEngineUtils {
todoCompletedAutoAdded = true;
}
const previewOptions = { order: [],
const previewOptions: any = { order: [],
fields: fields,
conditions: [`id IN ("${noteIds.join('","')}")`], ...options };