JENA-1305: refactor TextIndex to take a separate graphURI parameter

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/52f60589
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/52f60589
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/52f60589

Branch: refs/heads/master
Commit: 52f60589c59498cb7a7f52402f0515140952ab40
Parents: 4bb8a39
Author: Osma Suominen <[email protected]>
Authored: Mon Mar 13 17:55:34 2017 +0200
Committer: Osma Suominen <[email protected]>
Committed: Mon Mar 13 17:55:34 2017 +0200

----------------------------------------------------------------------
 .../apache/jena/query/text/DatasetGraphText.java    |  7 ++++++-
 .../java/org/apache/jena/query/text/TextIndex.java  |  4 ++--
 .../org/apache/jena/query/text/TextIndexLucene.java | 16 +++++++++++-----
 .../org/apache/jena/query/text/TextQueryPF.java     | 10 ++++------
 4 files changed, 23 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/52f60589/jena-text/src/main/java/org/apache/jena/query/text/DatasetGraphText.java
----------------------------------------------------------------------
diff --git 
a/jena-text/src/main/java/org/apache/jena/query/text/DatasetGraphText.java 
b/jena-text/src/main/java/org/apache/jena/query/text/DatasetGraphText.java
index d0664b8..9f70fca 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/DatasetGraphText.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/DatasetGraphText.java
@@ -93,12 +93,17 @@ public class DatasetGraphText extends DatasetGraphMonitor 
implements Transaction
 
     /** Search the text index on the text field associated with the predicate 
*/
     public Iterator<TextHit> search(String queryString, Node predicate, int 
limit) {
+        return search(queryString, predicate, null, limit) ;
+    }
+
+    /** Search the text index on the text field associated with the predicate 
within graph */
+    public Iterator<TextHit> search(String queryString, Node predicate, String 
graphURI, int limit) {
         queryString = QueryParserBase.escape(queryString) ;
         if ( predicate != null ) {
             String f = textIndex.getDocDef().getField(predicate) ;
             queryString = f + ":" + queryString ;
         }
-        List<TextHit> results = textIndex.query(predicate, queryString, limit) 
;
+        List<TextHit> results = textIndex.query(predicate, queryString, 
graphURI, limit) ;
         return results.iterator() ;
     }
 

http://git-wip-us.apache.org/repos/asf/jena/blob/52f60589/jena-text/src/main/java/org/apache/jena/query/text/TextIndex.java
----------------------------------------------------------------------
diff --git a/jena-text/src/main/java/org/apache/jena/query/text/TextIndex.java 
b/jena-text/src/main/java/org/apache/jena/query/text/TextIndex.java
index 999eb46..31416b2 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/TextIndex.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/TextIndex.java
@@ -46,9 +46,9 @@ public interface TextIndex extends Closeable //, Transactional
     /** Access the index - limit if -1 for as many as possible 
      * Throw QueryParseException for syntax errors in the query string.
      */ 
-    List<TextHit> query(Node property, String qs, int limit) ;
+    List<TextHit> query(Node property, String qs, String graphURI, int limit) ;
     
-    List<TextHit> query(Node property, String qs) ;
+    List<TextHit> query(Node property, String qs, String graphURI) ;
 
     EntityDefinition getDocDef() ;
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/52f60589/jena-text/src/main/java/org/apache/jena/query/text/TextIndexLucene.java
----------------------------------------------------------------------
diff --git 
a/jena-text/src/main/java/org/apache/jena/query/text/TextIndexLucene.java 
b/jena-text/src/main/java/org/apache/jena/query/text/TextIndexLucene.java
index c3c7437..c26a88c 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/TextIndexLucene.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/TextIndexLucene.java
@@ -370,14 +370,14 @@ public class TextIndexLucene implements TextIndex {
     }
 
     @Override
