Skip to content

Commit

Permalink
entity details
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Nov 13, 2024
1 parent c867ae1 commit 99d86ae
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 77 deletions.
30 changes: 17 additions & 13 deletions src/models/generated/schemasPublic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,30 @@ export interface TopicMention {


/**
* An entity like location, person, etc
* An entity: location or person.
*/
export interface EntityDetails {
/**
* Unique identifier of the entity
*/
uid: string;
uid?: string;
/**
* Entity label
*/
label?: string;
type?: "person" | "location";
/**
* Wikidata identifier of the entity.
*/
wikidataId?: string;
/**
* Entity name
* Total number of mentions of the entity.
*/
name: string;
type: "person" | "location";
totalMentions?: number;
/**
* Total number of content items the entity is mentioned in.
*/
totalContentItems?: number;
}


Expand Down Expand Up @@ -246,10 +258,6 @@ export interface SearchFacetBucket {
* Value that represents the bucket.
*/
value: string | number;
/**
* Unique ID of the value, if relevant and different from the value itself.
*/
uid?: string;
/**
* Label of the value, if relevant.
*/
Expand Down Expand Up @@ -290,10 +298,6 @@ export interface SearchFacetBucket {
* Value that represents the bucket.
*/
value: string | number;
/**
* Unique ID of the value, if relevant and different from the value itself.
*/
uid?: string;
/**
* Label of the value, if relevant.
*/
Expand Down
21 changes: 16 additions & 5 deletions src/schema/schemasPublic/EntityDetails.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "An entity like location, person, etc",
"description": "An entity: location or person.",
"type": "object",
"title": "EntityDetails",
"additionalProperties": false,
Expand All @@ -9,14 +9,25 @@
"type": "string",
"description": "Unique identifier of the entity"
},
"name": {
"label": {
"type": "string",
"description": "Entity name"
"description": "Entity label"
},
"type": {
"type": "string",
"enum": ["person", "location"]
},
"wikidataId": {
"type": "string",
"description": "Wikidata identifier of the entity."
},
"totalMentions": {
"type": "integer",
"description": "Total number of mentions of the entity."
},
"totalContentItems": {
"type": "integer",
"description": "Total number of content items the entity is mentioned in."
}
},
"required": ["uid", "type", "name"]
}
}
6 changes: 5 additions & 1 deletion src/services/entities/entities.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Service {
}

async find(params: Params<FindQuery> & Sanitized<FindQuery> & WithUser) {
return await this._find(params)
}

async _find(params: Params<FindQuery> & Sanitized<FindQuery> & WithUser) {
const qp = params.query!
debug('[find] with params:', qp)

Expand Down Expand Up @@ -209,7 +213,7 @@ class Service {
}

async get(id: string, params: any) {
return this.find({
return await this._find({
...params,
query: {
resolve: true,
Expand Down
122 changes: 66 additions & 56 deletions src/services/entities/entities.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { SolrNamespaces } from '../../solr'
import { authenticateAround as authenticate } from '../../hooks/authenticate'
import { rateLimit } from '../../hooks/rateLimiter'

const { validate, validateEach, queryWithCommonParams, utils } = require('../../hooks/params')
const { qToSolrFilter, filtersToSolrQuery } = require('../../hooks/search')
import { transformResponseDataItem, transformResponse } from '../../hooks/transformation'
import { inPublicApi } from '../../hooks/redaction'
import { transformEntityDetails } from '../../transformers/entity'

const orderByMap = {
relevance: 'score ASC',
Expand All @@ -16,63 +21,68 @@ const orderByMap = {

export const orderByValues = Object.keys(orderByMap)

const findAndGetParamsHooks = [
validate({
q: {
required: false,
min_length: 1,
max_length: 200,
},
resolve: {
required: false,
transform: () => true,
},
order_by: utils.orderBy({
values: orderByMap,
defaultValue: '-count',
}),
}),
validateEach(
'filters',
{
q: {
max_length: 200,
required: false,
},
context: {
choices: ['include', 'exclude'],
defaultValue: 'include',
},
op: {
choices: ['AND', 'OR'],
defaultValue: 'OR',
},
type: {
choices: ['string', 'type', 'uid', 'wikidataId'],
required: true,
// trasform is required because they shoyd be related to entities namespace.
// transform: (d) => {
// if (d === 'uid') {
// return d;
// }
// return `entity-${d}`;
// },
},
},
{
required: false,
}
),
qToSolrFilter('string'),
filtersToSolrQuery({
solrIndexProvider: () => SolrNamespaces.Entities,
}),
queryWithCommonParams(),
]

export default {
around: {
all: [authenticate(), rateLimit()],
},
before: {
all: [],
find: [
validate({
q: {
required: false,
min_length: 1,
max_length: 200,
},
resolve: {
required: false,
transform: () => true,
},
order_by: utils.orderBy({
values: orderByMap,
defaultValue: '-count',
}),
}),
validateEach(
'filters',
{
q: {
max_length: 200,
required: false,
},
context: {
choices: ['include', 'exclude'],
defaultValue: 'include',
},
op: {
choices: ['AND', 'OR'],
defaultValue: 'OR',
},
type: {
choices: ['string', 'type', 'uid', 'wikidataId'],
required: true,
// trasform is required because they shoyd be related to entities namespace.
// transform: (d) => {
// if (d === 'uid') {
// return d;
// }
// return `entity-${d}`;
// },
},
},
{
required: false,
}
),
qToSolrFilter('string'),
filtersToSolrQuery({
solrIndexProvider: () => SolrNamespaces.Entities,
}),
queryWithCommonParams(),
],
get: [],
find: findAndGetParamsHooks,
get: findAndGetParamsHooks,
create: [],
update: [],
patch: [],
Expand All @@ -81,8 +91,8 @@ export default {

after: {
all: [],
find: [],
get: [],
find: [transformResponseDataItem(transformEntityDetails, inPublicApi)],
get: [transformResponse(transformEntityDetails, inPublicApi)],
create: [],
update: [],
patch: [],
Expand Down
4 changes: 2 additions & 2 deletions src/services/newspapers/newspapers.hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { authenticateAround as authenticate } from '../../hooks/authenticate'
import { rateLimit } from '../../hooks/rateLimiter'
import { OrderByChoices } from './newspapers.schema'
import { transformResponseDataItem } from '../../hooks/transformation'
import { transformResponseDataItem, transformResponse } from '../../hooks/transformation'
import { inPublicApi } from '../../hooks/redaction'
import { transformNewspaper } from '../../transformers/newspaper'

Expand Down Expand Up @@ -70,7 +70,7 @@ module.exports = {
after: {
all: [returnCachedContents(), saveResultsInCache()],
find: [transformResponseDataItem(transformNewspaper, inPublicApi)],
get: [],
get: [transformResponse(transformNewspaper, inPublicApi)],
create: [],
update: [],
patch: [],
Expand Down
13 changes: 13 additions & 0 deletions src/transformers/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { EntityDetails } from '../models/generated/schemas'
import { EntityDetails as EntityDetailsPublic } from '../models/generated/schemasPublic'

export const transformEntityDetails = (input: EntityDetails): EntityDetailsPublic => {
return {
uid: input.uid,
label: input.name,
totalContentItems: input.countItems,
totalMentions: input.countMentions,
type: input.type,
wikidataId: input.wikidataId,
}
}

0 comments on commit 99d86ae

Please sign in to comment.