incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject git commit: Highlighting should now work for field less queries.
Date Wed, 19 Jun 2013 19:33:40 GMT
Updated Branches:
  refs/heads/master bf85c7283 -> 7c6dc48ba


Highlighting should now work for field less queries.


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

Branch: refs/heads/master
Commit: 7c6dc48ba5227672fab0d29c1ba424e9850f857f
Parents: bf85c72
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Wed Jun 19 15:33:13 2013 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Wed Jun 19 15:33:13 2013 -0400

----------------------------------------------------------------------
 .../org/apache/blur/utils/HighlightHelper.java  | 93 ++++++++++++++++++--
 .../apache/blur/manager/IndexManagerTest.java   | 36 +++++++-
 2 files changed, 121 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7c6dc48b/blur-core/src/main/java/org/apache/blur/utils/HighlightHelper.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/utils/HighlightHelper.java b/blur-core/src/main/java/org/apache/blur/utils/HighlightHelper.java
index ace086e..d63b61c 100644
--- a/blur-core/src/main/java/org/apache/blur/utils/HighlightHelper.java
+++ b/blur-core/src/main/java/org/apache/blur/utils/HighlightHelper.java
@@ -37,16 +37,22 @@ import org.apache.lucene.index.Term;
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MultiPhraseQuery;
 import org.apache.lucene.search.NumericRangeQuery;
+import org.apache.lucene.search.PhraseQuery;
+import org.apache.lucene.search.PrefixQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.TermRangeQuery;
 import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.search.WildcardQuery;
 import org.apache.lucene.search.highlight.Highlighter;
 import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
 import org.apache.lucene.search.highlight.QueryScorer;
 import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
 import org.apache.lucene.search.highlight.TextFragment;
 import org.apache.lucene.search.highlight.TokenSources;
