Skip to content

Commit

Permalink
for testing: #20
Browse files Browse the repository at this point in the history
  • Loading branch information
moisbo committed Oct 27, 2023
1 parent 9b3ece6 commit e5e7d66
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
67 changes: 42 additions & 25 deletions portal/src/components/SearchAdvanced.component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,38 @@
<ul class="px-2 list-disc list-inside">
<li class="px-3 py-1">The query string is parsed into a series of terms and operators.</li>
<li class="px-3 py-1">In general, the search functions are NOT case sensitive.</li>
<li class="px-3 py-1">A term can be a single word -- 'quick' or 'brown' -- or a phrase, surrounded by double quotes -- "quick brown" -- which searches for all the words in the phrase, in the same order. NB: In the Basic Search box, multi-word expressions are treated as being linked by OR regardless of whether they have quote marks around them.</li>
<li class="px-3 py-1">Wildcard searches can be run on terms consisting of a single word, using ? to replace a single character, and * to replace zero or more characters. Wildcards cannot be included in a phrase search.</li>
<li class="px-3 py-1">Regular expression patterns can be embedded in the query string by wrapping them in forward-slashes ("/")</li>
<li class="px-3 py-1">The reserved characters are: <code class="literal backdrop-blur">+ - = && || > &lt; ! ( ) { } [ ] ^ " ~ * ? : \ /</code></li>
<li class="px-3 py-1">Reserved characters should be escaped using a back-slash ("\"). Failing to escape these special characters correctly could lead to a syntax error which prevents your query from running. For example, to search for 'LGBTQ+', you would need to enter the string 'LGBTQ\+'.</li>
<li class="px-3 py-1">The familiar boolean operators AND, OR and NOT (also written &&, || and !) are also supported but beware that they do not honor the usual precedence rules, so parentheses should be used whenever multiple operators are used together. For instance, to search for files which contain both 'public' and 'house' or 'government' and 'house' or 'house' but not 'cottage', the query should be ((public AND house) OR (government AND house) OR house) AND NOT cottage</li>
<li class="px-3 py-1">If you search for the literal word AND, OR, and NOT they all should be escaped. eg. \OR. Note that this is a situation where the search is case sensitive: 'and' does not need to be escaped, but 'AND' does.</li>
<!-- <li class="px-3 py-1">Clicking on "Use Query String" will show you the actual search string used for your search. You can update your search string however it will not convert back to the search box</li>-->
<li class="px-3 py-1">A term can be a single word -- 'quick' or 'brown' -- or a phrase, surrounded by double
quotes -- "quick brown" -- which searches for all the words in the phrase, in the same order. NB: In the
Basic Search box, multi-word expressions are treated as being linked by OR regardless of whether they have
quote marks around them.
</li>
<li class="px-3 py-1">Wildcard searches can be run on terms consisting of a single word, using ? to replace a
single character, and * to replace zero or more characters. Wildcards cannot be included in a phrase search.
</li>
<li class="px-3 py-1">Regular expression patterns can be embedded in the query string by wrapping them in
forward-slashes ("/"). This search engine does not support full Perl-compatible regex syntax, for more see:
<a target="_blank" rel="noopener noreferrer" class="underline text-blue-300" href="https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html">RegExp Syntax</a>.
</li>
<li class="px-3 py-1">The reserved characters are: <code class="literal backdrop-blur">+ - = && || > &lt; ! (
) { } [ ] ^ " ~ * ? : \ /</code></li>
<li class="px-3 py-1">Reserved characters should be escaped using a back-slash ("\"). Failing to escape these
special characters correctly could lead to a syntax error which prevents your query from running. For
example, to search for 'LGBTQ+', you would need to enter the string 'LGBTQ\+'.
</li>
<!-- <li class="px-3 py-1">The familiar boolean operators AND, OR and NOT (also written &&, || and !) are also-->
<!-- supported but beware that they do not honor the usual precedence rules, so parentheses should be used-->
<!-- whenever multiple operators are used together. For instance, to search for files which contain both 'public'-->
<!-- and 'house' or 'government' and 'house' or 'house' but not 'cottage', the query should be ((public AND-->
<!-- house) OR (government AND house) OR house) AND NOT cottage-->
<!-- </li>-->
<!-- <li class="px-3 py-1">If you search for the literal word AND, OR, and NOT they all should be escaped. eg. \OR.-->
<!-- Note that this is a situation where the search is case sensitive: 'and' does not need to be escaped, but-->
<!-- 'AND' does.-->
<!-- </li>-->
<!-- <li class="px-3 py-1">Clicking on "Use Query String" will show you the actual search string used for your search. You can update your search string however it will not convert back to the search box</li>-->
</ul>
</el-row>
<el-row v-if="!useQueryString" class="px-2 pb-2" :gutter="10" v-for="(sg, index) in searchGroup" :key="index">
<el-row class="px-2 pb-2" :gutter="10" v-for="(sg, index) in searchGroup" :key="index">
<el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8" class="h-auto">
<el-select class="w-full m-2"
placeholder="Select a Field"
Expand Down Expand Up @@ -74,11 +95,11 @@
:rows="2"
type="textarea"
:autosize="true"
placeholder="name.@value: (market) AND name.@value: (forces)"
disabled
/>
</el-row>
<el-row class="p-2" :gutter="10" :justify="'space-between'">
<el-button-group v-if="!useQueryString">
<el-button-group>
<el-button @click="addNewLine"
class="cursor-pointer">
<font-awesome-icon icon="fa fa-plus"/>&nbsp;Add New Line
Expand All @@ -88,9 +109,13 @@
<font-awesome-icon icon="fa fa-rotate-left"/>&nbsp;Clear
</el-button>
</el-button-group>
<!-- <el-button @click="doUseQueryString">-->
<!-- {{ useQueryString ? 'Use Box Search' : 'Use Query String' }}-->
<!-- </el-button>-->
<el-tooltip class="box-item" effect="light" trigger="hover" content="This query string is what it is actually sent to the search engine, click search to update it"
placement="bottom-end">
<el-button @click="doUseQueryString()">
{{ useQueryString ? 'Hide Query' : 'Show Query' }}&nbsp;
<font-awesome-icon icon="fa-solid fa-circle-info"/>
</el-button>
</el-tooltip>
</el-row>
<el-row class="p-2" :gutter="10" :justify="'center'">
<el-button @click="advancedSearch"
Expand Down Expand Up @@ -145,14 +170,7 @@ export default {
methods: {
isEmpty,
advancedSearch() {
if (this.useQueryString) {
this.queries = {
queryString: this.textQueryString,
searchGroup: encodeURIComponent(JSON.stringify(this.searchGroup))
}
} else {
this.setQueryString();
}
this.setQueryString();
this.$emit('doAdvancedSearch', {queries: this.queries});
},
addNewLine() {
Expand Down Expand Up @@ -190,12 +208,11 @@ export default {
doUseQueryString() {
this.useQueryString = !this.useQueryString;
this.setQueryString();
this.textQueryString = this.queries.queryString;
},
setQueryString() {
let queryString = this.$elasticService.queryString(this.searchGroup);
this.textQueryString = this.$elasticService.queryString(this.searchGroup);
this.queries = {
queryString: queryString,
queryString: this.textQueryString,
searchGroup: encodeURIComponent(JSON.stringify(this.searchGroup))
}
}
Expand Down
18 changes: 11 additions & 7 deletions portal/src/elastic.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export default class ElasticService {
if (response.status !== 200) {
//httpService.checkAuthorised({status: response.status});
//TODO: Return an exact error from the API.
throw new Error('There was an error with your query');
const error = await response.json();
const msg = 'Query Error: ' + error?.message || 'There was an error with your query'
throw new Error(msg);
} else {
const results = await response.json();
//console.log(results);
Expand Down Expand Up @@ -198,6 +200,8 @@ export default class ElasticService {
const boolQuery = esb.boolQuery();
if (queries.queryString) {
boolQuery.must(esb.queryStringQuery(queries.queryString));
// boolQuery.must(esb.queryStringQuery(queries.queryString).escape(true));
//boolQuery.must(esb.simpleQueryStringQuery(queries.queryString));
} else {
//Note: this code below is never used. Delete
for (let q of queries) {
Expand Down Expand Up @@ -309,7 +313,7 @@ export default class ElasticService {
}

queryString(searchGroup) {
let queryString = '';
let qS = '';
searchGroup.forEach((sg, i) => {
let lastOneSG = false;
if (i + 1 === searchGroup.length) {
Expand All @@ -319,22 +323,22 @@ export default class ElasticService {
sg.searchInput = '*';
}
if (sg.field === 'all_fields') {
let qqq = '(';
let qqq = '( ';
Object.keys(this.fields).map((f, index, keys) => {
let lastOne = false;
if (index + 1 === keys.length) {
lastOne = true;
}
let qq = '';
qq = ` ${f} : ${sg.searchInput} ${!lastOne ? 'OR' : ''} `;
qq = String.raw`${f} : ${sg.searchInput} ${!lastOne ? 'OR' : ''} `;
qqq += qq;
});
queryString += ` ${qqq} ) ${!lastOneSG ? sg.operation : ''} `;
qS += String.raw`${qqq} ) ${!lastOneSG ? sg.operation : ''} `;
} else {
queryString += ` ( ${sg.field}: ( ${sg.searchInput} ) ) ${!lastOneSG ? sg.operation : ''}`;
qS += String.raw` ( ${sg.field}: ${sg.searchInput} ) ${!lastOneSG ? sg.operation : ''}`;
}
});
return queryString;
return qS;
}
}

Expand Down

0 comments on commit e5e7d66

Please sign in to comment.