Querying through search much improved.

master
Sander Vocke 6 years ago
parent fde1c94bc9
commit 9601aa35f8
  1. 4
      src/browser.js
  2. 55
      src/queries.js

@ -8,7 +8,7 @@ import ExpandLess from '@material-ui/icons/ExpandLess';
import ExpandMore from '@material-ui/icons/ExpandMore'; import ExpandMore from '@material-ui/icons/ExpandMore';
import Collapse from '@material-ui/core/Collapse'; import Collapse from '@material-ui/core/Collapse';
import { user_query_from_album_path } from './queries'; import { user_query_from_browsed_album } from './queries';
class AlbumTreeItem { class AlbumTreeItem {
name = false; name = false;
@ -95,7 +95,7 @@ export function AlbumListItem(props) {
const handleClick = () => { const handleClick = () => {
if (onNewQuery) { if (onNewQuery) {
var query = user_query_from_album_path(album.relative_path); var query = user_query_from_browsed_album(album.relative_path);
onNewQuery(query); onNewQuery(query);
} }
} }

@ -6,6 +6,7 @@ export function escape_regex(s) {
}; };
export function do_image_query(query, database, collection_path, collection_thumbs_path) { export function do_image_query(query, database, collection_path, collection_thumbs_path) {
console.log("Doing image query:", query);
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var queries = []; var queries = [];
queries.push(query); queries.push(query);
@ -42,7 +43,7 @@ export function do_album_query(query, database) {
export const MatchTypeEnum = { export const MatchTypeEnum = {
MATCH_EQUALS: 1, MATCH_EQUALS: 1,
MATCH_REGEXP: 2, MATCH_REGEXP_CASEINSENSITIVE: 2,
}; };
export const MatchAgainstEnum = { export const MatchAgainstEnum = {
@ -96,16 +97,20 @@ export class MatchingFilter extends ResultFilter {
to_sql_where() { to_sql_where() {
var match_type_str = false; var match_type_str = false;
var match_against = this.match_against;
var match_from = this.match_from;
if (this.match_type == MatchTypeEnum.MATCH_EQUALS) { if (this.match_type == MatchTypeEnum.MATCH_EQUALS) {
match_type_str = "="; match_type_str = "=";
} else if (this.match_type == MatchTypeEnum.MATCH_REGEXP) { } else if (this.match_type == MatchTypeEnum.MATCH_REGEXP_CASEINSENSITIVE) {
match_type_str = " REGEXP "; match_type_str = " REGEXP ";
match_from = "LOWER(" + match_from + ")";
match_against = "LOWER(" + match_against + ")";
} else { } else {
throw new Error('Unsupported match type: ' + this.match_type); throw new Error('Unsupported match type: ' + this.match_type);
} }
return "(" + (this.negate ? "NOT " : "") + this.match_against + match_type_str + this.match_from + ")"; return "(" + (this.negate ? "NOT " : "") + match_against + match_type_str + match_from + ")";
} }
} }
@ -167,22 +172,42 @@ export function maybe_album_query(user_query) {
} }
function filter_from_text_segment(result_type, segment) { function filter_from_text_segment(result_type, segment) {
var match_type = false; var filter = false;
var match_text = false;
var match_against = false;
if (result_type == ResultTypeEnum.IMAGE) { if (result_type == ResultTypeEnum.IMAGE) {
match_type = MatchTypeEnum.MATCH_EQUALS; // Option 1: match on image name
match_text = '"' + segment['text'] + '"'; var name_filter = new MatchingFilter(
match_against = "Images.name"; result_type,
"Images.name",
'"' + '.*' + escape_regex(segment['text']) + '.*' + '"',
MatchTypeEnum.MATCH_REGEXP_CASEINSENSITIVE,
segment['negated']
);
// Option 2: match on album path
var album_filter = new MatchingFilter(
result_type,
"Albums.relativePath",
'"' + escape_regex(segment['text']) + '(\/[^\/]+)*' + '"',
MatchTypeEnum.MATCH_REGEXP_CASEINSENSITIVE,
segment['negated']
);
filter = new LogicalOperatorFilter(result_type, name_filter, album_filter, LogicalOperatorEnum.OR);
} else if (result_type == ResultTypeEnum.ALBUM) { } else if (result_type == ResultTypeEnum.ALBUM) {
// Match against the album "name", which is the basename of its relative path. // Match against the album "name", which is the basename of its relative path.
match_type = MatchTypeEnum.MATCH_REGEXP; // TODO is it nicer to match on relative path?
match_text = '"' + '\/(.*\/)*' + escape_regex(segment['text']) + '"'; var name_filter = new MatchingFilter(
match_against = "Albums.relativePath"; result_type,
"Albums.relativePath",
'"' + '\/(.*\/)*' + escape_regex(segment['text']) + '"',
MatchTypeEnum.MATCH_REGEXP_CASEINSENSITIVE,
segment['negated']
);
filter = name_filter;
} }
return new MatchingFilter(result_type, match_against, match_text, match_type, segment['negated']); return filter;
} }
export function user_query_from_search_string(search_string) { export function user_query_from_search_string(search_string) {
@ -205,9 +230,9 @@ export function user_query_from_search_string(search_string) {
return r; return r;
} }
export function user_query_from_album_path(album_path) { export function user_query_from_browsed_album(album_path) {
var r = new UserQuery(); var r = new UserQuery();
var match_type = MatchTypeEnum.MATCH_REGEXP; var match_type = MatchTypeEnum.MATCH_REGEXP_CASEINSENSITIVE;
var match_text = '"' + escape_regex(album_path) + '(\/[^\/]+)*' + '"'; var match_text = '"' + escape_regex(album_path) + '(\/[^\/]+)*' + '"';
var match_against = "Albums.relativePath"; var match_against = "Albums.relativePath";
r.image_filter = new MatchingFilter(ResultTypeEnum.ALBUM, match_against, match_text, match_type, false); r.image_filter = new MatchingFilter(ResultTypeEnum.ALBUM, match_against, match_text, match_type, false);

Loading…
Cancel
Save