Implement search filter for all columns

2020-05-08 07:05发布

I found this search example in PostgreSQL http://www.postgresql.org/docs/current/interactive/textsearch-tables.html#TEXTSEARCH-TABLES-SEARCH

I tried to implement this code for this table this way:

CREATE TABLE ACCOUNT(
 ID INTEGER NOT NULL,
 USER_NAME TEXT,
 PASSWD TEXT,
 FIRST_NAME TEXT,
 LAST_NAME TEXT,
 LAST_LOGIN DATE,
 DATE_REGISTERED DATE,
 ROLE INTEGER,
 CAN_LOGIN INTEGER
)
;

-- ADD KEYS FOR TABLE ACCOUNT

ALTER TABLE ACCOUNT ADD CONSTRAINT KEY1 PRIMARY KEY (ID)
;

Java code

public List<AccountsObj> list(int firstRow, int rowCount, String sortField, boolean sortAscending) throws SQLException
    {
        String SqlStatement = null;

        Connection conn = ds.getConnection();
        if (conn == null)
        {
            throw new SQLException();
        }

        String sortDirection = sortAscending ? "ASC" : "DESC";

        SqlStatement = "SELECT * FROM ACCOUNT "
//            + " WHERE ? IS NULL OR ? IN (USER_NAME, FIRST_NAME, LAST_NAME)"
            + " WHERE to_tsvector('english', USER_NAME || ' ' ) @@ plainto_tsquery(?)"
            + " ORDER BY %S %S offset ? limit ? ";

        String sql = String.format(SqlStatement, sortField, sortDirection);

        PreparedStatement ps = null;
        ResultSet resultSet = null;
        List<AccountsObj> resultList = new ArrayList<>();

        try
        {
            conn.setAutoCommit(false);
            boolean committed = false;

            ps = conn.prepareStatement(sql);

            ps.setString(1, searchString);
            ps.setInt(2, firstRow);
            ps.setInt(3, rowCount);

            resultSet = ps.executeQuery();
            resultList = ProcessorArrayList(resultSet);

            conn.commit();
            committed = true;

        }
        finally
        {
            ps.close();
            conn.close();
        }

        return resultList;
    }

But I have two issues. When search string is empty the table is also empty. How I can solve this? Also how I can implement this search for every table column?

P.S I tried WHERE to_tsvector('english', USER_NAME || ' ' ) @@ plainto_tsquery(?) IS NOT NULL

But search filter is not applied.

1条回答
贪生不怕死
2楼-- · 2020-05-08 07:20

You will have to add your 'null guard' to the the fulltext search and use to_tsquery instead of plainto_tsquery (in order for prefix search to work).

SqlStatement = "SELECT * FROM ACCOUNT "
    + " WHERE (trim(?) = '') IS NOT FALSE"
    + " OR to_tsvector('english', USER_NAME || ' ' || FIRST_NAME || ' ' || LAST_NAME ) @@  to_tsquery(?)"
    + " ORDER BY user_name ASC offset ? limit ? ";

and add the searchString to your PreparedStatement a second time

 ps = conn.prepareStatement(sql);

 ps.setString(1, searchString);
 ps.setString(2, searchString);
 ps.setInt(3, firstRow);
 ps.setInt(4, rowCount);

Note using a fulltext search you will not be able to search for word-parts (like %user%, %name or us%name). You can search for prefixes though, e.g. user:*

查看更多
登录 后发表回答