如何在java中使用H2数据库的全文搜索功能?

时间:2011-07-07 13:56:30

标签: java database indexing h2

任何人都可以借助java程序解释如何在H2嵌入式数据库中使用全文搜索。

执行“SELECT * FROM FT_SEARCH_DATA('word',0,0)”返回一个选择查询字符串。我是否需要再次执行此操作才能获得最终结果?

1 个答案:

答案 0 :(得分:6)

使用H2全文索引的Java程序是the unit test program。一个例子:

    Connection conn = ...
    Statement stat = conn.createStatement();
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_INIT FOR \"org.h2.fulltext.FullText.init\"");
    stat.execute("CALL FT_INIT()");
    FullText.setIgnoreList(conn, "to,this");
    FullText.setWhitespaceChars(conn, " ,.-");
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
    stat.execute("INSERT INTO TEST VALUES(1, 'Welcome to this world, One_Word')");
    stat.execute("CALL FT_CREATE_INDEX('PUBLIC', 'TEST', NULL)");
    ResultSet rs;
    rs = stat.executeQuery("SELECT * FROM FT_SEARCH('Welcome', 0, 0)");
    assertTrue(rs.next());
    assertEquals("QUERY", rs.getMetaData().getColumnLabel(1));
    assertEquals("SCORE", rs.getMetaData().getColumnLabel(2));
    assertEquals("\"PUBLIC\".\"TEST\" WHERE \"ID\"=1", rs.getString(1));
    assertEquals("1.0", rs.getString(2));
    rs = stat.executeQuery("SELECT * FROM FT_SEARCH_DATA('One', 0, 0)");
    assertFalse(rs.next());
    rs = stat.executeQuery("SELECT * FROM FT_SEARCH_DATA('One_Word', 0, 0)");
    assertTrue(rs.next());
    rs = stat.executeQuery("SELECT * FROM FT_SEARCH_DATA('Welcome', 0, 0)");
    assertTrue(rs.next());

还有SQL script example。也许您对Javadocs for the FullText class感兴趣 - 这将解释FT_SEARCH_DATA / searchData返回的内容。