AutoCompletionTools { public static String preprocessQuery(String queryString, String idField) { StringBuilder subquery1 = new StringBuilder(); StringBuilder subquery2 = new StringBuilder(); StringBuilder subquery3 = new StringBuilder(); List<String> list = AutoCompletionTools.split(queryString); if (list.isEmpty()) { return null; } int charCount = 0; for (String string : list) { charCount += string.length(); string = QueryParser.escape(string); if (subquery1.length() == 0) { subquery1.append('('); subquery2.append("(\""); if (idField != null) { subquery3.append("("); subquery3.append(idField); subquery3.append(":\""); } } else { subquery1.append(" AND "); subquery2.append(' '); if (idField != null) { subquery3.append(' '); } } subquery1.append(string); subquery1.append('*'); subquery2.append(string); if (idField != null) { subquery3.append(string); } } subquery1.append(')'); subquery2.append("\"^2)"); subquery3.append("\")"); if (charCount < 2) { return null; } StringBuilder sb = new StringBuilder(subquery1); sb.append(" OR "); sb.append(subquery2); if (idField != null) { sb.append(" OR "); sb.append(subquery3); } return sb.toString(); }  static List<String> split(String s); static String preprocessQuery(String queryString, String idField); static void sortbyLabelLength(List<SearchResult> documents); static boolean fEquals(float f1, float f2);  }
@Test public void ttt() { assertEquals(null, AutoCompletionTools.preprocessQuery("", null)); assertEquals(null, AutoCompletionTools.preprocessQuery("", "id")); assertEquals(null, AutoCompletionTools.preprocessQuery(" ", null)); assertEquals(null, AutoCompletionTools.preprocessQuery(" ", "id")); assertEquals(null, AutoCompletionTools.preprocessQuery(" a ", null)); assertEquals(null, AutoCompletionTools.preprocessQuery(" a ", "id")); assertEquals("(ab*) OR (\"ab\"^2)", AutoCompletionTools.preprocessQuery(" ab ", null)); assertEquals("(ab*) OR (\"ab\"^2) OR (id:\"ab\")", AutoCompletionTools.preprocessQuery(" ab ", "id")); assertEquals("(ab\\(2\\+*) OR (\"ab\\(2\\+\"^2) OR (id:\"ab\\(2\\+\")", AutoCompletionTools.preprocessQuery(" ab(2+ ", "id")); assertEquals("(a* AND b*) OR (\"a b\"^2)", AutoCompletionTools.preprocessQuery(" a b ", null)); assertEquals("(a* AND b*) OR (\"a b\"^2) OR (id:\"a b\")", AutoCompletionTools.preprocessQuery(" a b ", "id")); assertEquals("(a\\:b*) OR (\"a\\:b\"^2)", AutoCompletionTools.preprocessQuery(" a:b ", null)); assertEquals("(a\\:b*) OR (\"a\\:b\"^2) OR (id:\"a\\:b\")", AutoCompletionTools.preprocessQuery(" a:b ", "id")); assertEquals("(me* AND a\\:b*) OR (\"me a\\:b\"^2)", AutoCompletionTools.preprocessQuery(" me a:b ", null)); assertEquals("(me* AND a\\:b*) OR (\"me a\\:b\"^2) OR (id:\"me a\\:b\")", AutoCompletionTools.preprocessQuery(" me a:b ", "id")); }