cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [1/2] git commit: CXF-5963: LuceneQueryVisitor should use analyzer to create query properly. Refactored LuceneQueryVisitor to use QueryBuilder and analyzer.
Date Tue, 26 Aug 2014 15:12:30 GMT
Repository: cxf
Updated Branches:
  refs/heads/master be6b95a69 -> 15f872670


CXF-5963: LuceneQueryVisitor should use analyzer to create query properly. Refactored LuceneQueryVisitor
to use QueryBuilder and analyzer.


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

Branch: refs/heads/master
Commit: fd3471e58e63ee672babc015ed0929f6cd6862dd
Parents: fad3938
Author: reta <drreta@gmail.com>
Authored: Tue Aug 26 11:11:47 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Tue Aug 26 11:11:47 2014 -0400

----------------------------------------------------------------------
 .../main/release/samples/jax_rs/search/pom.xml  |  1 +
 .../java/demo/jaxrs/search/client/Client.java   |  2 +
 .../java/demo/jaxrs/search/server/Catalog.java  |  5 +-
 .../ext/search/lucene/LuceneQueryVisitor.java   | 58 ++++++++++++++------
 4 files changed, 46 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/fd3471e5/distribution/src/main/release/samples/jax_rs/search/pom.xml
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/pom.xml b/distribution/src/main/release/samples/jax_rs/search/pom.xml
index e69b720..9a9a1fa 100644
--- a/distribution/src/main/release/samples/jax_rs/search/pom.xml
+++ b/distribution/src/main/release/samples/jax_rs/search/pom.xml
@@ -30,6 +30,7 @@
     </parent>
     <properties>
         <cxf.version>${project.version}</cxf.version>
+        <cxf.lucene.version>4.9.0</cxf.lucene.version>
         <httpclient.version>3.1</httpclient.version>
     </properties>
     <profiles>

http://git-wip-us.apache.org/repos/asf/cxf/blob/fd3471e5/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
index def695c..8eee763 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
@@ -46,7 +46,9 @@ public final class Client {
         list(url, httpClient);        
         
         search(url, httpClient, "ct==java");        
+        search(url, httpClient, "ct==Java");
         search(url, httpClient, "ct==websockets");
+        search(url, httpClient, "ct==WebSockets");
         
         delete(url, httpClient);
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/fd3471e5/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
index f66ecee..ea6e3c7 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
@@ -254,11 +254,12 @@ public class Catalog {
         return new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_4_9, analyzer));
     }
     
-    private static LuceneQueryVisitor< SearchBean > createVisitor() {
+    private LuceneQueryVisitor< SearchBean > createVisitor() {
         final Map< String, Class< ? > > fieldTypes = new HashMap< String,
Class< ? > >();
         fieldTypes.put("modified", Date.class);
         
-        LuceneQueryVisitor<SearchBean> visitor = new LuceneQueryVisitor<SearchBean>("ct",
"contents");
+        LuceneQueryVisitor<SearchBean> visitor = new LuceneQueryVisitor<SearchBean>(
+            "ct", "contents", analyzer);
         visitor.setPrimitiveFieldTypeMap(fieldTypes);
         return visitor;
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/fd3471e5/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
index 3383730..f9bf620 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
@@ -29,6 +29,7 @@ import org.apache.cxf.jaxrs.ext.search.ConditionType;
 import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.visitor.AbstractSearchConditionVisitor;
+import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.document.DateTools;
 import org.apache.lucene.document.DateTools.Resolution;
 import org.apache.lucene.index.Term;
@@ -40,6 +41,7 @@ import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.search.TermRangeQuery;
 import org.apache.lucene.search.WildcardQuery;
+import org.apache.lucene.util.QueryBuilder;
 
 import static org.apache.cxf.jaxrs.ext.search.ParamConverterUtils.getString;
 import static org.apache.cxf.jaxrs.ext.search.ParamConverterUtils.getValue;
@@ -50,6 +52,8 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
     private Map<String, String> contentsFieldMap;
     private boolean caseInsensitiveMatch;
     private Stack<List<Query>> queryStack = new Stack<List<Query>>();
+    private QueryBuilder queryBuilder;
+    
     public LuceneQueryVisitor() {
         this(Collections.<String, String>emptyMap());        
     }
@@ -57,7 +61,7 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
     public LuceneQueryVisitor(String contentsFieldAlias, String contentsFieldName) {
         this(Collections.singletonMap(contentsFieldAlias, contentsFieldName));
     }
-     
+         
     public LuceneQueryVisitor(String contentsFieldName) {
         this(Collections.<String, String>emptyMap(), contentsFieldName);
     }
@@ -67,8 +71,21 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
     }
     
     public LuceneQueryVisitor(Map<String, String> fieldsMap, String contentsFieldName)
{
+        this(fieldsMap, contentsFieldName, null);
+    }
+    
+    public LuceneQueryVisitor(String contentsFieldAlias, String contentsFieldName, Analyzer
analyzer) {
+        this(Collections.singletonMap(contentsFieldAlias, contentsFieldName), null, analyzer);
+    }
+    
+    public LuceneQueryVisitor(Map<String, String> fieldsMap, String contentsFieldName,
Analyzer analyzer) {
         super(fieldsMap);
         this.contentsFieldName = contentsFieldName;
+        
+        if (analyzer != null) {
+            queryBuilder = new QueryBuilder(analyzer);
+        }
+        
         queryStack.push(new ArrayList<Query>());
     }
     
@@ -100,6 +117,10 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
         return queries.isEmpty() ? null : queries.get(0);
     }
     
+    public void setCaseInsensitiveMatch(boolean caseInsensitiveMatch) {
+        this.caseInsensitiveMatch = caseInsensitiveMatch;
+    }   
+    
     private Query buildSimpleQuery(ConditionType ct, String name, Object value) {
         name = super.getRealPropertyName(name);
         validatePropertyValue(name, value);
@@ -147,25 +168,15 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
             
             String theContentsFieldName = getContentsFieldName(name);
             if (theContentsFieldName == null) {
-                Term term = new Term(name, strValue);
-                
                 if (!isWildCard) {
-                    query = new TermQuery(term);
+                    query = newTermQuery(name, strValue);
                 } else {
-                    query = new WildcardQuery(term);
+                    query = new WildcardQuery(new Term(name, strValue));
                 } 
             } else if (!isWildCard) {
-                PhraseQuery pquery = new PhraseQuery();
-                pquery.add(new Term(theContentsFieldName, name));
-                pquery.add(new Term(theContentsFieldName, strValue));
-                query = pquery;
+                query = newPhraseQuery(theContentsFieldName, strValue);
             } else {
-                BooleanQuery pquery = new BooleanQuery();
-                pquery.add(new TermQuery(new Term(theContentsFieldName, name)),
-                           BooleanClause.Occur.MUST);
-                pquery.add(new WildcardQuery(new Term(theContentsFieldName, strValue)),
-                           BooleanClause.Occur.MUST);
-                query = pquery;                
+                query = new WildcardQuery(new Term(theContentsFieldName, strValue));
             }
         } else {
             query = createRangeQuery(cls, name, value, ConditionType.EQUALS);
@@ -273,7 +284,18 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
         return booleanQuery;
     }
 
-    public void setCaseInsensitiveMatch(boolean caseInsensitiveMatch) {
-        this.caseInsensitiveMatch = caseInsensitiveMatch;
-    }    
+    private Query newTermQuery(final String field, final String query) {
+        return (queryBuilder != null) ? queryBuilder.createBooleanQuery(field, query) 
+            : new TermQuery(new Term(field, query));
+    }
+    
+    private Query newPhraseQuery(final String field, final String query) {
+        if (queryBuilder != null) {
+            return queryBuilder.createPhraseQuery(field, query);
+        }
+        
+        final PhraseQuery phraseQuery = new PhraseQuery();
+        phraseQuery.add(new Term(field, query));
+        return phraseQuery;
+    }
 }


Mime
View raw message