Skip to content

Commit

Permalink
fix: source filter in occurrence search page
Browse files Browse the repository at this point in the history
  • Loading branch information
JJJHANG committed Jan 31, 2024
1 parent 632297a commit a28f365
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
26 changes: 13 additions & 13 deletions apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def occurrence_search_v2(request):

for key, values in request.GET.lists():
if key in facet_values:
facet_selected[key] = values
facet_selected[key] = values

solr = SolrQuery('taibif_occurrence', facet_values)
req = solr.request(query_list)
Expand Down Expand Up @@ -551,18 +551,18 @@ def occurrence_search_v2(request):
#print ('--------', i, facet_selected[key], selected_facet_menu[key], menus[i])
tmp_menu = selected_facet_menu[key].copy()
tmp_menu_add = []
for selected in facet_selected[key]:
filtered = list(filter(lambda x: x['key'] == selected, tmp_menu['rows']))
if len(filtered) == 0 and len(tmp_menu['rows']) > 0:
#print(key, selected, tmp_menu)
tmp_menu['rows'].pop()
count = 0
for item in menus[i]['rows']:
#print (key, item['key'], selected, item['count'])
if str(item['key']) == str(selected):
count = item['count']
break
tmp_menu_add.append((selected, count))
# for selected in facet_selected[key]:
# filtered = list(filter(lambda x: x['key'] == selected, tmp_menu['rows']))
# if len(filtered) == 0 and len(tmp_menu['rows']) > 0:
# #print(key, selected, tmp_menu)
# tmp_menu['rows'].pop()
# count = 0
# for item in menus[i]['rows']:
# #print (key, item['key'], selected, item['count'])
# if str(item['key']) == str(selected):
# count = item['count']
# break
# tmp_menu_add.append((selected, count))
for x in tmp_menu_add:
tmp_menu['rows'].append({
'key': x[0],
Expand Down
10 changes: 6 additions & 4 deletions frontend-data/src/SearchSidebar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useRef} from 'react';
import React, {useState, useRef, forwardRef, useImperativeHandle} from 'react';
//import Accordion from "./components/Accordion";
//import Tree from "./components/Tree";
import SearchTaxon from './SearchSidebarTaxon';
Expand Down Expand Up @@ -51,6 +51,10 @@ function Accordion(props) {
setYearValue(yearSelected)
props.clearCondition(event,content.key)
};
// const handleClearClick = () => {
// yearSelected = [1795, 2023]
// setYearValue(yearSelected);
// };

const datasetMenuItems = content.rows.map((x) => {
if (content.key === 'dataset'){
Expand Down Expand Up @@ -121,13 +125,12 @@ function Accordion(props) {
<input type="checkbox" onChange={(e)=> {e.persist(); onClick(e, content.key, x.key)}} checked={itemChecked} />
<span className="checkmark"></span>
<span className="search-sidebar-count-group">
<Translation>{t => <span className="name">{t(x.label === false ? "GBIF" : x.label === true ? "TaiBIF IPT" : x.label)}</span>}</Translation>
<Translation>{t => <span className="name">{t(x.label == false ? 'GBIF' : 'TaiBIF IPT')}</span>}</Translation>
<span className="count">{count}</span>
</span>
</label>
</div>
);

} else {
const count = (x.count) >=0 ? x.count.toLocaleString() : null;
const itemChecked = filters.has(`${content.key}=${x.key}`);
Expand Down Expand Up @@ -235,7 +238,6 @@ function Accordion(props) {
}

function SearchSidebar(props) {
//console.log(props);
let isOccurrence = false;
let searchTypeLabel = '';
const [queryKeyword, setQueryKeyword] = useState(props.queryKeyword);
Expand Down
6 changes: 5 additions & 1 deletion frontend-data/src/TaibifSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class TaibifSearch extends React.Component {
}
});
filters.add(`year=${itemKey}`);
}
}

this.applyFilters(filters);
}
Expand Down Expand Up @@ -311,6 +311,10 @@ class TaibifSearch extends React.Component {
}
apiUrl = `${apiUrl}${facetQueryString}`;

// if (apiUrl.includes('selfProduced=')) {
// apiUrl = apiUrl.replace('selfProduced=', 'selfProduced:')
// }

window.history.pushState({stateObj:url}, "", url);

console.log('fetch:', apiUrl)
Expand Down
1 change: 0 additions & 1 deletion frontend-data/src/occurrence/OccurrenceSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export default function OccurrenceSearch(props) {
const bor = bor_allow.includes(row.basisOfRecord) ? row.basisOfRecord : (row.taibif_basisOfRecord ? row.taibif_basisOfRecord : "");
const datasetKey = row.taibifDatasetID ? row.taibifDatasetID : (row.taibif_datasetKey ? row.taibif_datasetKey : '')


return (
<tr key={index} onClick={(e)=>{window.location.href=`/occurrence/${row.taibif_occ_id}`}} className={classes.occurrenceRow}>
<td>{ sn }</td>
Expand Down
23 changes: 5 additions & 18 deletions utils/solr_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ def generate_solr_url(self, req_lists=[]):
self.solr_tuples.append(('fq', f'{key}:[{vlist[0]} TO {vlist[1]}]'))
else:
if key in JSON_FACET_MAP[self.core]:
self.solr_tuples.append(('fq', '{}:"{}"'.format(field, values[0])))
if key == 'selfProduced': # 布林值搜尋 value 不需要轉成 string
self.solr_tuples.append(('fq', '{}:{}'.format(field, values[0])))
else:
self.solr_tuples.append(('fq', '{}:"{}"'.format(field, values[0])))
else:
self.solr_tuples.append(('fq', ' OR '.join([f'{field}:"{x}"' for x in values])))
#self.solr_tuples.append(('fq', 'taibif_dataset_name:A OR taibif_dataset_name:B'))
Expand Down Expand Up @@ -417,26 +420,10 @@ def get_menus(self, key=''):
'label': '授權類型 Licence',
'rows': rows,
})

# Connect dataset page to occurrence page, using the key datasetKey
if data := resp['facets'].get('taibif_datasetKey', ''):
dataset_id = resp['facets'].get('dataset_id', '')
rows = []
for x in range(len(data['buckets'])):
if x < len(dataset_id['buckets']): # prevent limited dataset_id buckets cause index error
rows.append({
'key': dataset_id['buckets'][x]['val'],
'label': data['buckets'][x]['val'],
'count': data['buckets'][x]['count']
})
menus.append({
'key': 'taibif_datasetKey',
'label': '',
'rows': rows,
})

if data := resp['facets'].get('selfProduced', ''):
rows = [{'key': x['val'], 'label': x['val'], 'count': x['count']} for x in data['buckets']]

menus.append({
'key':'selfProduced',
'label': '資料來源 Source',
Expand Down

0 comments on commit a28f365

Please sign in to comment.