view roundup/backends/indexer_postgresql_fts.py @ 6915:9ff091537f43

postgresql native-fts; more indexer tests 1) Make postgresql native-fts actually work. 2) Add simple stopword filtering to sqlite native-fts indexer. 3) Add more tests for indexer_common get_indexer Details: 1) roundup/backends/indexer_postgresql_fts.py: ignore ValueError raised if we try to index a string with a null character in it. This could happen due to an incorrect text/ mime type on a file that has nulls in it. Replace ValueError raised by postgresql with customized IndexerQueryError if a search string has a null in it. roundup/backends/rdbms_common.py: Make postgresql native-fts work. When specified it was using using whatever was returned from get_indexer(). However loading the native-fts indexer backend failed because there was no connection to the postgresql database when this call was made. Simple solution, move the call after the open_connection call in Database::__init__(). However the open_connection call creates the schema for the database if it is not there. The schema builds tables for indexer=native type indexing. As part of the build it looks at the indexer to see the min/max size of the indexed tokens. No indexer define, we get a crash. So it's a a chicken/egg issue. I solved it by setting the indexer to the Indexer from indexer_common which has the min/max token size info. I also added a no-op save_indexer to this Indexer class. I claim save_indexer() isn't needed as a commit() on the db does all the saving required. Then after open_connection is called, I call get_indexer to retrieve the correct indexer and indexer_postgresql_fts woks since the conn connection property is defined. roundup/backends/indexer_common.py: add save_index() method for indexer. It does nothing but is needed in rdbms backends during schema initialization. 2) roundup/backends/indexer_sqlite_fts.py: when this indexer is used, the indexer test in DBTest on the word "the" fail. This is due to missing stopword filtering. Implement basic stopword filtering for bare stopwords (like 'the') to make the test pass. Note: this indexer is not currently automatically run by the CI suite, it was found during manual testing. However there is a FIXME to extract the indexer tests from DBTest and run it using this backend. roundup/configuration.py, roundup/doc/admin_guide.txt: update doc on stopword use for sqlite native-fts. test/db_test_base.py: DBTest::testStringBinary creates a file with nulls in it. It was breaking postgresql with native-fts indexer. Changed test to assign mime type application/octet-stream that prevents it from being processed by any text search indexer. add test to exclude indexer searching in specific props. This code path was untested before. test/test_indexer.py: add test to call find with no words. Untested code path. add test to index and find a string with a null \x00 byte. it was tested inadvertently by testStringBinary but this makes it explicit and moves it to indexer testing. (one version each for: generic, postgresql and mysql) Renamed Get_IndexerAutoSelectTest to Get_IndexerTest and renamed autoselect tests to include autoselect. Added tests for an invalid indexer and using native-fts with anydbm (unsupported combo) to make sure the code does something useful if the validation in configuration.py is broken. test/test_liveserver.py: add test to load an issue add test using text search (fts) to find the issue add tests to find issue using postgresql native-fts test/test_postgresql.py, test/test_sqlite.py: added explanation on how to setup integration test using native-fts. added code to clean up test environment if native-fts test is run.
author John Rouillard <rouilj@ieee.org>
date Mon, 05 Sep 2022 16:25:20 -0400
parents 0d99ae7c8de6
children
line wrap: on
line source

""" This implements the PostgreSQL full-text indexer.
The table consists of (Class, propname, itemid) instances as columns
along with an _tsv tsvector column. The _tsv column is searched using
@@ and the instances returned.
"""

import re

from roundup.backends.indexer_common import Indexer as IndexerBase
from roundup.i18n import _
from roundup.cgi.exceptions import IndexerQueryError

from psycopg2.errors import InFailedSqlTransaction, SyntaxError, \
                            UndefinedObject


