Commit 6cf300ea authored by Iker Hurtado's avatar Iker Hurtado
Browse files

Complex search logic implementation (4)

parent ded21d0f
......@@ -7708,8 +7708,9 @@
</div>
<div class="bool-buttons" style="width: 28%; display: inline-block" >
OR <span id="and-or-switch" ></span> AND
<button disabled >NOT</button>
<button disabled >(</button> <button disabled >)</button>
<button >NOT</button>
<button class="open-parentheses" >(</button>
<button class="close-parentheses">)</button>
<!--<input type="checkbox" name="and-or" class="not-symbol-btn" />NOT-->
</div>
</div>
......@@ -7751,6 +7752,15 @@
this.currentOperator = ( e ? 'AND' : 'OR');
});
this.openParenthButton = this.element.querySelector('.open-parentheses');
this.closeParenthButton = this.element.querySelector('.close-parentheses');
this.openParenthButton.addEventListener( 'click', e => {
this._addParenthesesInSearchQuery(true);
});
this.closeParenthButton.addEventListener( 'click', e => {
this._addParenthesesInSearchQuery(false);
});
this.elementTable= new ElementTable();
this.elementTable.setClickListener(elementArray => {
this.addElementsInSearchQuery(elementArray);
......@@ -7815,7 +7825,8 @@
//if (this.queryTypes[i] === 'MN'){
//queryObj.push(this._getESSimpleMatch('material_name', item));
let rootQueryObj = this._getESQueryFromSearchQuery();
let rootQueryObj =
this._getESQueryFromSearchQuery(this.searchQuery, this.queryTypes);
/* Pending to ADD *************/
//let filterMap = this.filterPanel.getValues();
......@@ -7903,46 +7914,71 @@
}
_getESQueryFromSearchQuery(){
_getESQueryFromSearchQuery(searchQuery, queryTypes){
// Query structure analysis - looking for parentheses (only one level supported)
let openIndex = -1;
let nestedQueries = []; // Queries into parentheses
let esQueries = [];
searchQuery.forEach( (item, i) => {
if ( searchQuery[i] === '(' ) openIndex = i;
else if ( openIndex >= 0 && searchQuery[i] === ')' ){
//nestedQueries.push(searchQuery.slice(openIndex+1, i));
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
esQueries.push(this._getESQueryFromSearchQuery(
searchQuery.slice(openIndex+1, i), queryTypes.slice(openIndex+1, i)));
}
});
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
if ( esQueries.length > 0){ // Recursion
console.log('esQueries', JSON.stringify(esQueries));
this.searchQuery.forEach( (item, i) => {
}else{ // BASE CASE: there is no parentheses
if (this.queryTypes[i] === 'F'){
let boolOperator;
searchQuery.forEach( (item, i) => {
if (searchQuery[i] === 'AND' || searchQuery[i] === 'OR')
boolOperator = item;
});
let esMatchQuery;
// *reduced* search if (this.formulaBox.getMultiplesOfFormula()){
//esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item), true);
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'F'){
let esMatchQuery;
// *reduced* search if (this.formulaBox.getMultiplesOfFormula()){
//esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item), true);
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
}else if (queryTypes[i] === 'E'){
// if (this.elementTable.getAllowOtherElements())
queryObj.push(this._getESSimpleMatch('atom_labels', item));
//else // Regular search, Not allowing other elements -> the elements inserted must be sorted for this to work
// queryObj.bool.must.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
}
});
return rootQueryObj;
}
}else if (this.queryTypes[i] === 'E'){
// if (this.elementTable.getAllowOtherElements())
queryObj.push(this._getESSimpleMatch('atom_labels', item));
//else // Regular search, Not allowing other elements -> the elements inserted must be sorted for this to work
// queryObj.bool.must.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
}
});
return rootQueryObj;
}
......@@ -8053,7 +8089,8 @@
// If the it's an element and is already in the query it's not inserted
if (type === 'E' && this.searchQuery.indexOf(tag) >= 0) return;
if ( this.searchQuery.length > 0 )
if ( this.searchQuery.length > 0
&& this.searchQuery[this.searchQuery.length-1] !== '(')
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery(tag, type);
this.updateSearchQuery();
......@@ -8069,6 +8106,16 @@
return true;
}
_addParenthesesInSearchQuery(isOpen){
if ( this.searchQuery.length > 0 && isOpen)
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery( (isOpen ? '(' : ')'), 'S');
this.updateSearchQuery();
//this._showSearchBox();
}
/*
addPropertiesInSearchQuery(propsMap){
propsMap.forEach((values, propName) => {
......
......@@ -362,10 +362,13 @@ div.title span.unfolded::before{
}
.bool-buttons button{
border: 1px solid #BBB;
color: #BBB;
cursor: inherit;
color: #777;
border: 1px solid #777;
background-color: #EEE;
text-align: center;
}
......
......@@ -101,8 +101,9 @@ class NewSearchMod {
</div>
<div class="bool-buttons" style="width: 28%; display: inline-block" >
OR <span id="and-or-switch" ></span> AND
<button disabled >NOT</button>
<button disabled >(</button> <button disabled >)</button>
<button >NOT</button>
<button class="open-parentheses" >(</button>
<button class="close-parentheses">)</button>
<!--<input type="checkbox" name="and-or" class="not-symbol-btn" />NOT-->
</div>
</div>
......@@ -144,6 +145,15 @@ class NewSearchMod {
this.currentOperator = ( e ? 'AND' : 'OR');
});
this.openParenthButton = this.element.querySelector('.open-parentheses');
this.closeParenthButton = this.element.querySelector('.close-parentheses');
this.openParenthButton.addEventListener( 'click', e => {
this._addParenthesesInSearchQuery(true);
});
this.closeParenthButton.addEventListener( 'click', e => {
this._addParenthesesInSearchQuery(false);
});
this.elementTable= new ElementTable();
this.elementTable.setClickListener(elementArray => {
this.addElementsInSearchQuery(elementArray);
......@@ -208,7 +218,8 @@ class NewSearchMod {
//if (this.queryTypes[i] === 'MN'){
//queryObj.push(this._getESSimpleMatch('material_name', item));
let rootQueryObj = this._getESQueryFromSearchQuery();
let rootQueryObj =
this._getESQueryFromSearchQuery(this.searchQuery, this.queryTypes);
/* Pending to ADD *************/
//let filterMap = this.filterPanel.getValues();
......@@ -296,46 +307,71 @@ class NewSearchMod {
}
_getESQueryFromSearchQuery(){
_getESQueryFromSearchQuery(searchQuery, queryTypes){
// Query structure analysis - looking for parentheses (only one level supported)
let openIndex = -1;
let nestedQueries = []; // Queries into parentheses
let esQueries = [];
searchQuery.forEach( (item, i) => {
if ( searchQuery[i] === '(' ) openIndex = i;
else if ( openIndex >= 0 && searchQuery[i] === ')' ){
//nestedQueries.push(searchQuery.slice(openIndex+1, i));
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
esQueries.push(this._getESQueryFromSearchQuery(
searchQuery.slice(openIndex+1, i), queryTypes.slice(openIndex+1, i)));
}
});
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
if ( esQueries.length > 0){ // Recursion
console.log('esQueries', JSON.stringify(esQueries));
this.searchQuery.forEach( (item, i) => {
}else{ // BASE CASE: there is no parentheses
if (this.queryTypes[i] === 'F'){
let boolOperator;
searchQuery.forEach( (item, i) => {
if (searchQuery[i] === 'AND' || searchQuery[i] === 'OR')
boolOperator = item;
});
let esMatchQuery;
// *reduced* search if (this.formulaBox.getMultiplesOfFormula()){
//esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item), true);
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'F'){
let esMatchQuery;
// *reduced* search if (this.formulaBox.getMultiplesOfFormula()){
//esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item), true);
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
}else if (queryTypes[i] === 'E'){
// if (this.elementTable.getAllowOtherElements())
queryObj.push(this._getESSimpleMatch('atom_labels', item));
//else // Regular search, Not allowing other elements -> the elements inserted must be sorted for this to work
// queryObj.bool.must.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
}
});
return rootQueryObj;
}
}else if (this.queryTypes[i] === 'E'){
// if (this.elementTable.getAllowOtherElements())
queryObj.push(this._getESSimpleMatch('atom_labels', item));
//else // Regular search, Not allowing other elements -> the elements inserted must be sorted for this to work
// queryObj.bool.must.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
}
});
return rootQueryObj;
}
......@@ -446,7 +482,8 @@ class NewSearchMod {
// If the it's an element and is already in the query it's not inserted
if (type === 'E' && this.searchQuery.indexOf(tag) >= 0) return;
if ( this.searchQuery.length > 0 )
if ( this.searchQuery.length > 0
&& this.searchQuery[this.searchQuery.length-1] !== '(')
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery(tag, type);
this.updateSearchQuery();
......@@ -462,6 +499,16 @@ class NewSearchMod {
return true;
}
_addParenthesesInSearchQuery(isOpen){
if ( this.searchQuery.length > 0 && isOpen)
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery( (isOpen ? '(' : ')'), 'S');
this.updateSearchQuery();
//this._showSearchBox();
}
/*
addPropertiesInSearchQuery(propsMap){
propsMap.forEach((values, propName) => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment