Skip to content

Instantly share code, notes, and snippets.

@KilianB
Created October 4, 2018 09:21
Show Gist options
  • Save KilianB/fac1e6844bb5f5d19bf39886e942294a to your computer and use it in GitHub Desktop.
Save KilianB/fac1e6844bb5f5d19bf39886e942294a to your computer and use it in GitHub Desktop.
Demonstrating lucene fulltext search not working with escaped colon
package com.github.kilianB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Kilian
*
*/
public class LuceneEscapedSearch {
private static Connection conn;
public static void setup() throws SQLException {
conn = DriverManager.getConnection("jdbc:h2:~/luceneExample", "sa", "");
try (Statement stmt = conn.createStatement()) {
//Reset databse
stmt.execute("DROP ALL OBJECTS");
// Create table
stmt.execute("CREATE TABLE t(path VARCHAR(100), PRIMARY KEY(path) )");
// Insert dummy entries
stmt.executeUpdate("INSERT INTO t VALUES('C:\\HelloWorld\\RandomPath1.txt')");
stmt.executeUpdate("INSERT INTO t VALUES('C:\\HelloWorld\\RandomPath2.txt')");
stmt.executeUpdate("INSERT INTO t VALUES('C:\\HelloWorld\\RandomPath3.txt')");
// Initialize full text search
stmt.execute("CREATE ALIAS IF NOT EXISTS FTL_INIT FOR \"org.h2.fulltext.FullTextLucene.init\";");
stmt.execute("CALL FTL_INIT();");
stmt.execute("CALL FTL_CREATE_INDEX('PUBLIC', 'T', 'PATH');");
stmt.execute("CALL FTL_REINDEX();"); // Just to be sure
}
}
private static boolean searchWithoutDelimiter() throws SQLException {
try (Statement stmt = conn.createStatement()) {
ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM FTL_SEARCH('C*',0,0);");
if (rs.next()) {
return rs.getInt(1) == 3;
}
}
return false;
}
private static boolean searchWithDelimiter() throws SQLException {
try (Statement stmt = conn.createStatement()) {
ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM FTL_SEARCH('C\\:*',0,0);");
if (rs.next()) {
return rs.getInt(1) == 3;
}
}
return false;
}
public static void main(String[] args) throws SQLException{
try {
setup();
System.out.printf("%-7s : %b %n","Without",searchWithoutDelimiter());
System.out.printf("%-7s : %b %n","With",searchWithDelimiter());
} catch (SQLException e) {
e.printStackTrace();
}finally {
//teardown
conn.close();
}
}
private static String escapeLuceneQuery(String input) {
// +\\-:!(){}[\\]^\"~*?:
input = input.replace("\\", "\\\\");
input = input.replaceAll("([+\\-:!(){}\\[\\]^\"~*?]|(&&))", "\\\\$1");
input = input.replace("||", "\\||");
return input;
}
/*
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.2</version>
</dependency>
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment