idok-commit AT lists.psi.ch
Subject: Commit emails of the iDok project
List archive
[idok-commit] idok commit r268 - in trunk/sites/psi/java/ch/psi/idok/gwt/twiki: client junit
Chronological Thread
- From: "AFS account Roman Geus" <geus AT savannah.psi.ch>
- To: idok-commit AT lists.psi.ch
- Subject: [idok-commit] idok commit r268 - in trunk/sites/psi/java/ch/psi/idok/gwt/twiki: client junit
- Date: Wed, 1 Oct 2008 14:54:20 +0200
- List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
- List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>
Author: geus
Date: Wed Oct 1 14:54:20 2008
New Revision: 268
Log:
Implemented conversion of TWiki query syntax to Lucene query syntax,
including a JUnit test
Added:
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/TwikiToLuceneConverter.java
(contents, props changed)
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/junit/
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/junit/TwikiToLuceneConverterTest.java
(contents, props changed)
Modified:
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/IdokSearchTwikiMashup.java
Modified:
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/IdokSearchTwikiMashup.java
==============================================================================
---
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/IdokSearchTwikiMashup.java
(original)
+++
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/IdokSearchTwikiMashup.java
Wed Oct 1 14:54:20 2008
@@ -36,7 +36,7 @@
/**
* Base URL of iDok search ReST interface
*/
- static final String BASEURL = "https://dms02.psi.ch/api/v1/search/";
+ static final String REST_BASE_URL =
"https://dms02.psi.ch/api/v1/search/";
/**
* Render diagnostic output if set to true
@@ -108,7 +108,7 @@
// Call iDok search ReST interface
if (queryParam != null && repository != null)
- doRestCall(repository, queryParam);
+ doRestCall(repository,
TwikiToLuceneConverter.convert(queryParam));
}
/**
@@ -121,7 +121,7 @@
* the iDok query string
*/
protected void doRestCall(String repository, String query) {
- String url = BASEURL + repository + "?q=" + query
+ String url = REST_BASE_URL + repository + "?q=" + query
+ "&outputfields=env,meta&num=" + MAX_HITS;
GWT.log("Requesting " + url + " using JSONP", null);
@@ -330,7 +330,7 @@
if (numHits == MAX_HITS)
idokJumpToResultsPanel
.add(new HTML(
- "<p>The maximum possible number of iDok
search results has been returned. There are likely to be more documents that
match your query. Please refine your search criteria to reduce the number of
search results.</p>"));
+ "<p>The maximum possible number of iDok
search results have been returned. There are likely to be more documents that
match your query. Please refine your search criteria to reduce the number of
search results.</p>"));
} catch (Throwable e) {
GWT.log("Error in 'renderQueryResult()' method", e);
Added:
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/TwikiToLuceneConverter.java
==============================================================================
--- (empty file)
+++
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/client/TwikiToLuceneConverter.java
Wed Oct 1 14:54:20 2008
@@ -0,0 +1,136 @@
+package ch.psi.idok.gwt.twiki.client;
+
+/**
+ * Convert TWiki query string to an equivalent Lucene query string
+ *
+ * @see ch.psi.idok.gwt.twiki.junit.TwikiToLuceneConverterTest
+ */
+public class TwikiToLuceneConverter {
+
+ /**
+ * TWiki input query string
+ */
+ private String twikiQuery;
+
+ /**
+ * Current position in the input query string
+ */
+ private int index;
+
+ /**
+ * Current query string term
+ */
+ private String currentTerm;
+
+ /**
+ * true if currentTerm is an exclude term
+ */
+ private boolean isExcludeTerm;
+
+ /**
+ * Convert a TWiki query string to an equivalent Lucene query string
+ *
+ * @param twikiQuery
+ * TWiki query string
+ * @return the converted Lucene query string
+ */
+ public static String convert(String twikiQuery) {
+ StringBuffer buf = new StringBuffer();
+ TwikiToLuceneConverter conv = new TwikiToLuceneConverter(twikiQuery);
+ int i = 0;
+ while (true) {
+ conv.getNextTerm();
+ if (conv.currentTerm.length() == 0)
+ break;
+ if (conv.isExcludeTerm)
+ buf.append(" NOT ");
+ else if (i > 0)
+ buf.append(" AND ");
+ buf.append(quote(conv.currentTerm));
+ i++;
+ }
+ return buf.toString();
+ }
+
+ private TwikiToLuceneConverter(String twikiQuery) {
+ this.twikiQuery = twikiQuery.trim();
+ index = 0;
+ }
+
+ /**
+ * Read the next query term
+ *
+ * Makes the next term available in the currentTerm field and sets the
+ * isExcludeTerm field.
+ */
+ private void getNextTerm() {
+ StringBuffer buf = new StringBuffer();
+
+ // advance to first non-space char
+ char ch = getNextChar();
+ while (ch == ' ')
+ ch = getNextChar();
+
+ isExcludeTerm = false;
+ if (ch == '+') {
+ // + is ignored
+ ch = getNextChar();
+ } else if (ch == '-') {
+ // - means exclude term
+ isExcludeTerm = true;
+ ch = getNextChar();
+ }
+
+ while (ch != ' ' && ch != '\0') {
+ if (ch == '"') {
+ // consume "
+ ch = getNextChar();
+ while (ch != '"' && ch != '\0') {
+ // write non "-char and consume
+ buf.append(ch);
+ ch = getNextChar();
+ }
+ // consume " or \0
+ ch = getNextChar();
+ } else {
+ // write and consume non space char
+ buf.append(ch);
+ ch = getNextChar();
+ }
+ }
+ currentTerm = buf.toString();
+ }
+
+ /**
+ * Consume and return the next character of the input query string
+ *
+ * As a side-effect the index field is updated.
+ *
+ * @return the next character of the input query string or '\0' no more
+ * characters are available
+ */
+ private char getNextChar() {
+ if (index >= twikiQuery.length())
+ return '\0';
+ else
+ return twikiQuery.charAt(index++);
+ }
+
+ /**
+ * Quote special characters of Lucene query term
+ *
+ * @param term
+ * the term to be quoted
+ * @return the term with special characters quoted
+ */
+ private static String quote(String term) {
+ StringBuffer buf = new StringBuffer();
+ for (char ch : term.toCharArray()) {
+ if (" +-&|!(){}[]^\"~*?:\\".indexOf(ch) != -1)
+ buf.append('\\');
+ buf.append(ch);
+ }
+ return buf.toString();
+ }
+
+}
Added:
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/junit/TwikiToLuceneConverterTest.java
==============================================================================
--- (empty file)
+++
trunk/sites/psi/java/ch/psi/idok/gwt/twiki/junit/TwikiToLuceneConverterTest.java
Wed Oct 1 14:54:20 2008
@@ -0,0 +1,28 @@
+package ch.psi.idok.gwt.twiki.junit;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import ch.psi.idok.gwt.twiki.client.TwikiToLuceneConverter;
+
+public class TwikiToLuceneConverterTest {
+
+ @Test
+ public void testConvert() {
+ assertEquals("roman AND geus", TwikiToLuceneConverter
+ .convert("roman geus"));
+ assertEquals("roman NOT geus", TwikiToLuceneConverter
+ .convert("roman -geus"));
+ assertEquals("roman\\ geus AND florian", TwikiToLuceneConverter
+ .convert("\"roman geus\" florian"));
+ assertEquals("rom\\(a\\)n\\ geus AND florian NOT hübner",
+ TwikiToLuceneConverter
+ .convert("\"rom(a)n geus\" +florian -hübner"));
+ assertEquals("roman", TwikiToLuceneConverter.convert("rom\"an"));
+ assertEquals("", TwikiToLuceneConverter.convert(""));
+ assertEquals("", TwikiToLuceneConverter.convert(" "));
+ assertEquals("roman", TwikiToLuceneConverter.convert(" roman "));
+ }
+
+}
- [idok-commit] idok commit r268 - in trunk/sites/psi/java/ch/psi/idok/gwt/twiki: client junit, AFS account Roman Geus, 10/01/2008
Archive powered by MHonArc 2.6.19.