Commit ded21d0f authored by Iker Hurtado's avatar Iker Hurtado
Browse files

Complex search logic implementation (3)

parent 3dcfa515
......@@ -7811,55 +7811,15 @@
util.showUserMsg('No query');
}else{
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
});
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'){
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);
//************** Material name or complex search expression
//if (this.queryTypes[i] === 'MN'){
//queryObj.push(this._getESSimpleMatch('material_name', item));
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
}else if (this.queryTypes[i] === 'MN'){
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] !== 'S'){ // property }
});
let filterMap = this.filterPanel.getValues();
this._addFiltersInSearchQuery(filterMap, queryObj);
//if (filters !== null) queryObj.query.bool.filter.push(filters);
let rootQueryObj = this._getESQueryFromSearchQuery();
/* Pending to ADD *************/
//let filterMap = this.filterPanel.getValues();
//this._addFiltersInSearchQuery(filterMap, queryObj);
this.materialList.setSearch(rootQueryObj);
//util.setBrowserHashPath('search','results');
......@@ -7943,97 +7903,63 @@
}
_sortElements(elements){
let numbers = [];
let elString = '';
elements.forEach( e => numbers.push(util.ELEMENTS.indexOf(e)) );
numbers.sort( (a, b) => a - b ); // atomic number-1
numbers.forEach( n => elString += util.ELEMENTS[n] );
//console.log('_sortElements ',numbers, elString);
return elString;
}
_getESQueryFromSearchQuery(){
_reduceFormula(formula){
let index = 0;
let map = new Map();
let key;
while ( index < formula.length ){
//console.log('_reduceFormula index', index);
let el2 = formula.substring(index, index+2);
let el1 = formula.substring(index, index+1);
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
});
if (util.ELEMENTS.indexOf(el2) >= 0){
map.set(el2, 1); // 1 default value
index += 2;
key = el2;
//console.log('eleemnt 2chars', key);
}else if (util.ELEMENTS.indexOf(el1) >= 0){
map.set(el1, 1); // 1 default value
index++;
key = el1;
//console.log('eleemnt 1chars', key);
}else{ // It's a number
let num = parseInt(el2);
if (num >= 10) index += 2; // 2 figures number
else index++;// 1 figure number
//console.log('number ', num, key);
map.set(key, num);
}
// console.log('FINAL LOOP', map, index);
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
let counter = 0;
while ( !checkIfReduced(map) ){ // console.log('Reducing', map);
let div = 1;
if (isDivisibleBy(map, 2)) div = 2;
else if (isDivisibleBy(map, 3)) div = 3;
else if (isDivisibleBy(map, 5)) div = 5;
else if (isDivisibleBy(map, 7)) div = 7;
else if (isDivisibleBy(map, 11)) div = 11;
this.searchQuery.forEach( (item, i) => {
map.forEach( (value, key) => {
map.set(key, (value/div));
});
//console.log('Reducing DIV', map);
counter++;
if (counter > 5) break;
}
if (this.queryTypes[i] === 'F'){
function checkIfReduced(formulaMap){
let min = 100;
formulaMap.forEach( (value, key) => {
if (value < min) min = value;
});
return min === 1;
}
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);
function isDivisibleBy(formulaMap, n){
let div = true;
formulaMap.forEach( (value, key) => {
if (value % n !== 0) div = false;
});
return div;
}
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
/*
let newFormula = '';
map.forEach( (value, key) => {
newFormula += key+(value === 1 ? '' : value);
});*/
}else if (this.queryTypes[i] === 'E'){
// if (this.elementTable.getAllowOtherElements())
queryObj.push(this._getESSimpleMatch('atom_labels', item));
let query = [];
map.forEach( (value, key) => {
query.push(key+value);
//newFormula += key+(value === 1 ? '' : value);
//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;
}
console.log('_reduceFormula RETURN: ', map, query);
return query;
_sortElements(elements){
let numbers = [];
let elString = '';
elements.forEach( e => numbers.push(util.ELEMENTS.indexOf(e)) );
numbers.sort( (a, b) => a - b ); // atomic number-1
numbers.forEach( n => elString += util.ELEMENTS[n] );
//console.log('_sortElements ',numbers, elString);
return elString;
}
_addFiltersInSearchQuery(filterMap, queryFilterArray){
//let filters = [];
filterMap.forEach((values/*Array*/, filterName) => {
......@@ -8246,6 +8172,85 @@
}
*/
}
_reduceFormula(formula){
let index = 0;
let map = new Map();
let key;
while ( index < formula.length ){
//console.log('_reduceFormula index', index);
let el2 = formula.substring(index, index+2);
let el1 = formula.substring(index, index+1);
if (util.ELEMENTS.indexOf(el2) >= 0){
map.set(el2, 1); // 1 default value
index += 2;
key = el2;
//console.log('eleemnt 2chars', key);
}else if (util.ELEMENTS.indexOf(el1) >= 0){
map.set(el1, 1); // 1 default value
index++;
key = el1;
//console.log('eleemnt 1chars', key);
}else{ // It's a number
let num = parseInt(el2);
if (num >= 10) index += 2; // 2 figures number
else index++;// 1 figure number
//console.log('number ', num, key);
map.set(key, num);
}
// console.log('FINAL LOOP', map, index);
}
let counter = 0;
while ( !checkIfReduced(map) ){ // console.log('Reducing', map);
let div = 1;
if (isDivisibleBy(map, 2)) div = 2;
else if (isDivisibleBy(map, 3)) div = 3;
else if (isDivisibleBy(map, 5)) div = 5;
else if (isDivisibleBy(map, 7)) div = 7;
else if (isDivisibleBy(map, 11)) div = 11;
map.forEach( (value, key) => {
map.set(key, (value/div));
});
//console.log('Reducing DIV', map);
counter++;
if (counter > 5) break;
}
function checkIfReduced(formulaMap){
let min = 100;
formulaMap.forEach( (value, key) => {
if (value < min) min = value;
});
return min === 1;
}
function isDivisibleBy(formulaMap, n){
let div = true;
formulaMap.forEach( (value, key) => {
if (value % n !== 0) div = false;
});
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, query);
return query;
}
}
......
......@@ -204,55 +204,15 @@ class NewSearchMod {
util.showUserMsg('No query');
}else{
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
});
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'){
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);
//************** Material name or complex search expression
//if (this.queryTypes[i] === 'MN'){
//queryObj.push(this._getESSimpleMatch('material_name', item));
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
}else if (this.queryTypes[i] === 'MN'){
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] !== 'S'){ // property }
});
let filterMap = this.filterPanel.getValues();
this._addFiltersInSearchQuery(filterMap, queryObj);
//if (filters !== null) queryObj.query.bool.filter.push(filters);
let rootQueryObj = this._getESQueryFromSearchQuery();
/* Pending to ADD *************/
//let filterMap = this.filterPanel.getValues();
//this._addFiltersInSearchQuery(filterMap, queryObj);
this.materialList.setSearch(rootQueryObj);
//util.setBrowserHashPath('search','results');
......@@ -336,97 +296,63 @@ class NewSearchMod {
}
_sortElements(elements){
let numbers = [];
let elString = '';
elements.forEach( e => numbers.push(util.ELEMENTS.indexOf(e)) );
numbers.sort( (a, b) => a - b ); // atomic number-1
numbers.forEach( n => elString += util.ELEMENTS[n] );
//console.log('_sortElements ',numbers, elString);
return elString;
}
_getESQueryFromSearchQuery(){
_reduceFormula(formula){
let index = 0;
let map = new Map();
let key;
while ( index < formula.length ){
//console.log('_reduceFormula index', index);
let el2 = formula.substring(index, index+2);
let el1 = formula.substring(index, index+1);
// Query structure analysis - no parentheses taken into account
let boolOperator;
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'S') boolOperator = item;
});
if (util.ELEMENTS.indexOf(el2) >= 0){
map.set(el2, 1); // 1 default value
index += 2;
key = el2;
//console.log('eleemnt 2chars', key);
}else if (util.ELEMENTS.indexOf(el1) >= 0){
map.set(el1, 1); // 1 default value
index++;
key = el1;
//console.log('eleemnt 1chars', key);
}else{ // It's a number
let num = parseInt(el2);
if (num >= 10) index += 2; // 2 figures number
else index++;// 1 figure number
//console.log('number ', num, key);
map.set(key, num);
}
// console.log('FINAL LOOP', map, index);
let rootQueryObj = { 'bool' : {} };
let queryObj;
if (boolOperator === 'AND'){
rootQueryObj.bool.must = [];
queryObj = rootQueryObj.bool.must;
}else{ // OR
rootQueryObj.bool.should = [];
queryObj = rootQueryObj.bool.should;
}
let counter = 0;
while ( !checkIfReduced(map) ){ // console.log('Reducing', map);
let div = 1;
if (isDivisibleBy(map, 2)) div = 2;
else if (isDivisibleBy(map, 3)) div = 3;
else if (isDivisibleBy(map, 5)) div = 5;
else if (isDivisibleBy(map, 7)) div = 7;
else if (isDivisibleBy(map, 11)) div = 11;
this.searchQuery.forEach( (item, i) => {
map.forEach( (value, key) => {
map.set(key, (value/div));
});
//console.log('Reducing DIV', map);
counter++;
if (counter > 5) break;
}
if (this.queryTypes[i] === 'F'){
function checkIfReduced(formulaMap){
let min = 100;
formulaMap.forEach( (value, key) => {
if (value < min) min = value;
});
return min === 1;
}
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);
function isDivisibleBy(formulaMap, n){
let div = true;
formulaMap.forEach( (value, key) => {
if (value % n !== 0) div = false;
});
return div;
}
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
queryObj.push(esMatchQuery);
/*
let newFormula = '';
map.forEach( (value, key) => {
newFormula += key+(value === 1 ? '' : value);
});*/
}else if (this.queryTypes[i] === 'E'){
// if (this.elementTable.getAllowOtherElements())
queryObj.push(this._getESSimpleMatch('atom_labels', item));
let query = [];
map.forEach( (value, key) => {
query.push(key+value);
//newFormula += key+(value === 1 ? '' : value);
//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;
}
console.log('_reduceFormula RETURN: ', map, query);
return query;
_sortElements(elements){
let numbers = [];
let elString = '';
elements.forEach( e => numbers.push(util.ELEMENTS.indexOf(e)) );
numbers.sort( (a, b) => a - b ); // atomic number-1
numbers.forEach( n => elString += util.ELEMENTS[n] );
//console.log('_sortElements ',numbers, elString);
return elString;
}
_addFiltersInSearchQuery(filterMap, queryFilterArray){
//let filters = [];
filterMap.forEach((values/*Array*/, filterName) => {
......@@ -639,6 +565,85 @@ class NewSearchMod {
}
*/
}
_reduceFormula(formula){
let index = 0;
let map = new Map();
let key;
while ( index < formula.length ){
//console.log('_reduceFormula index', index);
let el2 = formula.substring(index, index+2);
let el1 = formula.substring(index, index+1);
if (util.ELEMENTS.indexOf(el2) >= 0){
map.set(el2, 1); // 1 default value
index += 2;
key = el2;
//console.log('eleemnt 2chars', key);
}else if (util.ELEMENTS.indexOf(el1) >= 0){
map.set(el1, 1); // 1 default value
index++;
key = el1;
//console.log('eleemnt 1chars', key);
}else{ // It's a number
let num = parseInt(el2);
if (num >= 10) index += 2; // 2 figures number
else index++;// 1 figure number
//console.log('number ', num, key);
map.set(key, num);
}
// console.log('FINAL LOOP', map, index);
}
let counter = 0;
while ( !checkIfReduced(map) ){ // console.log('Reducing', map);
let div = 1;
if (isDivisibleBy(map, 2)) div = 2;
else if (isDivisibleBy(map, 3)) div = 3;
else if (isDivisibleBy(map, 5)) div = 5;
else if (isDivisibleBy(map, 7)) div = 7;
else if (isDivisibleBy(map, 11)) div = 11;
map.forEach( (value, key) => {
map.set(key, (value/div));
});
//console.log('Reducing DIV', map);
counter++;
if (counter > 5) break;
}
function checkIfReduced(formulaMap){
let min = 100;
formulaMap.forEach( (value, key) => {
if (value < min) min = value;
});
return min === 1;
}
function isDivisibleBy(formulaMap, n){
let div = true;
formulaMap.forEach( (value, key) => {
if (value % n !== 0) div = false;
});
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, query);
return query;
}
}
......
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