{
// Check which tokens we have, get the ID numbers
$sSQL = 'SELECT word_id, word_token, type';
- $sSQL .= " info->>'cc' as country, info->>'postcode' as postcode";
+ $sSQL .= " info->>'cc' as country, info->>'postcode' as postcode,";
+ $sSQL .= " info->>'word' as word, info->>'op' as operator,";
+ $sSQL .= " info->>'class' as class, info->>'type' as type";
$sSQL .= ' FROM word WHERE word_token in (';
$sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
) {
continue;
}
+ $sNormPostcode = $this->normalizeString($aWord['postcode']);
+ if (strpos($sNormQuery, $sNormPostcode) === false) {
+ continue;
+ }
$oToken = new Token\Postcode($iId, $aWord['postcode'], null);
+ break;
+ 'S': // tokens for classification terms (special phrases)
+ if ($aWord['class'] === null || $aWord['type'] === null
+ || $aWord['word'] === null
+ || strpos($sNormQuery, $aWord['word']) === false
+ ) {
+ continue;
+ }
+ $oToken = new Token\SpecialTerm(
+ $iId,
+ $aWord['class'],
+ $aWord['type'],
+ $aWord['op'] ? Operator::NEAR : Operator::NONE
+ );
+ break;
default:
continue;
}
def update_special_phrases(self, phrases, should_replace):
""" Replace the search index for special phrases with the new phrases.
+ If `should_replace` is True, then the previous set of will be
+ completely replaced. Otherwise the phrases are added to the
+ already existing ones.
"""
norm_phrases = set(((self.name_processor.get_normalized(p[0]), p[1], p[2], p[3])
for p in phrases))
with self.conn.cursor() as cur:
# Get the old phrases.
existing_phrases = set()
- cur.execute("""SELECT word, class, type, operator FROM word
- WHERE class != 'place'
- OR (type != 'house' AND type != 'postcode')""")
- for label, cls, typ, oper in cur:
- existing_phrases.add((label, cls, typ, oper or '-'))
+ cur.execute("SELECT info FROM word WHERE type = 'S'")
+ for (info, ) in cur:
+ existing_phrases.add((info['word'], info['class'], info['type'],
+ info.get('op') or '-'))
added = self._add_special_phrases(cur, norm_phrases, existing_phrases)
if should_replace:
for word, cls, typ, oper in to_add:
term = self.name_processor.get_search_normalized(word)
if term:
- copystr.add(word, ' ' + term, cls, typ,
- oper if oper in ('in', 'near') else None, 0)
+ copystr.add(term, 'S',
+ {'word': word, 'class': cls, 'type': typ,
+ 'op': oper if oper in ('in', 'near') else None})
added += 1
copystr.copy_out(cursor, 'word',
- columns=['word', 'word_token', 'class', 'type',
- 'operator', 'search_name_count'])
+ columns=['word_token', 'type', 'info'])
return added
if to_delete:
cursor.execute_values(
""" DELETE FROM word USING (VALUES %s) as v(name, in_class, in_type, op)
- WHERE word = name and class = in_class and type = in_type
- and ((op = '-' and operator is null) or op = operator)""",
- to_delete)
+ WHERE info->>'word' = name
+ and info->>'class' = in_class and info->>'type' = in_type
+ and ((op = '-' and info->>'op' is null) or op = info->>'op')
+ """, to_delete)
return len(to_delete)