Skip to content

Commit

Permalink
Fix when there's more than one annotatedField
Browse files Browse the repository at this point in the history
  • Loading branch information
KCMertens committed Apr 9, 2024
1 parent 25c00ea commit 5e8809b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/frontend/src/pages/search/results/Sort.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default Vue.extend({
opts.push(...getAnnotationSubset(
this.annotations,
this.corpus.annotationGroups,
this.corpus.annotatedFields.contents.annotations,
this.corpus.annotatedFields[this.corpus.mainAnnotatedField].annotations,
'Sort',
this.corpus.textDirection,
debug.debug
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/pages/search/results/groupby/GroupBy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
:options="annotations"
v-model="current.annotation"
/>.
<br>
<label><input type="checkbox" v-model="current.caseSensitive"> Case sensitive</label>

<div style="margin: 0.75em 0 1.5em 0;" v-if="context === 'context'">
Expand Down Expand Up @@ -100,6 +101,7 @@
:options="metadata"
/>
</section>
<br>
<label><input type="checkbox" v-model="current.caseSensitive"> Case sensitive</label>
</template>
<div v-else class="text-secondary h4" style="height: 100%; width: 100%; margin: 0; display: flex; align-items: center;">In this window you can apply grouping to the results. Click the buttons on the left to create a grouping criteria to get started.</div>
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/store/search/corpus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getState = b.state();

const get = {
/** All annotations, without duplicates and in no specific order */
allAnnotations: b.read((state): NormalizedAnnotation[] => Object.values(state.corpus?.annotatedFields ?? {}).flatMap(f => Object.values(f.annotations)), 'allAnnotations'),
allAnnotations: b.read((state): NormalizedAnnotation[] => Object.values(state.corpus?.annotatedFields[state.corpus.mainAnnotatedField].annotations ?? {}), 'allAnnotations'),
allAnnotationsMap: b.read((state): MapOf<NormalizedAnnotation> => mapReduce(get.allAnnotations(), 'id'), 'allAnnotationsMap'),

allMetadataFields: b.read((state): NormalizedMetadataField[] => Object.values(state.corpus?.metadataFields || {}), 'allMetadataFields'),
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/types/apptypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export type NormalizedIndexBase = {
/** Contains information about the internal structure of the index - which fields exist for tokens, which metadata fields exist for documents, etc */
export type NormalizedIndex = NormalizedIndexBase&{
annotatedFields: { [id: string]: NormalizedAnnotatedField; };
/** Key info annotatedFields */
mainAnnotatedField: string;
/**
* If no groups are defined by blacklab itself, all annotations of all annotatedFields are placed in generated groups.
* Note that an annotation may be part of more than one group.
Expand Down
15 changes: 5 additions & 10 deletions src/frontend/src/types/blacklabtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export interface BLMetadataField {
}

/** Contains information about the internal structure of the index - which fields exist for tokens, which metadata fields exist for documents, etc */
export interface BLIndexMetadataInternal {
export interface BLIndexMetadata {
/** Always present, except in really old versions of blacklab */
annotationGroups?: {
[annotatedFieldId: string]: Array<{
Expand Down Expand Up @@ -297,19 +297,14 @@ export interface BLIndexMetadataInternal {
/** yyyy-mm-dd hh:mm:ss */
timeModified: string;
};
}
type BLIndexMetadataV1 = BLIndexMetadataInternal&{
complexFields: {[id: string]: BLAnnotatedFieldV1};
};
type BLIndexMetadataV2 = BLIndexMetadataInternal&{

annotatedFields: {[id: string]: BLAnnotatedFieldV2};
/** key into annotatedFields */
mainAnnotatedField: string;
/** Only available if index contains actual documents and if versionInfo.blackLabVersion >= 2.0.0 */
documentCount?: number;
documentCount: number;
};

export type BLIndexMetadata = BLIndexMetadataV1|BLIndexMetadataV2;
export function isIndexMetadataV1(v: BLIndexMetadata): v is BLIndexMetadataV1 { return (v as any).complexFields != null; }

export type BLDocument = {
docPid: string;
docInfo: BLDocInfo;
Expand Down
99 changes: 51 additions & 48 deletions src/frontend/src/utils/blacklabutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,57 +112,59 @@ function normalizeAnnotatedField(field: BLTypes.BLAnnotatedField): NormalizedAnn

function normalizeAnnotationGroups(blIndex: BLTypes.BLIndexMetadata): NormalizedAnnotationGroup[] {
let annotationGroupsNormalized: NormalizedAnnotationGroup[] = [];
for (const [fieldId, field] of Object.entries(BLTypes.isIndexMetadataV1(blIndex) ? blIndex.complexFields : blIndex.annotatedFields) as Array<[string, BLTypes.BLAnnotatedField]>) {
const annotations = BLTypes.isAnnotatedFieldV1(field) ? field.properties : field.annotations;
const idsNotInGroups = new Set(Object.keys(annotations));

let hasUserDefinedGroup = false;

// Copy all predefined groups, removing nonexistant annotations and groups
if (blIndex.annotationGroups && blIndex.annotationGroups[fieldId]) {
for (const group of blIndex.annotationGroups[fieldId]) {
const normalizedGroup: NormalizedAnnotationGroup = {
annotatedFieldId: fieldId,
id: group.name,
entries: group.annotations.filter(id => annotations[id] != null),
isRemainderGroup: false
};
if (normalizedGroup.entries.length) {
annotationGroupsNormalized.push(normalizedGroup);
normalizedGroup.entries.forEach(id => idsNotInGroups.delete(id));
hasUserDefinedGroup = true;
}
const fieldId = blIndex.mainAnnotatedField;
const field = blIndex.annotatedFields[fieldId];

const annotations = BLTypes.isAnnotatedFieldV1(field) ? field.properties : field.annotations;
const idsNotInGroups = new Set(Object.keys(annotations));

let hasUserDefinedGroup = false;

// Copy all predefined groups, removing nonexistant annotations and groups
if (blIndex.annotationGroups && blIndex.annotationGroups[fieldId]) {
for (const group of blIndex.annotationGroups[fieldId]) {
const normalizedGroup: NormalizedAnnotationGroup = {
annotatedFieldId: fieldId,
id: group.name,
entries: group.annotations.filter(id => annotations[id] != null),
isRemainderGroup: false
};
if (normalizedGroup.entries.length) {
annotationGroupsNormalized.push(normalizedGroup);
normalizedGroup.entries.forEach(id => idsNotInGroups.delete(id));
hasUserDefinedGroup = true;
}
}
}

// Add all remaining annotations to the remainder group.
// First add all explicitly ordered annotations (annotatedField.displayOrder).
// Finally add everything else at the end, sorted by their displayNames.
if (idsNotInGroups.size) {
const remainingAnnotationsToAdd = new Set(idsNotInGroups);
const idsInRemainderGroup: string[] = [];

// annotations in displayOrder
if (!BLTypes.isAnnotatedFieldV1(field) && field.displayOrder) {
field.displayOrder.forEach(id => {
if (remainingAnnotationsToAdd.has(id)) {
remainingAnnotationsToAdd.delete(id);
idsInRemainderGroup.push(id);
}
});
}
// Finally all annotations without entry in displayOrder
idsInRemainderGroup.push(...[...remainingAnnotationsToAdd].sort((a, b) => annotations[a].displayName.localeCompare(annotations[b].displayName)));
// And create the group.
annotationGroupsNormalized.push({
annotatedFieldId: fieldId,
entries: idsInRemainderGroup,
id: 'Other',
// If there was a group defined from the index config, this is indeed the remainder group, otherwise this is just a normal group.
isRemainderGroup: hasUserDefinedGroup
// Add all remaining annotations to the remainder group.
// First add all explicitly ordered annotations (annotatedField.displayOrder).
// Finally add everything else at the end, sorted by their displayNames.
if (idsNotInGroups.size) {
const remainingAnnotationsToAdd = new Set(idsNotInGroups);
const idsInRemainderGroup: string[] = [];

// annotations in displayOrder
if (!BLTypes.isAnnotatedFieldV1(field) && field.displayOrder) {
field.displayOrder.forEach(id => {
if (remainingAnnotationsToAdd.has(id)) {
remainingAnnotationsToAdd.delete(id);
idsInRemainderGroup.push(id);
}
});
}
// Finally all annotations without entry in displayOrder
idsInRemainderGroup.push(...[...remainingAnnotationsToAdd].sort((a, b) => annotations[a].displayName.localeCompare(annotations[b].displayName)));
// And create the group.
annotationGroupsNormalized.push({
annotatedFieldId: fieldId,
entries: idsInRemainderGroup,
id: 'Other',
// If there was a group defined from the index config, this is indeed the remainder group, otherwise this is just a normal group.
isRemainderGroup: hasUserDefinedGroup
});
}

return annotationGroupsNormalized;
}

Expand Down Expand Up @@ -218,7 +220,7 @@ export function normalizeIndexBase(blIndex: BLTypes.BLIndex, id: string): Normal
export function normalizeIndex(blIndex: BLTypes.BLIndexMetadata): NormalizedIndex {
const annotationGroupsNormalized = normalizeAnnotationGroups(blIndex);
const metadataGroupsNormalized = normalizeMetadataGroups(blIndex);
const annotatedFields: BLTypes.BLAnnotatedField[] = Object.values(BLTypes.isIndexMetadataV1(blIndex) ? blIndex.complexFields : blIndex.annotatedFields);
const annotatedFields: BLTypes.BLAnnotatedField[] = Object.values(blIndex.annotatedFields);

return {
annotatedFields: mapReduce(annotatedFields.map(normalizeAnnotatedField), 'id'),
Expand All @@ -229,7 +231,7 @@ export function normalizeIndex(blIndex: BLTypes.BLIndexMetadata): NormalizedInde
// If BlackLab is an old format, this property doesn't exist
// If BlackLab is new, and the property is still missing, it's 0 (tokenCount and documentCount are always omitted when 0)
// Encode this in the fallback value, then later request the actual number of documents
documentCount: !BLTypes.isIndexMetadataV1(blIndex) ? blIndex.documentCount || 0 : -1,
documentCount: blIndex.documentCount,
documentFormat: blIndex.documentFormat,
fieldInfo: blIndex.fieldInfo,
id: blIndex.indexName,
Expand All @@ -240,7 +242,8 @@ export function normalizeIndex(blIndex: BLTypes.BLIndexMetadata): NormalizedInde
timeModified: blIndex.versionInfo.timeModified,
tokenCount: blIndex.tokenCount || 0,
status: blIndex.status,
indexProgress: blIndex.indexProgress || null
indexProgress: blIndex.indexProgress || null,
mainAnnotatedField: blIndex.mainAnnotatedField,
};
}

Expand Down

0 comments on commit 5e8809b

Please sign in to comment.