Currently, I am developing a Search Application using Angular7 and elasticsearchJS. Within my data Service, the elasticsearch JSON query body is generated from user inputs. While it functions properly with a simple string query in this.query, there seems to be an issue with handling a complex array from this.selectedTopics (checkbox selections).
https://i.sstatic.net/xTTx0.png
The problem arises as backslashes appear in my console.log output when viewing the query (refer to image).
console.log(this.selectedTopics);
// Output of selectedTopics:
// ["Lindenholz", "Sandstein"]
this.selectedTopics.forEach( (item) => {
this.termarray.push('{ "term": {"79_material":"' + item + '"}}');
});
console.log(this.termarray.join(', '));
// Output of termarray:
//
// [
// { "term": {"79_material":"Sandstein"}},
// { "term": {"79_material":"Lindenholz"}}
// ]
// The formatting appears correct in the console, but sending termarray to the JSON body results in incorrect backslashes.
this.body = {
'size': '100',
'from': '0',
'query': {
'filtered': {
'query' : {
'multi_match': {
'query': this.query,
'type': 'phrase_prefix',
'fields': this.selectedCategory
}
},
'filter': {
'bool': {
'must': [
this.termarray.join(', ')
]
}
}
}
},
'facets' : {
'79_material' : {
'terms' : {'field' : '79_material.keyword'}
},
'79_technik' : {
'terms' : {'field' : '79_technik.keyword'}
},
'79_kuenstler' : {
'terms' : {'field' : '79_kuenstler.content'}
},
'79_verortung' : {
'terms' : {'field' : '79_verortung.content'}
},
},
'sort' : [
{ '79_material' : {'order' : 'asc'}},
'_score'
]
};
https://i.sstatic.net/mPv3S.png
The desired result should resemble:
[
{'term' : { '79_material': 'holz' }},
{'term' : { '79_material': 'lindenholz' }}
]