Commit 3dcfa515 authored by Iker Hurtado's avatar Iker Hurtado
Browse files

Complex search logic implementation (2)

parent 4f332b41
......@@ -7663,6 +7663,17 @@
class NewSearchMod {
constructor() {
this.userGuidance = true; // can enabled/disabled
//this.showingSearchBox = false;
this.searchQuery = [];
this.queryTypes = []; //**** Types associated to query elements
// Types: element (E), formula (F), symbol (S) and prop names
this.searchFilters = [];
this.currentOperator = 'AND';
this.element = document.createElement('div');
this.element.setAttribute("id",'search-module');
this.element.innerHTML=
......@@ -7736,6 +7747,9 @@
let andOrSwitch = new SwitchComponent(util.IMAGE_DIR+'switch');
this.element.querySelector('#and-or-switch').appendChild(andOrSwitch.element);
andOrSwitch.setListener( e => {
this.currentOperator = ( e ? 'AND' : 'OR');
});
this.elementTable= new ElementTable();
this.elementTable.setClickListener(elementArray => {
......@@ -7749,8 +7763,8 @@
this.formulaBox.setAddFormulaListener(formula => {
if (formula.trim() !== ''){
this.addTagInSearchQuery(formula, 'F');
//this.formulaBox.disable(true);
this.addMatNameButton.disabled = true;
this.formulaBox.disable(true);
this.materialNameBox.disable(true);
}
});
......@@ -7766,8 +7780,6 @@
this.materialNameBox.disable(true);
}
});
/*
this.propertiesBox= new PropertiesBox();
this.propertiesBox.setAddPropertiesListener(propsMap => {
......@@ -7784,13 +7796,7 @@
this.currentTab = 'element';
this.addPanel.appendChild(this.elementTable.element);
this.userGuidance = true; // can enabled/disabled
//this.showingSearchBox = false;
this.searchQuery = [];
this.queryTypes = []; //**** Types associated to query elements
// Types: element (E), formula (F), symbol (S) and prop names
this.searchFilters = [];
this._events();
}
......@@ -7804,96 +7810,63 @@
if (this.searchQuery.lenght === 0){
util.showUserMsg('No query');
}else{
let queryObj = { 'bool' : {} };
//queryObj.bool.must = {};
queryObj.bool.filter = [];
let elements = [];
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
});
if (this.queryTypes[i] === 'F'){
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
//queryObj.bool.must = [];
//queryObj.bool.filter = [];
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'F'){
//if (this.elementTable.getAllowOtherElements())
//else
let esMatchQuery;
if (this.formulaBox.getMultiplesOfFormula()){
esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
}else
esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.bool.filter.push(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 (this.queryTypes[i] === 'MN'){
queryObj.bool.filter.push(this._getESSimpleMatch('material_name', item));
queryObj.push(this._getESSimpleMatch('material_name', item));
}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('')));
}
else if (this.queryTypes[i] === 'E')
elements.push(item);
//else if (this.queryTypes[i] !== 'S'){ // property }
});
if (elements.length > 0){ // If there are elements (there is no formula or material name)
if (this.elementTable.getAllowOtherElements())
queryObj.bool.filter.push(this._getFieldESMatch('atom_labels', elements, true));//getAtomsESMatch(elements));
else // Regular search, the elements inserted must be sorted for this to work
{
queryObj.bool.filter.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
}
}
///atom_labels": { "operator": "and",
let filterMap = this.filterPanel.getValues();
this._addFiltersInSearchQuery(filterMap, queryObj.bool.filter);
this._addFiltersInSearchQuery(filterMap, queryObj);
//if (filters !== null) queryObj.query.bool.filter.push(filters);
this.materialList.setSearch(queryObj);
this.materialList.setSearch(rootQueryObj);
//util.setBrowserHashPath('search','results');
this.element.querySelector('.add-box').style.display = 'none';
}
/*
if (this.searchQuery.lenght === 0){
util.showUserMsg('No query');
}else{
let queryObj = {};
let elements = [];
queryObj.search_by = {};
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'F') queryObj.search_by.formula = item;
else if (this.queryTypes[i] === 'E') elements.push(item);
else if (this.queryTypes[i] !== 'S'){ // property
if (this.queryTypes[i].indexOf('band-gap') === 0){ // special case
let bandGapData = this.queryTypes[i].split(':');
queryObj.band_gap = {"min": util.eV2J(bandGapData[1]),
"max": util.eV2J(bandGapData[2])};
queryObj.band_gap_direct = bandGapData[3];
}else if (this.queryTypes[i].indexOf('mass-density') === 0){ // special case
let massDensity = this.queryTypes[i].split(':');
queryObj.mass_density = {"min": massDensity[1], "max": massDensity[2]};
}else if (this.queryTypes[i].indexOf('has') === 0){// boolean fields
queryObj[replaceDashes(this.queryTypes[i])] = 'Yes';
}else // general fields
queryObj[replaceDashes(this.queryTypes[i])] = item.split(' | ');
}
});
if (elements.length > 0)
queryObj.search_by.element = elements.join(',');
let exclusiveEl = this.element.querySelector('.exclusive-search:checked');
queryObj.search_by.exclusive = (exclusiveEl === null ? '0' : '1');
this.materialList.setSearch(queryObj);
util.setBrowserHashPath('search','results');
this.element.querySelector('.add-box').style.display = 'none';
}
*/
});
......@@ -8043,12 +8016,21 @@
return div;
}
/*
let newFormula = '';
map.forEach( (value, key) => {
newFormula += key+(value === 1 ? '' : value);
});*/
let query = [];
map.forEach( (value, key) => {
query.push(key+value);
//newFormula += key+(value === 1 ? '' : value);
});
console.log('_reduceFormula RETURN: ', map, newFormula);
return newFormula;
console.log('_reduceFormula RETURN: ', map, query);
return query;
}
......@@ -8071,7 +8053,7 @@
queryFilterArray.push( this._getESSimpleMatch(filterNameDef, values, false) );
}else{ // normal case
queryFilterArray.push( this._getFieldESMatch(filterNameDef, values, false) );
queryFilterArray.push( this._getESOperatorMatch(filterNameDef, values, false) );
}
});
......@@ -8086,7 +8068,7 @@
}
_getFieldESMatch(field, elements, and = true){
_getESOperatorMatch(field, elements, and = true){
let elementsString = '';
if (elements.length > 0) elementsString = elements.join(' ');
......@@ -8146,7 +8128,7 @@
if (type === 'E' && this.searchQuery.indexOf(tag) >= 0) return;
if ( this.searchQuery.length > 0 )
this._addItemInSearchQuery('&', 'S');
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery(tag, type);
this.updateSearchQuery();
//this._showSearchBox();
......@@ -8189,12 +8171,12 @@
this.queryTypes.splice(index, 1);
if ( index > 0 ){
let prevSymbol = this.searchQuery[index-1];
if (prevSymbol === '&' || prevSymbol === '|') {
if (prevSymbol === 'AND' || prevSymbol === 'OR') {
this.searchQuery.splice(index-1, 1);
this.queryTypes.splice(index-1, 1);
}
}
// Check if after the removal a & or | symbol remains as the first in query
// Check if after the removal a AND or OR symbol remains as the first in query
if (this.queryTypes[0] === 'S'){
this.searchQuery.splice(0, 1);
this.queryTypes.splice(0, 1);
......@@ -9148,7 +9130,7 @@
]
}
} `;
*/
postQuery= `
{
......@@ -9160,7 +9142,7 @@
]
}
} `;
*/
console.log('SENDING: ', postQuery);
......
......@@ -171,7 +171,7 @@ postQuery= `
]
}
} `;
*/
postQuery= `
{
......@@ -183,7 +183,7 @@ postQuery= `
]
}
} `;
*/
console.log('SENDING: ', postQuery);
......
......@@ -56,6 +56,17 @@ function replaceDashes(s){
class NewSearchMod {
constructor() {
this.userGuidance = true; // can enabled/disabled
//this.showingSearchBox = false;
this.searchQuery = [];
this.queryTypes = []; //**** Types associated to query elements
// Types: element (E), formula (F), symbol (S) and prop names
this.searchFilters = [];
this.currentOperator = 'AND';
this.element = document.createElement('div');
this.element.setAttribute("id",'search-module');
this.element.innerHTML=
......@@ -129,6 +140,9 @@ class NewSearchMod {
let andOrSwitch = new SwitchComponent(util.IMAGE_DIR+'switch');
this.element.querySelector('#and-or-switch').appendChild(andOrSwitch.element);
andOrSwitch.setListener( e => {
this.currentOperator = ( e ? 'AND' : 'OR');
});
this.elementTable= new ElementTable();
this.elementTable.setClickListener(elementArray => {
......@@ -142,8 +156,8 @@ class NewSearchMod {
this.formulaBox.setAddFormulaListener(formula => {
if (formula.trim() !== ''){
this.addTagInSearchQuery(formula, 'F');
//this.formulaBox.disable(true);
this.addMatNameButton.disabled = true;
this.formulaBox.disable(true);
this.materialNameBox.disable(true);
}
});
......@@ -159,8 +173,6 @@ class NewSearchMod {
this.materialNameBox.disable(true);
}
});
/*
this.propertiesBox= new PropertiesBox();
this.propertiesBox.setAddPropertiesListener(propsMap => {
......@@ -177,13 +189,7 @@ class NewSearchMod {
this.currentTab = 'element';
this.addPanel.appendChild(this.elementTable.element);
this.userGuidance = true; // can enabled/disabled
//this.showingSearchBox = false;
this.searchQuery = [];
this.queryTypes = []; //**** Types associated to query elements
// Types: element (E), formula (F), symbol (S) and prop names
this.searchFilters = [];
this._events();
}
......@@ -197,96 +203,63 @@ class NewSearchMod {
if (this.searchQuery.lenght === 0){
util.showUserMsg('No query');
}else{
let queryObj = { 'bool' : {} };
//queryObj.bool.must = {};
queryObj.bool.filter = [];
let elements = [];
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
});
if (this.queryTypes[i] === 'F'){
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
//queryObj.bool.must = [];
//queryObj.bool.filter = [];
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'F'){
//if (this.elementTable.getAllowOtherElements())
//else
let esMatchQuery;
if (this.formulaBox.getMultiplesOfFormula()){
esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
}else
esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.bool.filter.push(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 (this.queryTypes[i] === 'MN'){
queryObj.bool.filter.push(this._getESSimpleMatch('material_name', item));
queryObj.push(this._getESSimpleMatch('material_name', item));
}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('')));
}
else if (this.queryTypes[i] === 'E')
elements.push(item);
//else if (this.queryTypes[i] !== 'S'){ // property }
});
if (elements.length > 0){ // If there are elements (there is no formula or material name)
if (this.elementTable.getAllowOtherElements())
queryObj.bool.filter.push(this._getFieldESMatch('atom_labels', elements, true));//getAtomsESMatch(elements));
else // Regular search, the elements inserted must be sorted for this to work
{
queryObj.bool.filter.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
}
}
///atom_labels": { "operator": "and",
let filterMap = this.filterPanel.getValues();
this._addFiltersInSearchQuery(filterMap, queryObj.bool.filter);
this._addFiltersInSearchQuery(filterMap, queryObj);
//if (filters !== null) queryObj.query.bool.filter.push(filters);
this.materialList.setSearch(queryObj);
this.materialList.setSearch(rootQueryObj);
//util.setBrowserHashPath('search','results');
this.element.querySelector('.add-box').style.display = 'none';
}
/*
if (this.searchQuery.lenght === 0){
util.showUserMsg('No query');
}else{
let queryObj = {};
let elements = [];
queryObj.search_by = {};
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'F') queryObj.search_by.formula = item;
else if (this.queryTypes[i] === 'E') elements.push(item);
else if (this.queryTypes[i] !== 'S'){ // property
if (this.queryTypes[i].indexOf('band-gap') === 0){ // special case
let bandGapData = this.queryTypes[i].split(':');
queryObj.band_gap = {"min": util.eV2J(bandGapData[1]),
"max": util.eV2J(bandGapData[2])};
queryObj.band_gap_direct = bandGapData[3];
}else if (this.queryTypes[i].indexOf('mass-density') === 0){ // special case
let massDensity = this.queryTypes[i].split(':');
queryObj.mass_density = {"min": massDensity[1], "max": massDensity[2]};
}else if (this.queryTypes[i].indexOf('has') === 0){// boolean fields
queryObj[replaceDashes(this.queryTypes[i])] = 'Yes';
}else // general fields
queryObj[replaceDashes(this.queryTypes[i])] = item.split(' | ');
}
});
if (elements.length > 0)
queryObj.search_by.element = elements.join(',');
let exclusiveEl = this.element.querySelector('.exclusive-search:checked');
queryObj.search_by.exclusive = (exclusiveEl === null ? '0' : '1');
this.materialList.setSearch(queryObj);
util.setBrowserHashPath('search','results');
this.element.querySelector('.add-box').style.display = 'none';
}
*/
});
......@@ -436,12 +409,21 @@ class NewSearchMod {
return div;
}
/*
let newFormula = '';
map.forEach( (value, key) => {
newFormula += key+(value === 1 ? '' : value);
});*/
let query = [];
map.forEach( (value, key) => {
query.push(key+value);
//newFormula += key+(value === 1 ? '' : value);
});
console.log('_reduceFormula RETURN: ', map, newFormula);
return newFormula;
console.log('_reduceFormula RETURN: ', map, query);
return query;
}
......@@ -464,7 +446,7 @@ class NewSearchMod {
queryFilterArray.push( this._getESSimpleMatch(filterNameDef, values, false) );
}else{ // normal case
queryFilterArray.push( this._getFieldESMatch(filterNameDef, values, false) );
queryFilterArray.push( this._getESOperatorMatch(filterNameDef, values, false) );
}
});
......@@ -479,7 +461,7 @@ class NewSearchMod {
}
_getFieldESMatch(field, elements, and = true){
_getESOperatorMatch(field, elements, and = true){
let elementsString = '';
if (elements.length > 0) elementsString = elements.join(' ');
......@@ -539,7 +521,7 @@ class NewSearchMod {
if (type === 'E' && this.searchQuery.indexOf(tag) >= 0) return;
if ( this.searchQuery.length > 0 )
this._addItemInSearchQuery('&', 'S');
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery(tag, type);
this.updateSearchQuery();
//this._showSearchBox();
......@@ -582,12 +564,12 @@ class NewSearchMod {
this.queryTypes.splice(index, 1);
if ( index > 0 ){
let prevSymbol = this.searchQuery[index-1];
if (prevSymbol === '&' || prevSymbol === '|') {
if (prevSymbol === 'AND' || prevSymbol === 'OR') {
this.searchQuery.splice(index-1, 1);
this.queryTypes.splice(index-1, 1);
}
}
// Check if after the removal a & or | symbol remains as the first in query
// Check if after the removal a AND or OR symbol remains as the first in query
if (this.queryTypes[0] === 'S'){
this.searchQuery.splice(0, 1);
this.queryTypes.splice(0, 1);
......
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