Skip to content
Snippets Groups Projects
Commit db697a35 authored by esikkala's avatar esikkala
Browse files

Add option for hiding unknown value in facets

parent 1fccd67c
No related branches found
No related tags found
No related merge requests found
......@@ -99,6 +99,11 @@ export const getFacet = async ({
})
}
}
if (facetConfig.hideUnknownValue) {
q = q.replace(/<UNKNOWN_VALUES>/g, '')
} else {
q = q.replace(/<UNKNOWN_VALUES>/g, unknownBlock)
}
q = q.replace('<SELECTED_VALUES>', selectedBlock)
q = q.replace('<SELECTED_VALUES_NO_HITS>', selectedNoHitsBlock)
q = q.replace(/<FACET_VALUE_FILTER>/g, facetConfig.facetValueFilter)
......@@ -159,6 +164,7 @@ export const getFacet = async ({
endpoint: endpoint.url,
useAuth: endpoint.useAuth,
resultMapper: mapper,
resultMapperConfig: facetConfig,
resultFormat
})
if (facetConfig.type === 'hierarchical') {
......@@ -267,3 +273,25 @@ export const generateSelectedFilter = ({
FILTER(?id ${inverse ? 'NOT' : ''} IN ( ${selections} ))
`)
}
const unknownBlock = `
UNION
{
# 'Unknown' facet value for results with no predicate path
{
SELECT DISTINCT (count(DISTINCT ?instance) as ?instanceCount) {
<FILTER>
VALUES ?facetClass { <FACET_CLASS> }
?instance a ?facetClass .
FILTER NOT EXISTS {
?instance <MISSING_PREDICATE> [] .
}
}
}
FILTER(?instanceCount > 0)
BIND(IRI("http://ldf.fi/MISSING_VALUE") AS ?id)
# prefLabel for <http://ldf.fi/MISSING_VALUE> is given in client/translations
BIND('0' as ?parent)
BIND(<UNKNOWN_SELECTED> as ?selected)
}
`
......@@ -28,16 +28,32 @@ export const mapCount = sparqlBindings => {
return sparqlBindings[0].count.value
}
export const mapFacet = (sparqlBindings, previousSelections) => {
export const mapFacet = ({ sparqlBindings, config }) => {
let results = []
if (sparqlBindings.length > 0) {
results = mapFacetValues(sparqlBindings)
results = sparqlBindings.map(b => {
try {
return {
id: b.id.value,
prefLabel: b.prefLabel
? b.prefLabel.value
: '0', // temporary prefLabel for <http://ldf.fi/MISSING_VALUE> to support sorting
selected: b.selected.value,
parent: b.parent ? b.parent.value : null,
instanceCount: b.instanceCount.value
}
} catch (err) {
console.log(err)
}
return null
})
return results
}
return results
}
export const mapHierarchicalFacet = (sparqlBindings, previousSelections) => {
const results = mapFacetValues(sparqlBindings)
export const mapHierarchicalFacet = ({ sparqlBindings, config }) => {
const results = mapFacet({ sparqlBindings, config })
let treeData = getTreeFromFlatData({
flatData: results,
getKey: node => node.id, // resolve a node's key
......@@ -51,7 +67,7 @@ export const mapHierarchicalFacet = (sparqlBindings, previousSelections) => {
})
}
export const mapTimespanFacet = sparqlBindings => {
export const mapTimespanFacet = ({ sparqlBindings, config }) => {
const b = sparqlBindings[0]
return {
min: b.min.value,
......@@ -201,26 +217,6 @@ const trimResult = arr => {
return arr.slice(i, j + 1)
}
const mapFacetValues = sparqlBindings => {
const results = sparqlBindings.map(b => {
try {
return {
id: b.id.value,
prefLabel: b.prefLabel
? b.prefLabel.value
: '0', // temporary prefLabel for <http://ldf.fi/MISSING_VALUE> to support sorting
selected: b.selected.value,
parent: b.parent ? b.parent.value : null,
instanceCount: b.instanceCount.value
}
} catch (err) {
console.log(err)
}
return null
})
return results
}
const comparator = (a, b) => {
if (Array.isArray(a.prefLabel)) {
a.prefLabel = a.prefLabel[0]
......
......@@ -72,25 +72,7 @@ export const facetValuesQuery = `
<FACET_VALUE_FILTER>
<LABELS>
}
UNION
{
# 'Unknown' facet value for results with no predicate path
{
SELECT DISTINCT (count(DISTINCT ?instance) as ?instanceCount) {
<FILTER>
VALUES ?facetClass { <FACET_CLASS> }
?instance a ?facetClass .
FILTER NOT EXISTS {
?instance <MISSING_PREDICATE> [] .
}
}
}
FILTER(?instanceCount > 0)
BIND(IRI("http://ldf.fi/MISSING_VALUE") AS ?id)
# prefLabel for <http://ldf.fi/MISSING_VALUE> is given in client/translations
BIND('0' as ?parent)
BIND(<UNKNOWN_SELECTED> as ?selected)
}
<UNKNOWN_VALUES>
}
<ORDER_BY>
`
......
......@@ -37,6 +37,7 @@ export const perspective1Config = {
author: {
id: 'author',
facetValueFilter: '',
hideUnknownValue: true,
label: 'Author',
labelPath: 'mmm-schema:manuscript_author/skos:prefLabel',
predicate: 'mmm-schema:manuscript_author',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment