Skip to content
Snippets Groups Projects
Unverified Commit 8a3184f5 authored by Martin Weise's avatar Martin Weise
Browse files

Updated form when switching type

parent 7274e862
No related branches found
No related tags found
4 merge requests!231CI: Remove build for log-service,!228Better error message handling in the frontend,!223Release of version 1.4.0,!215Resolve "Fix the unit independent search"
...@@ -152,11 +152,10 @@ def search(): ...@@ -152,11 +152,10 @@ def search():
req_body = request.json req_body = request.json
logging.debug('search request body: %s', req_body) logging.debug('search request body: %s', req_body)
search_term = req_body.get("search_term") search_term = req_body.get("search_term")
type = req_body.get("type")
t1 = req_body.get("t1") t1 = req_body.get("t1")
t2 = req_body.get("t2") t2 = req_body.get("t2")
field = req_body.get("field") field = req_body.get("field")
value = req_body.get("value") value = req_body.get("value")
fieldValuePairs = req_body.get("field_value_pairs") fieldValuePairs = req_body.get("field_value_pairs")
response = general_search(type, search_term, t1, t2, fieldValuePairs) response = general_search(search_term, t1, t2, fieldValuePairs)
return response, 200 return response, 200
...@@ -111,7 +111,7 @@ def get_fields_for_index(index): ...@@ -111,7 +111,7 @@ def get_fields_for_index(index):
return fields_list return fields_list
def general_search(type=None, search_term=None, t1=None, t2=None, fieldValuePairs=None): def general_search(search_term=None, t1=None, t2=None, fieldValuePairs=None):
""" """
Main method for seaching stuff in the opensearch db Main method for seaching stuff in the opensearch db
...@@ -149,9 +149,6 @@ def general_search(type=None, search_term=None, t1=None, t2=None, fieldValuePair ...@@ -149,9 +149,6 @@ def general_search(type=None, search_term=None, t1=None, t2=None, fieldValuePair
"is_public", "is_public",
] ]
queries = [] queries = []
if type is not None:
logging.debug("search for specific index: %s", type)
index = type
if search_term is not None: if search_term is not None:
logging.debug('query has search_term present') logging.debug('query has search_term present')
text_query = { text_query = {
...@@ -177,6 +174,10 @@ def general_search(type=None, search_term=None, t1=None, t2=None, fieldValuePair ...@@ -177,6 +174,10 @@ def general_search(type=None, search_term=None, t1=None, t2=None, fieldValuePair
logging.debug('query has fieldValuePairs present') logging.debug('query has fieldValuePairs present')
musts = [] musts = []
for field, value in fieldValuePairs.items(): for field, value in fieldValuePairs.items():
if field == "type" and value in searchable_indices:
logging.debug("search for specific index: %s", value)
index = value
continue
if field in field_list: if field in field_list:
if field.startswith(index) and "." in field: if field.startswith(index) and "." in field:
new_field = field[field.index(".") + 1:len(field)] new_field = field[field.index(".") + 1:len(field)]
......
...@@ -6,8 +6,9 @@ class SearchService { ...@@ -6,8 +6,9 @@ class SearchService {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios.get(`/api/search/${type}/fields`, { headers: { Accept: 'application/json' } }) axios.get(`/api/search/${type}/fields`, { headers: { Accept: 'application/json' } })
.then((response) => { .then((response) => {
const jsonResponse = response.data const json = response.data
resolve(jsonResponse) console.debug('fields result', json)
resolve(json)
}) })
.catch((error) => { .catch((error) => {
const { code, message } = error const { code, message } = error
...@@ -18,13 +19,15 @@ class SearchService { ...@@ -18,13 +19,15 @@ class SearchService {
}) })
} }
search (type, searchTerm, keyValuePairs) { search (searchData) {
// transform values to what the search API expects
const searchTerm = searchData.search_term
delete searchData.search_term
searchData = Object.fromEntries(Object.entries(searchData).filter(([_, v]) => v != null)) // https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript
const payload = { const payload = {
type,
search_term: searchTerm, search_term: searchTerm,
field_value_pairs: { ...keyValuePairs } field_value_pairs: { ...searchData }
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios.post('/api/search', payload, { headers: { Accept: 'application/json' } }) axios.post('/api/search', payload, { headers: { Accept: 'application/json' } })
.then((response) => { .then((response) => {
......
...@@ -147,8 +147,7 @@ ...@@ -147,8 +147,7 @@
<v-row> <v-row>
<v-col cols="auto"> <v-col cols="auto">
<v-select <v-select
v-model="advancedSearchType" v-model="advancedSearchData.type"
clearable
:items="fieldItems" :items="fieldItems"
item-text="name" item-text="name"
item-value="value" item-value="value"
...@@ -270,12 +269,11 @@ export default { ...@@ -270,12 +269,11 @@ export default {
{ name: 'False', value: false } { name: 'False', value: false }
], ],
fieldsResponse: null, fieldsResponse: null,
advancedSearchType: null,
advancedSearchData: { advancedSearchData: {
name: '', name: null,
internal_name: '', internal_name: null,
id: '', id: null,
type: '' type: null
} }
} }
}, },
...@@ -326,7 +324,7 @@ export default { ...@@ -326,7 +324,7 @@ export default {
return this.$store.state.databaseCount return this.$store.state.databaseCount
}, },
hideFields () { hideFields () {
const selectedOption = this.advancedSearchType const selectedOption = this.advancedSearchData.type
return { return {
hideNameField: selectedOption === 'identifier', hideNameField: selectedOption === 'identifier',
hideInternalNameField: ['identifier', 'user', 'concept', 'unit'].includes(selectedOption) hideInternalNameField: ['identifier', 'user', 'concept', 'unit'].includes(selectedOption)
...@@ -377,19 +375,17 @@ export default { ...@@ -377,19 +375,17 @@ export default {
}) })
} }
}, },
advancedSearchType: { 'advancedSearchData.type': {
async handler () { handler (newType, oldType) {
if (this.advancedSearchType) { if (!newType) {
const promise = await SearchService.getFields(this.advancedSearchType.toLowerCase()) return
this.fieldsResponse = JSON.parse(JSON.stringify(promise))
console.log('Fields Response: ', this.fieldsResponse)
} else {
// Clear form fields from advancedSearchData if fields dropdown is cleared
for (const key in this.advancedSearchData) {
delete this.advancedSearchData[key]
}
this.fieldsResponse = null // Reset fieldsResponse when type is cleared from user
} }
console.debug('switched advanced search type to', newType)
this.resetAdvancedSearchFields()
SearchService.getFields(newType)
.then((response) => {
this.fieldsResponse = response
})
}, },
immediate: true immediate: true
} }
...@@ -414,6 +410,12 @@ export default { ...@@ -414,6 +410,12 @@ export default {
submit () { submit () {
this.$refs.form.validate() this.$refs.form.validate()
}, },
/* Removes all advanced search fields when switching the type */
resetAdvancedSearchFields () {
Object.keys(this.advancedSearchData)
.filter(k => !['name', 'internal_name', 'id', 'type'].includes(k))
.forEach(k => delete this.advancedSearchData[k])
},
login () { login () {
const redirect = ![undefined, '/', '/login'].includes(this.$router.currentRoute.path) const redirect = ![undefined, '/', '/login'].includes(this.$router.currentRoute.path)
this.$router.push({ path: '/login', query: redirect ? { redirect: this.$router.currentRoute.path } : {} }) this.$router.push({ path: '/login', query: redirect ? { redirect: this.$router.currentRoute.path } : {} })
...@@ -487,6 +489,7 @@ export default { ...@@ -487,6 +489,7 @@ export default {
console.debug('runtime config', this.$config) console.debug('runtime config', this.$config)
}, },
advancedSearch () { advancedSearch () {
console.debug('performing advanced search')
if (this.search) { if (this.search) {
this.advancedSearchData.search_term = this.search this.advancedSearchData.search_term = this.search
} else { } else {
...@@ -500,7 +503,7 @@ export default { ...@@ -500,7 +503,7 @@ export default {
}, },
isAdvancedSearchEmpty () { isAdvancedSearchEmpty () {
return !( return !(
this.advancedSearchType || this.advancedSearchData.type ||
this.advancedSearchData.id || this.advancedSearchData.id ||
this.advancedSearchData.name || this.advancedSearchData.name ||
this.advancedSearchData.internal_name this.advancedSearchData.internal_name
...@@ -550,12 +553,12 @@ export default { ...@@ -550,12 +553,12 @@ export default {
// Generates a dynamic v-model; It will be attached to the advancedSearchData object // Generates a dynamic v-model; It will be attached to the advancedSearchData object
if (!item) { return '' } if (!item) { return '' }
return `${this.advancedSearchType}.${item.attribute_name}` return `${this.advancedSearchData.type}.${item.attribute_name}`
}, },
shouldRenderItem (item) { shouldRenderItem (item) {
// Checks if item's attribute_name matches any wanted field // Checks if item's attribute_name matches any wanted field
// The expected response is of a flattened format, so this method must be modified accordingly if the response is changed // The expected response is of a flattened format, so this method must be modified accordingly if the response is changed
return this.dynamicFieldsMap()[this.advancedSearchType].includes(item.attribute_name) return this.dynamicFieldsMap()[this.advancedSearchData.type].includes(item.attribute_name)
} }
}, },
head () { head () {
...@@ -565,8 +568,7 @@ export default { ...@@ -565,8 +568,7 @@ export default {
}, },
provide () { provide () {
return { return {
advancedSearchData: this.advancedSearchData, advancedSearchData: this.advancedSearchData
advancedSearchType: this.advancedSearchType
} }
} }
} }
......
...@@ -37,7 +37,7 @@ import EventBus from '@/api/eventBus' ...@@ -37,7 +37,7 @@ import EventBus from '@/api/eventBus'
import SearchService from '@/api/search.service' import SearchService from '@/api/search.service'
export default { export default {
inject: ['advancedSearchData', 'advancedSearchType'], inject: ['advancedSearchData'],
data () { data () {
return { return {
results: [], results: [],
...@@ -88,7 +88,7 @@ export default { ...@@ -88,7 +88,7 @@ export default {
}, },
created () { created () {
EventBus.$on('advancedSearchButtonClicked', () => { EventBus.$on('advancedSearchButtonClicked', () => {
this.doAdvancedSearch(this.advancedSearchType, this.advancedSearchData) this.doAdvancedSearch(this.advancedSearchData)
}) })
}, },
beforeDestroy () { beforeDestroy () {
...@@ -96,7 +96,7 @@ export default { ...@@ -96,7 +96,7 @@ export default {
}, },
mounted () { mounted () {
if (Object.keys(this.advancedSearchData).some(key => key !== 'search_term')) { if (Object.keys(this.advancedSearchData).some(key => key !== 'search_term')) {
this.doAdvancedSearch(this.advancedSearchType, this.advancedSearchData) this.doAdvancedSearch(this.advancedSearchData)
} else if (this.query) { } else if (this.query) {
this.retrieve(this.query) this.retrieve(this.query)
} }
...@@ -107,7 +107,7 @@ export default { ...@@ -107,7 +107,7 @@ export default {
return return
} }
this.loading = true this.loading = true
SearchService.search(this.type, this.query, []) SearchService.search(this.query)
.then((hits) => { .then((hits) => {
this.results = hits.map(h => h._source) this.results = hits.map(h => h._source)
}) })
...@@ -115,9 +115,9 @@ export default { ...@@ -115,9 +115,9 @@ export default {
this.loading = false this.loading = false
}) })
}, },
doAdvancedSearch (advancedSearchType, advancedSearchData) { doAdvancedSearch (advancedSearchData) {
console.debug('advanced search type:', advancedSearchType, 'data:', advancedSearchData) console.debug('advanced search data:', advancedSearchData)
SearchService.search(advancedSearchType, null, advancedSearchData) SearchService.search(advancedSearchData)
.then((response) => { .then((response) => {
this.results = response.map(h => h._source) this.results = response.map(h => h._source)
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment