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

Complex search logic implementation (10)

parent f5bfea2b
......@@ -7937,7 +7937,7 @@
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'F' || queryTypes[i] === 'E'){
addItem( (openIndex >= 0 ? parFormulas : formulas), i,
item+(queryTypes[i] === 'E' ? 'X' : ''));
item+(queryTypes[i] === 'E' ? '0' : ''));
}else if ( searchQuery[i] === '(' ){
if (i-1 >= 0 ) parOperator = searchQuery[i-1];
......@@ -7966,6 +7966,30 @@
// the elements inserted must be sorted for this to work
// queryObj.bool.must.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
// if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
let rootQueryObj = { 'bool' : {} };
rootQueryObj.bool.should = [];
let queryObj = rootQueryObj.bool.should;
formulas.forEach( formula => {
let pFormula;
let searchByElement = (formula.indexOf('0') >= 0);
if (searchByElement){ // There are some element in the search expression
/** TDO**/
pFormula = this._processFormula(formula, 'element-string');
//queryObj.push(this._getESSimpleMatch('atom_labels_keyword', pFormula));
}else{ // Only formulas
if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
pFormula = this._reduceFormula(formula, false);
queryObj.push(this._getESSimpleMatch('formula_reduced_keyword', pFormula));
}else{
pFormula = this._processFormula(formula, 'canonical-formula');
queryObj.push(this._getESSimpleMatch('formula_cell_keyword', pFormula));
}
}
});
return rootQueryObj;
function addItem(formulas, i, item){
if (i === 0 || searchQuery[i-1] === '('){
......@@ -8026,13 +8050,13 @@
if (queryTypes[i] === 'F'){ // Formula case
let esMatchQuery;
if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced_terms', this._reduceFormula(item));
}else
esMatchQuery = this._getESOperatorMatch('formula', this._tokenizeFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_cell_terms', this._processFormula(item, 'tokens'));
queryObj.push(esMatchQuery);
}else if (queryTypes[i] === 'E'){ // Element case
queryObj.push(this._getESSimpleMatch('atom_labels', item));
queryObj.push(this._getESSimpleMatch('atom_labels_terms', item));
}else if (queryTypes[i] === 'Q'){
queryObj.push(item);
......@@ -8044,18 +8068,6 @@
} // _getESQueryFromSearchQuery_otherElements()
_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){
......@@ -8300,12 +8312,25 @@
*/
}
_reduceFormula(formula){
_sortElements(elements){
let numbers = [];
let sortedElements = [];
elements.forEach( e => numbers.push(util.ELEMENTS.indexOf(e)) );
numbers.sort( (a, b) => a - b ); // atomic number-1
numbers.forEach( n => sortedElements.push(util.ELEMENTS[n]) );
//console.log('_sortElements ',numbers, elString);
return sortedElements;
}
_reduceFormula(formula, getTokens = true){
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);
......@@ -8362,30 +8387,29 @@
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);
});
let tokens = [];
let canonicalFormula = '';
if (getTokens){
map.forEach( (value, key) => tokens.push(key+value) );
}else{
let sortedElements = this._sortElements( Array.from( map.keys() ) );
sortedElements.forEach( element => {
canonicalFormula += element+map.get(element);
//canonicalFormula += element+(map.get(element) === 1 ? '' : map.get(element));
});
}
console.log('_reduceFormula RETURN: ', map, query);
return query;
console.log('_reduceFormula RETURN: ', map, tokens, canonicalFormula);
return (getTokens ? tokens : canonicalFormula);
}
_tokenizeFormula(formula){
_processFormula(formula, type){
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);
......@@ -8409,15 +8433,35 @@
// console.log('FINAL LOOP', map, index);
}
let query = [];
map.forEach( (value, key) => {
query.push( value === 1 ? key : key+value);
//newFormula += key+(value === 1 ? '' : value);
});
console.log('_tokenizeFormula RETURN: ', map, query);
return query;
if (type === 'tokens'){
let tokens = [];
map.forEach( (value, key) => tokens.push(key+value) );
console.log('_processFormula RETURN: ', map, tokens);
return tokens;
}else{
let sortedElements = this._sortElements( Array.from( map.keys() ) );
if (type === 'canonical-formula'){
let formulaString = '';
sortedElements.forEach( element => {
formulaString += element+map.get(element);
});
console.log('_processFormula RETURN: ', map, formulaString);
return formulaString;
}else{ // elements-string
let elementsString = '';
let elementsInFormulas = [];
sortedElements.forEach( element => {
elementsString += element;
let val = map.get(element);
if (val !== 0) elementsInFormulas.push(element+val);
});
console.log('_processFormula RETURN: ', map, [elementsInFormulas ,elementsString]);
return [elementsInFormulas ,elementsString];
}
}
}
}
......
......@@ -330,7 +330,7 @@ class NewSearchMod {
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'F' || queryTypes[i] === 'E'){
addItem( (openIndex >= 0 ? parFormulas : formulas), i,
item+(queryTypes[i] === 'E' ? 'X' : ''));
item+(queryTypes[i] === 'E' ? '0' : ''));
}else if ( searchQuery[i] === '(' ){
if (i-1 >= 0 ) parOperator = searchQuery[i-1];
......@@ -359,6 +359,30 @@ class NewSearchMod {
// the elements inserted must be sorted for this to work
// queryObj.bool.must.push(this._getESSimpleMatch('atom_labels_keyword', this._sortElements(elements) )); ///elements.join('')));
// if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
let rootQueryObj = { 'bool' : {} };
rootQueryObj.bool.should = [];
let queryObj = rootQueryObj.bool.should;
formulas.forEach( formula => {
let pFormula;
let searchByElement = (formula.indexOf('0') >= 0);
if (searchByElement){ // There are some element in the search expression
/** TDO**/
pFormula = this._processFormula(formula, 'element-string');
//queryObj.push(this._getESSimpleMatch('atom_labels_keyword', pFormula));
}else{ // Only formulas
if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
pFormula = this._reduceFormula(formula, false);
queryObj.push(this._getESSimpleMatch('formula_reduced_keyword', pFormula));
}else{
pFormula = this._processFormula(formula, 'canonical-formula');
queryObj.push(this._getESSimpleMatch('formula_cell_keyword', pFormula));
}
}
});
return rootQueryObj;
function addItem(formulas, i, item){
if (i === 0 || searchQuery[i-1] === '('){
......@@ -419,13 +443,13 @@ class NewSearchMod {
if (queryTypes[i] === 'F'){ // Formula case
let esMatchQuery;
if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced_terms', this._reduceFormula(item));
}else
esMatchQuery = this._getESOperatorMatch('formula', this._tokenizeFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_cell_terms', this._processFormula(item, 'tokens'));
queryObj.push(esMatchQuery);
}else if (queryTypes[i] === 'E'){ // Element case
queryObj.push(this._getESSimpleMatch('atom_labels', item));
queryObj.push(this._getESSimpleMatch('atom_labels_terms', item));
}else if (queryTypes[i] === 'Q'){
queryObj.push(item);
......@@ -437,18 +461,6 @@ class NewSearchMod {
} // _getESQueryFromSearchQuery_otherElements()
_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){
......@@ -693,12 +705,25 @@ class NewSearchMod {
*/
}
_reduceFormula(formula){
_sortElements(elements){
let numbers = [];
let sortedElements = [];
elements.forEach( e => numbers.push(util.ELEMENTS.indexOf(e)) );
numbers.sort( (a, b) => a - b ); // atomic number-1
numbers.forEach( n => sortedElements.push(util.ELEMENTS[n]) );
//console.log('_sortElements ',numbers, elString);
return sortedElements;
}
_reduceFormula(formula, getTokens = true){
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);
......@@ -755,30 +780,29 @@ 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);
});
let tokens = [];
let canonicalFormula = '';
if (getTokens){
map.forEach( (value, key) => tokens.push(key+value) );
}else{
let sortedElements = this._sortElements( Array.from( map.keys() ) );
sortedElements.forEach( element => {
canonicalFormula += element+map.get(element);
//canonicalFormula += element+(map.get(element) === 1 ? '' : map.get(element));
});
}
console.log('_reduceFormula RETURN: ', map, query);
return query;
console.log('_reduceFormula RETURN: ', map, tokens, canonicalFormula);
return (getTokens ? tokens : canonicalFormula);
}
_tokenizeFormula(formula){
_processFormula(formula, type){
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);
......@@ -802,15 +826,35 @@ class NewSearchMod {
// console.log('FINAL LOOP', map, index);
}
let query = [];
map.forEach( (value, key) => {
query.push( value === 1 ? key : key+value);
//newFormula += key+(value === 1 ? '' : value);
});
console.log('_tokenizeFormula RETURN: ', map, query);
return query;
if (type === 'tokens'){
let tokens = [];
map.forEach( (value, key) => tokens.push(key+value) );
console.log('_processFormula RETURN: ', map, tokens);
return tokens;
}else{
let sortedElements = this._sortElements( Array.from( map.keys() ) );
if (type === 'canonical-formula'){
let formulaString = '';
sortedElements.forEach( element => {
formulaString += element+map.get(element);
});
console.log('_processFormula RETURN: ', map, formulaString);
return formulaString;
}else{ // elements-string
let elementsString = '';
let elementsInFormulas = [];
sortedElements.forEach( element => {
elementsString += element;
let val = map.get(element);
if (val !== 0) elementsInFormulas.push(element+val);
});
console.log('_processFormula RETURN: ', map, [elementsInFormulas ,elementsString]);
return [elementsInFormulas ,elementsString];
}
}
}
}
......
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