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);
}
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
});
})
}

View File

@ -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 => {
if (showError) {
showEsError();
}
console.log(err);
});
};

View File

@ -120,6 +120,8 @@
</div>
<div class="modal-body">
<h2>Simple search</h2>
<table class="table">
<tbody>
<tr>
@ -168,6 +170,12 @@
<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
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>
@ -207,10 +215,16 @@
<br/>
<div class="form-group">
<input type="number" class="form-control" id="settingFragmentSize">
<label for="settingFragmentSize">Highlight context size in characters</label>
<input type="number" class="form-control" id="settingFragmentSize">
</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>
<select id="settingDisplay" class="form-control form-control-sm">
<option value="grid">Grid</option>

View File

@ -84,10 +84,16 @@
<br/>
<div class="form-group">
<input type="number" class="form-control" id="settingFragmentSize">
<label for="settingFragmentSize">Highlight context size in characters</label>
<input type="number" class="form-control" id="settingFragmentSize">
</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>
<select id="settingDisplay" class="form-control form-control-sm">
<option value="grid">Grid</option>

File diff suppressed because one or more lines are too long