1 -- Get tokens used for searching the given place.
3 -- These are the tokens that will be saved in the search_name table.
4 CREATE OR REPLACE FUNCTION token_get_name_search_tokens(info JSONB)
7 SELECT (info->>'names')::INTEGER[]
8 $$ LANGUAGE SQL IMMUTABLE STRICT;
11 -- Get tokens for matching the place name against others.
13 -- This should usually be restricted to full name tokens.
14 CREATE OR REPLACE FUNCTION token_get_name_match_tokens(info JSONB)
17 SELECT (info->>'names')::INTEGER[]
18 $$ LANGUAGE SQL IMMUTABLE STRICT;
21 -- Return the housenumber tokens applicable for the place.
22 CREATE OR REPLACE FUNCTION token_get_housenumber_search_tokens(info JSONB)
25 SELECT (info->>'hnr_tokens')::INTEGER[]
26 $$ LANGUAGE SQL IMMUTABLE STRICT;
29 -- Return the housenumber in the form that it can be matched during search.
30 CREATE OR REPLACE FUNCTION token_normalized_housenumber(info JSONB)
34 $$ LANGUAGE SQL IMMUTABLE STRICT;
37 CREATE OR REPLACE FUNCTION token_addr_street_match_tokens(info JSONB)
40 SELECT (info->>'street')::INTEGER[]
41 $$ LANGUAGE SQL IMMUTABLE STRICT;
44 CREATE OR REPLACE FUNCTION token_addr_place_match_tokens(info JSONB)
47 SELECT (info->>'place_match')::INTEGER[]
48 $$ LANGUAGE SQL IMMUTABLE STRICT;
51 CREATE OR REPLACE FUNCTION token_addr_place_search_tokens(info JSONB)
54 SELECT (info->>'place_search')::INTEGER[]
55 $$ LANGUAGE SQL IMMUTABLE STRICT;
58 DROP TYPE IF EXISTS token_addresstoken CASCADE;
59 CREATE TYPE token_addresstoken AS (
65 CREATE OR REPLACE FUNCTION token_get_address_tokens(info JSONB)
66 RETURNS SETOF token_addresstoken
68 SELECT key, (value->>1)::int[] as match_tokens,
69 (value->>0)::int[] as search_tokens
70 FROM jsonb_each(info->'addr');
71 $$ LANGUAGE SQL IMMUTABLE STRICT;
74 CREATE OR REPLACE FUNCTION token_normalized_postcode(postcode TEXT)
77 SELECT CASE WHEN postcode SIMILAR TO '%(,|;)%' THEN NULL ELSE upper(trim(postcode))END;
78 $$ LANGUAGE SQL IMMUTABLE STRICT;
81 -- Return token info that should be saved permanently in the database.
82 CREATE OR REPLACE FUNCTION token_strip_info(info JSONB)
86 $$ LANGUAGE SQL IMMUTABLE STRICT;
88 --------------- private functions ----------------------------------------------
90 CREATE OR REPLACE FUNCTION getorcreate_full_word(norm_term TEXT, lookup_terms TEXT[],
92 OUT partial_tokens INT[])
95 partial_terms TEXT[] = '{}'::TEXT[];
100 SELECT min(word_id) INTO full_token
101 FROM word WHERE word = norm_term and type = 'W';
103 IF full_token IS NULL THEN
104 full_token := nextval('seq_word');
105 INSERT INTO word (word_id, word_token, type, word, info)
106 SELECT full_token, lookup_term, 'W', norm_term,
107 json_build_object('count', 0)
108 FROM unnest(lookup_terms) as lookup_term;
111 FOR term IN SELECT unnest(string_to_array(unnest(lookup_terms), ' ')) LOOP
113 IF NOT (ARRAY[term] <@ partial_terms) THEN
114 partial_terms := partial_terms || term;
118 partial_tokens := '{}'::INT[];
119 FOR term IN SELECT unnest(partial_terms) LOOP
120 SELECT min(word_id), max(info->>'count') INTO term_id, term_count
121 FROM word WHERE word_token = term and type = 'w';
123 IF term_id IS NULL THEN
124 term_id := nextval('seq_word');
126 INSERT INTO word (word_id, word_token, type, info)
127 VALUES (term_id, term, 'w', json_build_object('count', term_count));
130 IF term_count < {{ max_word_freq }} THEN
131 partial_tokens := array_merge(partial_tokens, ARRAY[term_id]);
139 CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
145 SELECT min(word_id) INTO return_id FROM word
146 WHERE word_token = lookup_term and type = 'H';
148 IF return_id IS NULL THEN
149 return_id := nextval('seq_word');
150 INSERT INTO word (word_id, word_token, type)
151 VALUES (return_id, lookup_term, 'H');