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

Optimade query generation from the UI search query and the property fields

parent 56fcf30e
Pipeline #90240 skipped with stage
...@@ -43,7 +43,7 @@ class FilterPanel { ...@@ -43,7 +43,7 @@ class FilterPanel {
let structureGroupBox = this.createPropsGroupBox('Structure') let structureGroupBox = this.createPropsGroupBox('Structure')
this.element.append(structureGroupBox); this.element.append(structureGroupBox);
const systemTypeField = new CheckboxesField('System type', 'system-type', [ const systemTypeField = new CheckboxesField('System type', 'material_type', [
{value: 'bulk', text: 'Bulk'}, {value: 'bulk', text: 'Bulk'},
{value: '2D', text: '2D'}, {value: '2D', text: '2D'},
{value: '1D', text: '1D'} {value: '1D', text: '1D'}
...@@ -51,7 +51,7 @@ class FilterPanel { ...@@ -51,7 +51,7 @@ class FilterPanel {
this.fields.push(systemTypeField) this.fields.push(systemTypeField)
structureGroupBox.append(systemTypeField.element); /// By looping***** structureGroupBox.append(systemTypeField.element); /// By looping*****
const crystalSystemField = new CheckboxesField('Crystal system', 'crystal-system', [ const crystalSystemField = new CheckboxesField('Crystal system', 'crystal_system', [
{value: 'cubic', text: 'Cubic'}, {value: 'cubic', text: 'Cubic'},
{value: 'hexagonal', text: 'Hexagonal'}, {value: 'hexagonal', text: 'Hexagonal'},
{value: 'trigonal', text: 'Trigonal'}, {value: 'trigonal', text: 'Trigonal'},
...@@ -68,7 +68,7 @@ class FilterPanel { ...@@ -68,7 +68,7 @@ class FilterPanel {
this.fields.push(spaceGroupField) this.fields.push(spaceGroupField)
structureGroupBox.append(spaceGroupField.element); structureGroupBox.append(spaceGroupField.element);
const structureTypeField = new AutocompleteField('Structure type', "structure-type", 'structure_type'); const structureTypeField = new AutocompleteField('Structure type', 'structure_type');
this.fields.push(structureTypeField) this.fields.push(structureTypeField)
structureGroupBox.append(structureTypeField.element); structureGroupBox.append(structureTypeField.element);
...@@ -82,7 +82,7 @@ class FilterPanel { ...@@ -82,7 +82,7 @@ class FilterPanel {
propertiesGroupBox.append(massDensityField.element); propertiesGroupBox.append(massDensityField.element);
const boolValueFields = new CheckboxesField('Results containing...', undefined, [ const boolValueFields = new CheckboxesField('Results containing...', undefined, [
{value: 'Band structure', text: 'Band structure', id: 'has-band-structure'}, {value: 'Band structure', text: 'Band structure', id: 'has_band_structure'},
{value: 'DOS', text: 'DOS', id: 'has-dos'}, {value: 'DOS', text: 'DOS', id: 'has-dos'},
{value: 'Thermal properties', text: 'Thermal properties', id: 'has-thermal-properties'}, {value: 'Thermal properties', text: 'Thermal properties', id: 'has-thermal-properties'},
]); ]);
...@@ -206,7 +206,7 @@ class CheckboxesField{ ...@@ -206,7 +206,7 @@ class CheckboxesField{
this.checkboxes.forEach( checkbox => { this.checkboxes.forEach( checkbox => {
if (checkbox.checked) { if (checkbox.checked) {
let s = checkbox.className; let s = checkbox.className;
values.push({fieldId: s.substring(0, s.indexOf('-field')), value: true}); values.push({fieldId: s.substring(0, s.indexOf('-field')), value: [true]});
} }
}); });
return values; return values;
...@@ -249,8 +249,8 @@ class SpaceGroupField{ ...@@ -249,8 +249,8 @@ class SpaceGroupField{
getValues(){ getValues(){
let type = this.select.options[this.select.selectedIndex].value; let type = this.select.options[this.select.selectedIndex].value;
let propName = (type === 'by-number' ? 'space-group-number' let propName = (type === 'by-number' ? 'space_group_number'
: 'space-group-international-short-symbol'); : 'space_group_international_short_symbol');
let value = this.input.value; let value = this.input.value;
return ( value.trim() === '' ? null : { fieldId: propName, value: [value] } ); return ( value.trim() === '' ? null : { fieldId: propName, value: [value] } );
...@@ -268,7 +268,7 @@ class SpaceGroupField{ ...@@ -268,7 +268,7 @@ class SpaceGroupField{
class AutocompleteField{ class AutocompleteField{
constructor(name, id, apiId){ constructor(name, id){
this.fieldId = id this.fieldId = id
this.element = document.createElement('div'); this.element = document.createElement('div');
this.element.className = 'filter-quantity-box'; this.element.className = 'filter-quantity-box';
...@@ -281,8 +281,8 @@ class AutocompleteField{ ...@@ -281,8 +281,8 @@ class AutocompleteField{
this.autocomplete.element.classList.add('textfield-filter'); this.autocomplete.element.classList.add('textfield-filter');
this.element.append(this.autocomplete.element) this.element.append(this.autocomplete.element)
let r1 = util.serverReq(util.getSuggestionURL(apiId), (e) => { let r1 = util.serverReq(util.getSuggestionURL(this.fieldId), (e) => {
let names = JSON.parse(r1.response)[apiId]; let names = JSON.parse(r1.response)[this.fieldId];
this.autocomplete.autocomplete(names); this.autocomplete.autocomplete(names);
}); });
} }
......
...@@ -340,29 +340,32 @@ class NewSearchMod { ...@@ -340,29 +340,32 @@ class NewSearchMod {
sendQuery(){ sendQuery(){
//**** The optimade query must be formed from the search box and the properties selected //**** The optimade query must be formed from the search box and the properties selected
let optimadeQuery = this.searchBox.getOptimadeQuery();// + this.filterPanel.getValues() let optimadeQuery = this.searchBox.getOptimadeQuery(true);//
console.log('sendQuery optimadeQuery: ', optimadeQuery); let optimadeQueryForProps = getOptimadeQueryFromProps(this.filterPanel.getValues());
if (optimadeQuery !== ''){ // if there are items in the serach box
optimadeQuery += (optimadeQueryForProps.length > 0 ? ' AND '+optimadeQueryForProps : '');
console.log('sendQuery optimadeQuery: ', optimadeQuery, optimadeQueryForProps );
this.materialList.initSearch(optimadeQuery); this.materialList.initSearch(optimadeQuery);
} }
getOptimadeQueryFromSearchQuery(searchQuery, queryTypes){ function getOptimadeQueryFromProps(propsValuesMap){
let optimadeQuery = '';
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'E'){
optimadeQuery += ' elements HAS ONLY "'+searchQuery[i]+'"'
}else if ( queryTypes[i] === 'F' ){ let query = '';
}else if ( queryTypes[i] === 'S' ){ propsValuesMap.forEach( (values, prop) => {
optimadeQuery += ' '+searchQuery[i]+'' let subquery = '';
values.forEach( v => { // values should be and array, sometimes with just one value
let val = ( v === true ? 'TRUE' : '"'+v+'"')
subquery += (subquery === '' ? '' : ' OR ')+prop+'='+val;
})
query += (query === '' ? '' : ' AND ')+`(${subquery})`
})
return query
} }
});
console.log('optimadeQuery: ', optimadeQuery)
return optimadeQuery;
} }
_getESQueryFromSearchQuery(searchQuery, queryTypes){ _getESQueryFromSearchQuery(searchQuery, queryTypes){
let formulas = []; let formulas = [];
let parFormulas = []; let parFormulas = [];
......
...@@ -85,11 +85,36 @@ class SearchBox{ ...@@ -85,11 +85,36 @@ class SearchBox{
} }
getOptimadeQuery(){ getOptimadeQuery(allowOtherElements){
if (this.searchQuery.length === 0 || if (this.searchQuery.length === 0 ||
!isQueryWellFormed(this.searchQuery)) return ''; !isQueryWellFormed(this.searchQuery)) return '';
else return 'elements HAS ALL "Si", "Cl"'; //else if () // MATERIAL CASE
else{
let optimadeQuery = '';
//let otherElementsFlag = ( allowOtherElements ? '' : 'ONLY ' )
// Inclusive search
this.searchQuery.forEach( (item, i) => {
if (this.queryTypes[i] === 'E'){
optimadeQuery += ` elements HAS "${this.searchQuery[i]}"`
}else if ( this.queryTypes[i] === 'F' ){
optimadeQuery += ` formula="${this.searchQuery[i]}"`
}else if ( this.queryTypes[i] === 'S' ){
optimadeQuery += ' '+this.searchQuery[i]+''
}
});
console.log('optimadeQuery: ', optimadeQuery)
//return 'elements HAS ONLY "F"';
return optimadeQuery;//'elements HAS ALL "Si", "Cl"';
}
function isQueryWellFormed(searchQuery){ function isQueryWellFormed(searchQuery){
const openingParIndex = searchQuery.indexOf('(') const openingParIndex = searchQuery.indexOf('(')
...@@ -219,6 +244,7 @@ class SearchBox{ ...@@ -219,6 +244,7 @@ class SearchBox{
this.searchQueryChangeListener = listener; this.searchQueryChangeListener = listener;
} }
} }
// EXPORTS // EXPORTS
......
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