class Indexer(IndexerBase):
    def __init__(self, db):
        IndexerBase.__init__(self, db)
        self.db = db
        if db.conn.server_version < 110000:
            db.sql("select version()")
            server_descr = db.cursor.fetchone()
            raise ValueError("Postgres native_fts indexing requires postgres "
                             "11.0 or newer. Server is version: %s" %
                             server_descr)
        self.reindex = 0
        self.query_language = True

    def close(self):
        """close the indexing database"""
        # just nuke the circular reference
        self.db = None

    def save_index(self):
        """Save the changes to the index."""
        # not necessary - the RDBMS connection will handle this for us
        pass

    def force_reindex(self):
        """Force a reindexing of the database.  This essentially
        empties the __fts table and sets a flag so
        that the databases are reindexed"""
        self.reindex = 1

    def should_reindex(self):
        """returns True if the indexes need to be rebuilt"""
        return self.reindex

    def add_text(self, identifier, text, mime_type='text/plain'):
        """ "identifier" is  (classname, itemid, property) """
        if mime_type != 'text/plain':
            return

        # Ensure all elements of the identifier are strings 'cos the itemid
        # column is varchar even if item ids may be numbers elsewhere in the
        # code. ugh.
        identifier = tuple(map(str, identifier))

        # removed pre-processing of text that incudes only words with:
        # self.minlength <= len(word) <= self.maxlength
        # Not sure if that is correct.

        # first, find the rowid of the (classname, itemid, property)
        a = self.db.arg  # arg is the token for positional parameters
        sql = 'select ctid from __fts where _class=%s and '\
            '_itemid=%s and _prop=%s' % (a, a, a)
        self.db.cursor.execute(sql, identifier)
        r = self.db.cursor.fetchone()
        if not r:
            # not previously indexed
            sql = 'insert into __fts (_class, _itemid, _prop, _tsv)'\
                ' values (%s, %s, %s, to_tsvector(%s, %s))' % (a, a, a, a, a)
            try:
                self.db.cursor.execute(sql, identifier +
                                    (self.db.config['INDEXER_LANGUAGE'], text))
            except ValueError:
                # if text is binary or otherwise un-indexable,
                # we get a ValueError. For right now ignore it.
                pass
        else:
            id = r[0]
            sql = 'update __fts set _tsv=to_tsvector(%s, %s) where ctid=%s' % \
                  (a, a, a)
            self.db.cursor.execute(sql, (self.db.config['INDEXER_LANGUAGE'],
                                         text, id))

    def find(self, wordlist):
        """look up all the words in the wordlist.
           For testing wordlist is actually a list.
           In production, wordlist is a list of a single string
           that is a postgresql websearch_to_tsquery() query.

           https://www.postgresql.org/docs/14/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES
        """

        if not wordlist:
            return []

        a = self.db.arg  # arg is the token for positional parameters

        # removed filtering of word in wordlist to include only
        # words with:  self.minlength <= len(word) <= self.maxlength
        if wordlist[0].startswith("ts:"):
            wordlist[0] = wordlist[0][3:]
            sql = ('select _class, _itemid, _prop from __fts '
                   'where _tsv @@ to_tsquery(%s, %s)' % (a, a))

        else:
            if re.search(r'[<>!&|()*]', " ".join(wordlist)):
                # assume this is a ts query processed by websearch_to_tsquery.
                # since it has operator characters in it.
                raise IndexerQueryError(_('You have non-word/operator '
                'characters "<>!&|()*" in your query. Did you want to '
                'do a tsquery search and forgot to start it with "ts:"?'))
            else:
                sql = 'select _class, _itemid, _prop from __fts '\
                      'where _tsv @@ websearch_to_tsquery(%s, %s)' % (a, a)

        try:
            # tests supply a multi element word list. Join them.
            self.db.cursor.execute(sql, (self.db.config['INDEXER_LANGUAGE'],
                                         " ".join(wordlist),))
        except SyntaxError as e:
            # reset the cursor as it's invalid currently
            # reuse causes an InFailedSqlTransaction
            self.db.rollback()

            raise IndexerQueryError(e.args[0])
        except ValueError as e:
            # raised when search string has a null bytes in it or
            # is otherwise unsuitable.
            raise IndexerQueryError(
                "Invalid search string, do you have a null in there? " + e.args[0])
        except InFailedSqlTransaction:
            # reset the cursor as it's invalid currently
            self.db.rollback()
            raise
        except UndefinedObject as e:
            # try for a nicer user error
            self.db.rollback()
            lookfor = ('text search configuration "%s" does '
                       'not exist' % self.db.config['INDEXER_LANGUAGE'])
            if lookfor in e.args[0]:
                raise ValueError(_("Check tracker config.ini for a bad "
                                   "indexer_language setting. Error is: %s") %
                                 e)
            else:
                raise

        return self.db.cursor.fetchall()

Roundup Issue Tracker: http://roundup-tracker.org/