+import org.apache.lucene.util.BytesRef;
 
 public class HighlightHelper {
 
@@ -114,8 +120,6 @@ public class HighlightHelper {
   public static Document highlight(int docId, Document document, Query query, BlurAnalyzer
analyzer,
       IndexReader reader, String preTag, String postTag) throws IOException, InvalidTokenOffsetsException
{
 
-    Query fixedQuery = fixSuperQuery(query);
-
     SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(preTag, postTag);
     Document result = new Document();
     for (IndexableField f : document) {
@@ -126,6 +130,9 @@ public class HighlightHelper {
       }
       String text = f.stringValue();
       Number numericValue = f.numericValue();
+
+      Query fixedQuery = fixSuperQuery(query, name);
+
       if (numericValue != null) {
         if (shouldNumberBeHighlighted(name, numericValue, fixedQuery)) {
           String numberHighlight = preTag + text + postTag;
@@ -145,21 +152,93 @@ public class HighlightHelper {
     return result;
   }
 
-  private static Query fixSuperQuery(Query query) {
+  private static Query fixSuperQuery(Query query, String name) {
     if (query instanceof BooleanQuery) {
       BooleanQuery bq = (BooleanQuery) query;
+      BooleanQuery newBq = new BooleanQuery();
       for (BooleanClause booleanClause : bq) {
-        booleanClause.setQuery(fixSuperQuery(booleanClause.getQuery()));
+        newBq.add(fixSuperQuery(booleanClause.getQuery(), name), booleanClause.getOccur());
       }
-      return bq;
+      return newBq;
     } else if (query instanceof SuperQuery) {
       SuperQuery sq = (SuperQuery) query;
-      return sq.getQuery();
+      return setFieldIfNeeded(sq.getQuery(), name);
     } else {
-      return query;
+      return setFieldIfNeeded(query, name);
     }
   }
 
+  private static Query setFieldIfNeeded(Query query, String name) {
+    if (query instanceof TermQuery) {
+      TermQuery tq = (TermQuery) query;
+      Term term = tq.getTerm();
+      if (term.field().equals(BlurConstants.SUPER)) {
+        return new TermQuery(new Term(name, term.bytes()));
+      }
+    } else if (query instanceof WildcardQuery) {
+      WildcardQuery wq = (WildcardQuery) query;
+      Term term = wq.getTerm();
+      if (term.field().equals(BlurConstants.SUPER)) {
+        return new WildcardQuery(new Term(name, term.bytes()));
+      }
+    } else if (query instanceof MultiPhraseQuery) {
+      MultiPhraseQuery mpq = (MultiPhraseQuery) query;
+      int[] positions = mpq.getPositions();
+      List<Term[]> termArrays = mpq.getTermArrays();
+      if (isTermField(termArrays, BlurConstants.SUPER)) {
+        MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery();
+        multiPhraseQuery.setSlop(mpq.getSlop());
+        for (int i = 0; i < termArrays.size(); i++) {
+          multiPhraseQuery.add(changeFields(termArrays.get(i), name), positions[i]);
+        }
+        return multiPhraseQuery;
+      }
+    } else if (query instanceof PhraseQuery) {
+      PhraseQuery pq = (PhraseQuery) query;
+      Term[] terms = pq.getTerms();
+      int[] positions = pq.getPositions();
+      String field = terms[0].field();
+      if (field.equals(BlurConstants.SUPER)) {
+        PhraseQuery phraseQuery = new PhraseQuery();
+        for (int i = 0; i < terms.length; i++) {
+          phraseQuery.add(new Term(name, terms[i].bytes()), positions[i]);
+        }
+        phraseQuery.setSlop(pq.getSlop());
+        return phraseQuery;
+      }
+    } else if (query instanceof PrefixQuery) {
+      PrefixQuery pq = (PrefixQuery) query;
+      Term term = pq.getPrefix();
+      if (term.field().equals(BlurConstants.SUPER)) {
+        return new PrefixQuery(new Term(name, term.bytes()));
+      }
+    } else if (query instanceof TermRangeQuery) {
+      TermRangeQuery trq = (TermRangeQuery) query;
+      BytesRef lowerTerm = trq.getLowerTerm();
+      BytesRef upperTerm = trq.getUpperTerm();
+      boolean includeUpper = trq.includesUpper();
+      boolean includeLower = trq.includesLower();
+      String field = trq.getField();
+      if (field.equals(BlurConstants.SUPER)) {
+        return new TermRangeQuery(name, lowerTerm, upperTerm, includeLower, includeUpper);
+      }
+    }
+    return query;
+  }
+
+  private static Term[] changeFields(Term[] terms, String name) {
+    Term[] newTerms = new Term[terms.length];
+    for (int i = 0; i < terms.length; i++) {
+      newTerms[i] = new Term(name, terms[i].bytes());
+    }
+    return newTerms;
+  }
+
+  private static boolean isTermField(List<Term[]> termArrays, String fieldName) {
+    Term[] terms = termArrays.get(0);
+    return terms[0].field().equals(fieldName);
+  }
+
   public static boolean shouldNumberBeHighlighted(String name, Number numericValue, Query
query) {
     if (query instanceof BooleanQuery) {
       BooleanQuery booleanQuery = (BooleanQuery) query;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7c6dc48b/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 a4aa7be..837d0a2 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
@@ -278,7 +278,41 @@ public class IndexManagerTest {
     Selector selector = new Selector().setRowId("row-6");
     HighlightOptions highlightOptions = new HighlightOptions();
     SimpleQuery simpleQuery = new SimpleQuery();
-    simpleQuery.setQueryStr(FAMILY + ".testcol12:value101");
+    simpleQuery.setQueryStr(FAMILY2 + ".testcol13:value105 " + FAMILY + ".testcol12:value101");
+    highlightOptions.setSimpleQuery(simpleQuery);
+    selector.setHighlightOptions(highlightOptions);
+    FetchResult fetchResult = new FetchResult();
+    indexManager.fetchRow(TABLE, selector, fetchResult);
+
+    assertNotNull(fetchResult.rowResult.row);
+    Row row = newRow("row-6", newRecord(FAMILY, "record-6B", newColumn("testcol12", "<<<value101>>>")));
+    row.recordCount = 3;
+    assertEquals(row, fetchResult.rowResult.row);
+  }
+  
+  @Test
+  public void testFetchRowByRowIdHighlightingWithFullText() throws Exception {
+    Selector selector = new Selector().setRowId("row-6");
+    HighlightOptions highlightOptions = new HighlightOptions();
+    SimpleQuery simpleQuery = new SimpleQuery();
+    simpleQuery.setQueryStr("cool value101");
+    highlightOptions.setSimpleQuery(simpleQuery);
+    selector.setHighlightOptions(highlightOptions);
+    FetchResult fetchResult = new FetchResult();
+    indexManager.fetchRow(TABLE, selector, fetchResult);
+
+    assertNotNull(fetchResult.rowResult.row);
+    Row row = newRow("row-6", newRecord(FAMILY, "record-6B", newColumn("testcol12", "<<<value101>>>")));
+    row.recordCount = 3;
+    assertEquals(row, fetchResult.rowResult.row);
+  }
+  
+  @Test
+  public void testFetchRowByRowIdHighlightingWithFullTextWildCard() throws Exception {
+    Selector selector = new Selector().setRowId("row-6");
+    HighlightOptions highlightOptions = new HighlightOptions();
+    SimpleQuery simpleQuery = new SimpleQuery();
+    simpleQuery.setQueryStr("cool ?alue101");
     highlightOptions.setSimpleQuery(simpleQuery);
     selector.setHighlightOptions(highlightOptions);
     FetchResult fetchResult = new FetchResult();


Mime
View raw message