-    public List<TextHit> query(Node property, String qs) {
-        return query(property, qs, MAX_N) ;
+    public List<TextHit> query(Node property, String qs, String graphURI) {
+        return query(property, qs, graphURI, MAX_N) ;
     }
 
     @Override
-    public List<TextHit> query(Node property, String qs, int limit) {
+    public List<TextHit> query(Node property, String qs, String graphURI, int 
limit) {
         try (IndexReader indexReader = DirectoryReader.open(directory)) {
-            return query$(indexReader, property, qs, limit) ;
+            return query$(indexReader, property, qs, graphURI, limit) ;
         }
         catch (ParseException ex) {
             throw new TextIndexParseException(qs, ex.getMessage()) ;
@@ -387,7 +387,13 @@ public class TextIndexLucene implements TextIndex {
         }
     }
 
-    private List<TextHit> query$(IndexReader indexReader, Node property, 
String qs, int limit) throws ParseException, IOException {
+    private List<TextHit> query$(IndexReader indexReader, Node property, 
String qs, String graphURI, int limit) throws ParseException, IOException {
+        if (graphURI != null) {
+            String escaped = QueryParserBase.escape(graphURI) ;
+            String qs2 = getDocDef().getGraphField() + ":" + escaped ;
+            qs = "(" + qs + ") AND " + qs2 ;
+        }
+
         IndexSearcher indexSearcher = new IndexSearcher(indexReader) ;
         Query query = parseQuery(qs, queryAnalyzer) ;
         if ( limit <= 0 )

http://git-wip-us.apache.org/repos/asf/jena/blob/52f60589/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
----------------------------------------------------------------------
diff --git 
a/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java 
b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
index 9c0120f..853c06f 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
@@ -48,7 +48,6 @@ import org.apache.jena.sparql.pfunction.PropertyFunctionBase ;
 import org.apache.jena.sparql.util.IterLib ;
 import org.apache.jena.sparql.util.NodeFactoryExtra ;
 import org.apache.jena.sparql.util.Symbol ;
-import org.apache.lucene.queryparser.classic.QueryParserBase ;
 import org.slf4j.Logger ;
 import org.slf4j.LoggerFactory ;
 
@@ -239,17 +238,15 @@ public class TextQueryPF extends PropertyFunctionBase {
 
     private ListMultimap<String,TextHit> query(Node property, String 
queryString, int limit, ExecutionContext execCxt) {
         // use the graph information in the text index if possible
+        String graph = null;
         if (textIndex.getDocDef().getGraphField() != null
             && execCxt.getActiveGraph() instanceof GraphView) {
             GraphView activeGraph = (GraphView)execCxt.getActiveGraph() ;
             if (!Quad.isUnionGraph(activeGraph.getGraphName())) {
-                String uri = 
+                graph =
                     activeGraph.getGraphName() != null 
                     ? 
TextQueryFuncs.graphNodeToString(activeGraph.getGraphName())
                     : Quad.defaultGraphNodeGenerated.getURI() ;
-                String escaped = QueryParserBase.escape(uri) ;
-                String qs2 = textIndex.getDocDef().getGraphField() + ":" + 
escaped ;
-                queryString = "(" + queryString + ") AND " + qs2 ;
             }
         }
 
@@ -277,8 +274,9 @@ public class TextQueryPF extends PropertyFunctionBase {
         }
 
         final String queryStr = queryString; // final needed for the lambda 
function
+        final String graphURI = graph; // final needed for the lambda function
         ListMultimap<String,TextHit> results = queryCache.getOrFill(cacheKey, 
() -> {
-            List<TextHit> resultList = textIndex.query(property, queryStr, 
limit) ;
+            List<TextHit> resultList = textIndex.query(property, queryStr, 
graphURI, limit) ;
             ListMultimap<String,TextHit> resultMultimap = 
LinkedListMultimap.create();
             for (TextHit result : resultList) {
                 
resultMultimap.put(TextQueryFuncs.subjectToString(result.getNode()), result);

Reply via email to