Enable advanced search with query_string

This commit is contained in:
simon987 2021-05-06 20:07:20 -04:00
parent fc7f30d670
commit 523c123e2e
5 changed files with 81 additions and 22 deletions

View File

@ -511,8 +511,8 @@ function search(after = null) {
searchResults.appendChild(preload); searchResults.appendChild(preload);
} }
let query = searchBar.value; let searchBarValue = searchBar.value;
let empty = query === ""; let empty = searchBarValue === "";
let condition = empty ? "should" : "must"; let condition = empty ? "should" : "must";
let filters = [ let filters = [
{range: {size: {gte: size_min, lte: size_max}}}, {range: {size: {gte: size_min, lte: size_max}}},
@ -561,19 +561,32 @@ function search(after = null) {
filters.push({range: {mtime: {lte: date_max}}}) 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 = { let q = {
"_source": { "_source": {
excludes: ["content", "_tie"] excludes: ["content", "_tie"]
}, },
query: { query: {
bool: { bool: {
[condition]: { [condition]: query,
simple_query_string: {
query: query,
fields: fields,
default_operator: "and"
}
},
filter: filters 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"]; let hits = searchResult["hits"]["hits"];
if (hits) { if (hits) {
lastDoc = hits[hits.length - 1]; lastDoc = hits[hits.length - 1];
@ -645,7 +660,25 @@ function search(after = null) {
reachedEnd = hits.length !== SIZE; reachedEnd = hits.length !== SIZE;
insertHits(resultContainer, hits); insertHits(resultContainer, hits);
searchBusy = false; 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
});
})
} }

View File

@ -70,7 +70,7 @@ function strUnescape(str) {
for (let i = 0; i < str.length; i++) { for (let i = 0; i < str.length; i++) {
const c = str[i]; const c = str[i];
const next = str[i+1]; const next = str[i + 1];
if (c === ']') { if (c === ']') {
if (next === ']') { if (next === ']') {
@ -102,7 +102,8 @@ const _defaults = {
treemapSize: "large", treemapSize: "large",
suggestPath: true, suggestPath: true,
fragmentSize: 100, fragmentSize: 100,
columns: 5 columns: 5,
queryMode: "simple"
}; };
function loadSettings() { function loadSettings() {
@ -120,6 +121,7 @@ function loadSettings() {
$("#settingSuggestPath").prop("checked", CONF.options.suggestPath); $("#settingSuggestPath").prop("checked", CONF.options.suggestPath);
$("#settingFragmentSize").val(CONF.options.fragmentSize); $("#settingFragmentSize").val(CONF.options.fragmentSize);
$("#settingColumns").val(CONF.options.columns); $("#settingColumns").val(CONF.options.columns);
$("#settingQueryMode").val(CONF.options.queryMode);
} }
function Settings() { function Settings() {
@ -127,6 +129,7 @@ function Settings() {
this._onUpdate = function () { this._onUpdate = function () {
$("#fuzzyToggle").prop("checked", this.options.fuzzy); $("#fuzzyToggle").prop("checked", this.options.fuzzy);
$("#searchBar").attr("placeholder", this.options.queryMode === "simple" ? "Search" : "Advanced search");
updateColumnStyle(); updateColumnStyle();
}; };
@ -165,6 +168,7 @@ function updateSettings() {
CONF.options.suggestPath = $("#settingSuggestPath").prop("checked"); CONF.options.suggestPath = $("#settingSuggestPath").prop("checked");
CONF.options.fragmentSize = $("#settingFragmentSize").val(); CONF.options.fragmentSize = $("#settingFragmentSize").val();
CONF.options.columns = $("#settingColumns").val(); CONF.options.columns = $("#settingColumns").val();
CONF.options.queryMode = $("#settingQueryMode").val();
CONF.save(); CONF.save();
if (typeof searchDebounced !== "undefined") { 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({ return jQuery.ajax({
url: url, url: url,
type: "post", type: "post",
data: JSON.stringify(data), data: JSON.stringify(data),
contentType: "application/json" contentType: "application/json"
}).fail(err => { }).fail(err => {
showEsError(); if (showError) {
showEsError();
}
console.log(err); console.log(err);
}); });
}; };
@ -212,7 +218,7 @@ function updateColumnStyle() {
const style = document.getElementById("style"); const style = document.getElementById("style");
if (style) { if (style) {
style.innerHTML = style.innerHTML =
` `
@media screen and (min-width: 1500px) { @media screen and (min-width: 1500px) {
.container { .container {
max-width: 1440px; max-width: 1440px;

View File

@ -120,6 +120,8 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<h2>Simple search</h2>
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> <tr>
@ -168,6 +170,12 @@
<p>For more information, see <a target="_blank" <p>For more information, see <a target="_blank"
href="//www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html">Elasticsearch href="//www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html">Elasticsearch
documentation</a></p> documentation</a></p>
<h2>Advanced search</h2>
<p>For documentation about the advanced search mode, see <a target="_blank"
href="//www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax">Elasticsearch
documentation</a></p>
</div> </div>
</div> </div>
</div> </div>
@ -207,10 +215,16 @@
<br/> <br/>
<div class="form-group"> <div class="form-group">
<input type="number" class="form-control" id="settingFragmentSize">
<label for="settingFragmentSize">Highlight context size in characters</label> <label for="settingFragmentSize">Highlight context size in characters</label>
<input type="number" class="form-control" id="settingFragmentSize">
</div> </div>
<label for="settingQueryMode">Search mode</label>
<select id="settingQueryMode" class="form-control form-control-sm">
<option value="simple">Simple</option>
<option value="advanced">Advanced</option>
</select>
<label for="settingDisplay">Display</label> <label for="settingDisplay">Display</label>
<select id="settingDisplay" class="form-control form-control-sm"> <select id="settingDisplay" class="form-control form-control-sm">
<option value="grid">Grid</option> <option value="grid">Grid</option>

View File

@ -84,10 +84,16 @@
<br/> <br/>
<div class="form-group"> <div class="form-group">
<input type="number" class="form-control" id="settingFragmentSize">
<label for="settingFragmentSize">Highlight context size in characters</label> <label for="settingFragmentSize">Highlight context size in characters</label>
<input type="number" class="form-control" id="settingFragmentSize">
</div> </div>
<label for="settingQueryMode">Search mode</label>
<select id="settingQueryMode" class="form-control form-control-sm">
<option value="simple">Simple</option>
<option value="advanced">Advanced</option>
</select>
<label for="settingDisplay">Display</label> <label for="settingDisplay">Display</label>
<select id="settingDisplay" class="form-control form-control-sm"> <select id="settingDisplay" class="form-control form-control-sm">
<option value="grid">Grid</option> <option value="grid">Grid</option>

File diff suppressed because one or more lines are too long