marsad-location / code /marsad_location_schema.sql
rafiulbiswas's picture
Upload 3 files
38ac623 verified
Raw History Blame Contribute Delete
5.01 kB
-- ============================================================
-- MARSAD location extraction: Supabase / PostgreSQL schema
-- ADAPT: replace public.posts / public.topics / user id types with the real MARSAD tables before applying.
-- ============================================================
-- Places actually used by MARSAD (filled on demand from the gazetteer, not all 450k places)
create table if not exists public.geo_places (
place_id text primary key, -- 'gn:360630', 'cc:QA', 'osm:N123', 'rg:gulf'
level text not null, -- region|country|state|county|city|district|street|poi|natural|other
name_en text,
name_ar text,
country_code text,
country_name text,
state_name text,
city_name text,
parent_id text,
lat double precision,
lon double precision,
population bigint default 0
);
create index if not exists geo_places_country_idx on public.geo_places (country_code);
-- One row per location mention in a post
create table if not exists public.post_locations (
id bigserial primary key,
post_id bigint not null, -- ADAPT type + add: references public.posts(id) on delete cascade
topic_id bigint, -- ADAPT / optional
mention text not null,
start_char int,
end_char int,
detected_by text, -- model|flag|coordinates|country_code|abbreviation|hashtag|stretched_word
detection_score real,
predicted_type text,
resolution_status text not null, -- resolved|country_only|ambiguous|unresolved
decided_by text,
confidence real,
location_role text not null default 'mentioned', -- mentioned|author|origin|destination|negated
place_id text references public.geo_places(place_id),
entity_type text,
country_code text,
country text,
state text,
county text,
city text,
district text,
landmark text,
name_ar text,
latitude double precision,
longitude double precision,
country_inferred boolean,
spelling_corrected boolean,
candidates jsonb, -- ranked options when ambiguous
model_version text not null,
created_at timestamptz not null default now()
);
create index if not exists post_locations_post_idx on public.post_locations (post_id);
create index if not exists post_locations_country_idx on public.post_locations (country_code);
create index if not exists post_locations_place_idx on public.post_locations (place_id);
create index if not exists post_locations_status_idx on public.post_locations (resolution_status);
create unique index if not exists post_locations_uniq on public.post_locations (post_id, start_char, end_char, model_version);
-- One row per user: where the author is (self-statement, profile field), strongest evidence wins
create table if not exists public.user_locations (
user_id text primary key, -- ADAPT type
country_code text,
country text,
city text,
place_id text references public.geo_places(place_id),
evidence text, -- profile_field|self_statement
confidence real,
n_posts int default 0,
model_version text,
updated_at timestamptz not null default now()
);
-- Daily counts for the map (country -> state -> city). ADAPT the join to the real posts table / date column.
create materialized view if not exists public.location_daily as
select
date_trunc('day', p.created_at)::date as day,
pl.topic_id,
pl.country_code,
coalesce(pl.country, pl.country_code) as country,
coalesce(pl.state, '(unknown state)') as state,
coalesce(pl.city, '(unknown city)') as city,
count(*) as mentions,
count(distinct pl.post_id) as posts
from public.post_locations pl
join public.posts p on p.id = pl.post_id -- ADAPT
where pl.resolution_status in ('resolved', 'country_only')
and pl.location_role <> 'negated'
and pl.country_code is not null
group by 1, 2, 3, 4, 5, 6;
create index if not exists location_daily_idx on public.location_daily (day, country_code);
-- refresh on a schedule (pg_cron or the enrichment job):
-- refresh materialized view public.location_daily;
-- Dashboard queries
-- countries: select country_code, country, sum(posts) from location_daily where day between $1 and $2 group by 1,2;
-- drill-down: select state, city, sum(posts) from location_daily where country_code = $3 and day between $1 and $2 group by 1,2;
-- posts: select p.* from post_locations pl join posts p on p.id = pl.post_id
-- where pl.country_code = $3 and pl.city = $4 order by p.created_at desc limit 100;