From 523c123e2e997138381e3498501e75c47e26acdb Mon Sep 17 00:00:00 2001 From: simon987 Date: Thu, 6 May 2021 20:07:20 -0400 Subject: [PATCH] Enable advanced search with query_string --- src/static/js/search.js | 55 ++++++++++++++++++++++++++++++-------- src/static/js/util.js | 16 +++++++---- src/static/search.html | 16 ++++++++++- src/static/stats.html | 8 +++++- src/web/static_generated.c | 8 +++--- 5 files changed, 81 insertions(+), 22 deletions(-) diff --git a/src/static/js/search.js b/src/static/js/search.js index 34416f8..fb753b7 100644 --- a/src/static/js/search.js +++ b/src/static/js/search.js @@ -511,8 +511,8 @@ function search(after = null) { searchResults.appendChild(preload); } - let query = searchBar.value; - let empty = query === ""; + let searchBarValue = searchBar.value; + let empty = searchBarValue === ""; let condition = empty ? "should" : "must"; let filters = [ {range: {size: {gte: size_min, lte: size_max}}}, @@ -561,19 +561,32 @@ function search(after = null) { filters.push({range: {mtime: {lte: date_max}}}) } + let query; + if (CONF.options.queryMode === "simple") { + query = { + simple_query_string: { + query: searchBarValue, + fields: fields, + default_operator: "and" + } + } + } else { + query = { + query_string: { + query: searchBarValue, + default_field: "name", + default_operator: "and" + } + } + } + let q = { "_source": { excludes: ["content", "_tie"] }, query: { bool: { - [condition]: { - simple_query_string: { - query: query, - fields: fields, - default_operator: "and" - } - }, + [condition]: query, filter: filters } }, @@ -611,7 +624,9 @@ function search(after = null) { } } - $.jsonPost("es", q).then(searchResult => { + const showError = CONF.options.queryMode === "advanced"; + + $.jsonPost("es", q, showError).then(searchResult => { let hits = searchResult["hits"]["hits"]; if (hits) { lastDoc = hits[hits.length - 1]; @@ -645,7 +660,25 @@ function search(after = null) { reachedEnd = hits.length !== SIZE; insertHits(resultContainer, hits); searchBusy = false; - }); + }).fail(() => { + searchBusy = false; + if (!after) { + preload.remove(); + } + + console.log("QUERY:") + console.log(q) + $.toast({ + heading: "Query error", + text: "Could not parse or execute query, please check the Advanced search documentation. " + + "See server logs for more information.", + stack: false, + bgColor: "#FF8F00", + textColor: "#FFF3E0", + position: 'bottom-right', + hideAfter: false + }); + }) } diff --git a/src/static/js/util.js b/src/static/js/util.js index 8d3b30f..92ccd0e 100644 --- a/src/static/js/util.js +++ b/src/static/js/util.js @@ -70,7 +70,7 @@ function strUnescape(str) { for (let i = 0; i < str.length; i++) { const c = str[i]; - const next = str[i+1]; + const next = str[i + 1]; if (c === ']') { if (next === ']') { @@ -102,7 +102,8 @@ const _defaults = { treemapSize: "large", suggestPath: true, fragmentSize: 100, - columns: 5 + columns: 5, + queryMode: "simple" }; function loadSettings() { @@ -120,6 +121,7 @@ function loadSettings() { $("#settingSuggestPath").prop("checked", CONF.options.suggestPath); $("#settingFragmentSize").val(CONF.options.fragmentSize); $("#settingColumns").val(CONF.options.columns); + $("#settingQueryMode").val(CONF.options.queryMode); } function Settings() { @@ -127,6 +129,7 @@ function Settings() { this._onUpdate = function () { $("#fuzzyToggle").prop("checked", this.options.fuzzy); + $("#searchBar").attr("placeholder", this.options.queryMode === "simple" ? "Search" : "Advanced search"); updateColumnStyle(); }; @@ -165,6 +168,7 @@ function updateSettings() { CONF.options.suggestPath = $("#settingSuggestPath").prop("checked"); CONF.options.fragmentSize = $("#settingFragmentSize").val(); CONF.options.columns = $("#settingColumns").val(); + CONF.options.queryMode = $("#settingQueryMode").val(); CONF.save(); if (typeof searchDebounced !== "undefined") { @@ -187,14 +191,16 @@ function updateSettings() { }); } -jQuery["jsonPost"] = function (url, data) { +jQuery["jsonPost"] = function (url, data, showError = true) { return jQuery.ajax({ url: url, type: "post", data: JSON.stringify(data), contentType: "application/json" }).fail(err => { - showEsError(); + if (showError) { + showEsError(); + } console.log(err); }); }; @@ -212,7 +218,7 @@ function updateColumnStyle() { const style = document.getElementById("style"); if (style) { style.innerHTML = - ` + ` @media screen and (min-width: 1500px) { .container { max-width: 1440px; diff --git a/src/static/search.html b/src/static/search.html index b675a76..772c53e 100644 --- a/src/static/search.html +++ b/src/static/search.html @@ -120,6 +120,8 @@