incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [3/3] git commit: Changed the super query from super:<....> to just <....>.
Date Sun, 28 Jul 2013 19:08:08 GMT
Changed the super query from super:<....> to just <....>.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/4b5f5c07
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/4b5f5c07
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/4b5f5c07

Branch: refs/heads/master
Commit: 4b5f5c074177745c4dd54204e9d02620c7e7c28c
Parents: 26606f2
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Sun Jul 28 15:06:34 2013 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Sun Jul 28 15:06:34 2013 -0400

----------------------------------------------------------------------
 .../apache/blur/manager/IndexManagerTest.java   |   6 +-
 .../apache/blur/lucene/search/SuperParser.java  | 104 +++++++++++++------
 .../apache/blur/lucene/search/SuperQuery.java   |   4 +-
 .../blur/lucene/search/SuperParserTest.java     |  22 ++--
 4 files changed, 87 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/4b5f5c07/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java b/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
index 56db377..b462438 100644
--- a/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
+++ b/blur-core/src/test/java/org/apache/blur/manager/IndexManagerTest.java
@@ -351,7 +351,7 @@ public class IndexManagerTest {
   public void testQueryWithJoinAll() throws Exception {
     BlurQuery blurQuery = new BlurQuery();
     blurQuery.simpleQuery = new SimpleQuery();
-    blurQuery.simpleQuery.queryStr = "+super:<+test-family.testcol12:value101 +test-family.testcol13:value102>
+super:<test-family2.testcol18:value501>";
+    blurQuery.simpleQuery.queryStr = "+<+test-family.testcol12:value101 +test-family.testcol13:value102>
+<test-family2.testcol18:value501>";
 
     blurQuery.simpleQuery.superQueryOn = true;
     blurQuery.simpleQuery.type = ScoreType.SUPER;
@@ -377,7 +377,7 @@ public class IndexManagerTest {
   public void testQueryWithJoin() throws Exception {
     BlurQuery blurQuery = new BlurQuery();
     blurQuery.simpleQuery = new SimpleQuery();
-    blurQuery.simpleQuery.queryStr = "+super:<+test-family.testcol12:value101 +test-family.testcol13:value102>
+super:<test-family2.testcol18:value501>";
+    blurQuery.simpleQuery.queryStr = "+<+test-family.testcol12:value101 +test-family.testcol13:value102>
+<test-family2.testcol18:value501>";
     blurQuery.simpleQuery.superQueryOn = true;
     blurQuery.simpleQuery.type = ScoreType.SUPER;
     blurQuery.fetch = 10;
@@ -402,7 +402,7 @@ public class IndexManagerTest {
   public void testQueryWithJoinForcingSuperQuery() throws Exception {
     BlurQuery blurQuery = new BlurQuery();
     blurQuery.simpleQuery = new SimpleQuery();
-    blurQuery.simpleQuery.queryStr = "+super:<test-family.testcol1:value1> +super:<test-family.testcol3:value234123>";
+    blurQuery.simpleQuery.queryStr = "+<test-family.testcol1:value1> +<test-family.testcol3:value234123>";
     blurQuery.simpleQuery.superQueryOn = true;
     blurQuery.simpleQuery.type = ScoreType.SUPER;
     blurQuery.fetch = 10;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/4b5f5c07/blur-query/src/main/java/org/apache/blur/lucene/search/SuperParser.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/lucene/search/SuperParser.java b/blur-query/src/main/java/org/apache/blur/lucene/search/SuperParser.java
index 015e13e..93c7738 100644
--- a/blur-query/src/main/java/org/apache/blur/lucene/search/SuperParser.java
+++ b/blur-query/src/main/java/org/apache/blur/lucene/search/SuperParser.java
@@ -2,8 +2,6 @@ package org.apache.blur.lucene.search;
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.apache.blur.analysis.BlurAnalyzer;
 import org.apache.blur.log.Log;
@@ -24,9 +22,15 @@ public class SuperParser extends BlurQueryParser {
 
   private static Log LOG = LogFactory.getLog(SuperParser.class);
 
+  interface Group {
+    void match(int start, int end) throws ParseException;
+  }
+
   private final String _defaultField = SUPER;
-  private static final Pattern PATTERN = Pattern.compile("super\\s*?\\:\\s*?\\<(.*?)\\>");
-  private static final Pattern CHECK = Pattern.compile("super\\s*?\\:\\s*?\\<");
+  // private static final Pattern PATTERN =
+  // Pattern.compile("super\\s*?\\:\\s*?\\<(.*?)\\>");
+  // private static final Pattern CHECK =
+  // Pattern.compile("super\\s*?\\:\\s*?\\<");
   private static final String SEP = ".";
   private final boolean _superSearch;
   private final Filter _queryFilter;
@@ -34,6 +38,8 @@ public class SuperParser extends BlurQueryParser {
   private final Version _matchVersion;
   private final Term _defaultPrimeDocTerm;
   private final String _prefixToSub = "______SUPERBASEFIELD_";
+  private int _lastStart = 0;
+  private int _lastEnd = 0;
 
   public SuperParser(Version matchVersion, BlurAnalyzer a, boolean superSearch, Filter queryFilter,
       ScoreType scoreType, Term defaultPrimeDocTerm) {
@@ -47,30 +53,22 @@ public class SuperParser extends BlurQueryParser {
     setAllowLeadingWildcard(true);
   }
 
-  public Query parse(String queryStr) throws ParseException {
-    Matcher matcher = PATTERN.matcher(queryStr);
-    Map<String, Query> subQueries = new HashMap<String, Query>();
-    int subQueryIndex = 0;
-    StringBuilder builder = new StringBuilder();
-    int lastStart = 0;
-    int lastEnd = 0;
-    while (matcher.find()) {
-      int count = matcher.groupCount();
-      int start = matcher.start();
-      int end = matcher.end();
-      if (lastStart != start) {
-        builder.append(queryStr.substring(lastEnd, start));
-      }
-      String realQuery = queryStr.substring(start, end);
-      LOG.debug("Realquery [{0}]", realQuery);
-      for (int i = 0; i < count; i++) {
-        String superQueryStr = matcher.group(i + 1);
-        Matcher matcherCheck = CHECK.matcher(superQueryStr);
-        if (matcherCheck.find()) {
-          throw new ParseException("Embedded super queries are not allowed [" + queryStr
+ "].");
+  public Query parse(final String queryStr) throws ParseException {
+    final Map<String, Query> subQueries = new HashMap<String, Query>();
+    final StringBuilder builder = new StringBuilder();
+    match(queryStr, new Group() {
+      int _subQueryIndex = 0;
+
+      @Override
+      public void match(int start, int end) throws ParseException {
+        if (_lastStart != start) {
+          builder.append(queryStr.substring(_lastEnd, start));
         }
+        String realQuery = queryStr.substring(start, end);
+        LOG.debug("Realquery [{0}]", realQuery);
+        String superQueryStr = getMatchText(realQuery);
         LOG.debug("Parseable sub query [{0}]", superQueryStr);
-        String key = _prefixToSub + subQueryIndex;
+        String key = _prefixToSub + _subQueryIndex;
         QueryParser newParser = getNewParser();
         Query query = newParser.parse(superQueryStr);
         if (!isSameGroupName(query)) {
@@ -82,19 +80,59 @@ public class SuperParser extends BlurQueryParser {
           query = wrapFilter(query);
         }
         subQueries.put(key, query);
-        builder.append(_prefixToSub).append(':').append(subQueryIndex);
-        subQueryIndex++;
+        builder.append(_prefixToSub).append(':').append(_subQueryIndex);
+        _subQueryIndex++;
+        _lastStart = start;
+        _lastEnd = end;
       }
-      lastStart = start;
-      lastEnd = end;
-    }
-    if (lastEnd < queryStr.length()) {
-      builder.append(queryStr.substring(lastEnd));
+
+      private String getMatchText(String match) {
+        return match.substring(1, match.length() - 1);
+      }
+    });
+    if (_lastEnd < queryStr.length()) {
+      builder.append(queryStr.substring(_lastEnd));
     }
     Query query = super.parse(builder.toString());
     return reprocess(replaceRealQueries(query, subQueries));
   }
 
+  private static void match(String source, Group group) throws ParseException {
+    int line = 1;
+    int length = source.length();
+    int start = -1;
+    char p = 0;
+    int column = 1;
+    for (int i = 0; i < length; i++) {
+      char c = source.charAt(i);
+      if (c == '<') {
+        if (p != '\\') {
+          if (start == -1) {
+            start = i;
+          } else {
+            throw new ParseException("Cannot parse '" + source + "': Encountered \"<\"
at line " + line + ", column "
+                + column + ".");
+          }
+        }
+      } else if (c == '>') {
+        if (p != '\\') {
+          if (start != -1) {
+            group.match(start, i + 1);
+            start = -1;
+          } else {
+            throw new ParseException("Cannot parse '" + source + "': Encountered \">\"
at line " + line + ", column "
+                + column + ".");
+          }
+        }
+      } else if (c == '\n') {
+        line++;
+        column = 0;
+      }
+      column++;
+      p = c;
+    }
+  }
+
   private SuperQuery newSuperQuery(Query query) {
     return new SuperQuery(wrapFilter(query), _scoreType, _defaultPrimeDocTerm);
   }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/4b5f5c07/blur-query/src/main/java/org/apache/blur/lucene/search/SuperQuery.java
----------------------------------------------------------------------
diff --git a/blur-query/src/main/java/org/apache/blur/lucene/search/SuperQuery.java b/blur-query/src/main/java/org/apache/blur/lucene/search/SuperQuery.java
index a25e926..6939bd6 100644
--- a/blur-query/src/main/java/org/apache/blur/lucene/search/SuperQuery.java
+++ b/blur-query/src/main/java/org/apache/blur/lucene/search/SuperQuery.java
@@ -74,11 +74,11 @@ public class SuperQuery extends AbstractWrapperQuery {
   }
 
   public String toString() {
-    return "super:<" + _query.toString() + ">";
+    return "<" + _query.toString() + ">";
   }
 
   public String toString(String field) {
-    return "super:<" + _query.toString(field) + ">";
+    return "<" + _query.toString(field) + ">";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/4b5f5c07/blur-query/src/test/java/org/apache/blur/lucene/search/SuperParserTest.java
----------------------------------------------------------------------
diff --git a/blur-query/src/test/java/org/apache/blur/lucene/search/SuperParserTest.java b/blur-query/src/test/java/org/apache/blur/lucene/search/SuperParserTest.java
index 8d95427..c1814dc 100644
--- a/blur-query/src/test/java/org/apache/blur/lucene/search/SuperParserTest.java
+++ b/blur-query/src/test/java/org/apache/blur/lucene/search/SuperParserTest.java
@@ -47,7 +47,7 @@ public class SuperParserTest {
 
   @Test
   public void test1() throws ParseException {
-    Query query = parser.parse(" +super:<a.a:a a.d:e a.b:b> ");
+    Query query = parser.parse(" +<a.a:a a.d:e a.b:b> ");
 
     BooleanQuery booleanQuery = new BooleanQuery();
     booleanQuery.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
@@ -64,7 +64,7 @@ public class SuperParserTest {
 
   @Test
   public void test2() throws ParseException {
-    Query query = parser.parse("super:<a.c:c a.d:d>");
+    Query query = parser.parse("<a.c:c a.d:d>");
 
     BooleanQuery booleanQuery = new BooleanQuery();
     booleanQuery.add(new TermQuery(new Term("a.c", "c")), Occur.SHOULD);
@@ -82,7 +82,7 @@ public class SuperParserTest {
 
   @Test
   public void test4() throws ParseException {
-    Query query = parser.parse("super:<a.a:a a.d:e a.b:b>  -super:<b.c:c b.d:d>");
+    Query query = parser.parse("<a.a:a a.d:e a.b:b>  -<b.c:c b.d:d>");
 
     BooleanQuery booleanQuery1 = new BooleanQuery();
     booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
@@ -107,7 +107,7 @@ public class SuperParserTest {
   public void test5() throws ParseException {
     parser = new SuperParser(LUCENE_VERSION, new BlurAnalyzer(new WhitespaceAnalyzer(LUCENE_VERSION)),
true, null,
         ScoreType.SUPER, new Term("_primedoc_"));
-    Query query = parser.parse("super:<a.a:a a.d:{e TO f} a.b:b a.test:hello\\<>
-super:<g.c:c g.d:d>");
+    Query query = parser.parse("<a.a:a a.d:{e TO f} a.b:b a.test:hello\\<> -<g.c:c
g.d:d>");
 
     BooleanQuery booleanQuery1 = new BooleanQuery();
     booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
@@ -235,28 +235,28 @@ public class SuperParserTest {
 
   @Test
   public void test21() throws ParseException {
-    Query q = parseSq("super:<f1:word1> word2");
+    Query q = parseSq("<f1:word1> word2");
     Query q1 = bq(bc(sq(tq("f1", "word1"))), bc(sq(tq("super", "word2"))));
     assertQuery(q1, q);
   }
 
   @Test
   public void test22() throws ParseException {
-    Query q = parseSq("super:<f1:word1> word2 super:<word3>");
+    Query q = parseSq("<f1:word1> word2 <word3>");
     Query q1 = bq(bc(sq(tq("f1", "word1"))), bc(sq(tq("super", "word2"))), bc(sq(tq("super",
"word3"))));
     assertQuery(q1, q);
   }
 
   @Test
   public void test23() throws ParseException {
-    Query q = parseSq("super:<f1:word1>  super:<word3> word2");
+    Query q = parseSq("<f1:word1>  <word3> word2");
     Query q1 = bq(bc(sq(tq("f1", "word1"))), bc(sq(tq("super", "word3"))), bc(sq(tq("super",
"word2"))));
     assertQuery(q1, q);
   }
 
   @Test
   public void test24() throws ParseException {
-    Query q = parseSq("super:<f1:word1> +word6 super:<word3> word2");
+    Query q = parseSq("<f1:word1> +word6 <word3> word2");
     Query q1 = bq(bc(sq(tq("f1", "word1"))), bc_m(sq(tq("super", "word6"))), bc(sq(tq("super",
"word3"))),
         bc(sq(tq("super", "word2"))));
     assertQuery(q1, q);
@@ -264,7 +264,7 @@ public class SuperParserTest {
 
   @Test
   public void test25() throws ParseException {
-    Query q = parseSq("+leading super:<f1:word1> +word6 super:<word3> word2");
+    Query q = parseSq("+leading <f1:word1> +word6 <word3> word2");
     Query q1 = bq(bc_m(sq(tq("super", "leading"))), bc(sq(tq("f1", "word1"))), bc_m(sq(tq("super",
"word6"))),
         bc(sq(tq("super", "word3"))), bc(sq(tq("super", "word2"))));
     assertQuery(q1, q);
@@ -272,7 +272,7 @@ public class SuperParserTest {
 
   @Test
   public void test25_AND_ORs() throws ParseException {
-    Query q = parseSq("leading AND super:<f1:word1> OR word6 super:<word3> word2");
+    Query q = parseSq("leading AND <f1:word1> OR word6 <word3> word2");
     Query q1 = bq(bc_m(sq(tq("super", "leading"))), bc_m(sq(tq("f1", "word1"))), bc(sq(tq("super",
"word6"))),
         bc(sq(tq("super", "word3"))), bc(sq(tq("super", "word2"))));
     assertQuery(q1, q);
@@ -280,7 +280,7 @@ public class SuperParserTest {
 
   @Test
   public void test26() throws ParseException {
-    Query q = parseSq("-leading super:<f1:word1> +word6 super:<word3> word2");
+    Query q = parseSq("-leading <f1:word1> +word6 <word3> word2");
     Query q1 = bq(bc_n(sq(tq("super", "leading"))), bc(sq(tq("f1", "word1"))), bc_m(sq(tq("super",
"word6"))),
         bc(sq(tq("super", "word3"))), bc(sq(tq("super", "word2"))));
     assertQuery(q1, q);


Mime
View raw message