Skip to content

Commit

Permalink
allow combining tokens in text and title/attributes, fixes #2820
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Apr 29, 2022
1 parent 70edd9a commit f705c43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/services/search/expressions/note_content_protected_fulltext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const utils = require("../../utils");

// FIXME: create common subclass with NoteContentUnprotectedFulltextExp to avoid duplication
class NoteContentProtectedFulltextExp extends Expression {
constructor(operator, tokens, raw) {
constructor(operator, {tokens, raw, flatText}) {
super();

if (operator !== '*=*') {
Expand All @@ -19,6 +19,7 @@ class NoteContentProtectedFulltextExp extends Expression {

this.tokens = tokens;
this.raw = !!raw;
this.flatText = !!flatText;
}

execute(inputNoteSet) {
Expand Down Expand Up @@ -49,7 +50,17 @@ class NoteContentProtectedFulltextExp extends Expression {

content = this.preprocessContent(content, type, mime);

if (!this.tokens.find(token => !content.includes(token))) {
const nonMatchingToken = this.tokens.find(token =>
!content.includes(token) &&
(
// in case of default fulltext search we should consider both title, attrs and content
// so e.g. "hello world" should match when "hello" is in title and "world" in content
!this.flatText
|| !becca.notes[noteId].getFlatText().includes(token)
)
);

if (!nonMatchingToken) {
resultNoteSet.add(becca.notes[noteId]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const utils = require("../../utils");

// FIXME: create common subclass with NoteContentProtectedFulltextExp to avoid duplication
class NoteContentUnprotectedFulltextExp extends Expression {
constructor(operator, tokens, raw) {
constructor(operator, {tokens, raw, flatText}) {
super();

if (operator !== '*=*') {
Expand All @@ -17,6 +17,7 @@ class NoteContentUnprotectedFulltextExp extends Expression {

this.tokens = tokens;
this.raw = !!raw;
this.flatText = !!flatText;
}

execute(inputNoteSet) {
Expand All @@ -35,7 +36,17 @@ class NoteContentUnprotectedFulltextExp extends Expression {

content = this.preprocessContent(content, type, mime);

if (!this.tokens.find(token => !content.includes(token))) {
const nonMatchingToken = this.tokens.find(token =>
!content.includes(token) &&
(
// in case of default fulltext search we should consider both title, attrs and content
// so e.g. "hello world" should match when "hello" is in title and "world" in content
!this.flatText
|| !becca.notes[noteId].getFlatText().includes(token)
)
);

if (!nonMatchingToken) {
resultNoteSet.add(becca.notes[noteId]);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/services/search/services/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function getFulltext(tokens, searchContext) {
if (!searchContext.fastSearch) {
return new OrExp([
new NoteFlatTextExp(tokens),
new NoteContentProtectedFulltextExp('*=*', tokens),
new NoteContentUnprotectedFulltextExp('*=*', tokens)
new NoteContentProtectedFulltextExp('*=*', {tokens, flatText: true}),
new NoteContentUnprotectedFulltextExp('*=*', {tokens, flatText: true})
]);
}
else {
Expand Down Expand Up @@ -141,8 +141,8 @@ function getExpression(tokens, searchContext, level = 0) {
i++;

return new OrExp([
new NoteContentUnprotectedFulltextExp(operator, [tokens[i].token], raw),
new NoteContentProtectedFulltextExp(operator, [tokens[i].token], raw)
new NoteContentUnprotectedFulltextExp(operator, {tokens: [tokens[i].token], raw }),
new NoteContentProtectedFulltextExp(operator, {tokens: [tokens[i].token], raw })
]);
}

Expand Down Expand Up @@ -196,8 +196,8 @@ function getExpression(tokens, searchContext, level = 0) {

return new OrExp([
new PropertyComparisonExp(searchContext, 'title', '*=*', tokens[i].token),
new NoteContentProtectedFulltextExp('*=*', [tokens[i].token]),
new NoteContentUnprotectedFulltextExp('*=*', [tokens[i].token])
new NoteContentProtectedFulltextExp('*=*', {tokens: [tokens[i].token]}),
new NoteContentUnprotectedFulltextExp('*=*', {tokens: [tokens[i].token]})
]);
}

Expand Down

0 comments on commit f705c43

Please sign in to comment.