cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1397180 - in /cxf/trunk/rt/rs/extensions/search: ./ src/main/java/org/apache/cxf/jaxrs/ext/search/ src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/ src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/ src/test/java/org/apache/cxf/jaxrs/...
Date Thu, 11 Oct 2012 17:20:16 GMT
Author: sergeyb
Date: Thu Oct 11 17:20:15 2012
New Revision: 1397180

URL: http://svn.apache.org/viewvc?rev=1397180&view=rev
Log:
Prototyping support for mapping FIQL to Lucene Query

Added:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
  (with props)
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/search/pom.xml
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionVisitor.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java

Modified: cxf/trunk/rt/rs/extensions/search/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/pom.xml?rev=1397180&r1=1397179&r2=1397180&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/pom.xml (original)
+++ cxf/trunk/rt/rs/extensions/search/pom.xml Thu Oct 11 17:20:15 2012
@@ -36,6 +36,7 @@
     <properties>
         <hibernate.em.version>4.1.0.Final</hibernate.em.version>
         <hsqldb.version>1.8.0.10</hsqldb.version>
+        <cxf.lucene.version>4.0.0-BETA</cxf.lucene.version>
         <cxf.osgi.import>
             org.apache.cxf.jaxrs.impl.tl
         </cxf.osgi.import>
@@ -58,6 +59,25 @@
             <version>${cxf.geronimo.jpa.version}</version>
             <scope>provided</scope>
             <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.lucene</groupId>
+            <artifactId>lucene-core</artifactId>
+            <version>${cxf.lucene.version}</version>
+            <scope>provided</scope>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.lucene</groupId>
+            <artifactId>lucene-analyzers-common</artifactId>
+            <version>${cxf.lucene.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.lucene</groupId>
+            <artifactId>lucene-queryparser</artifactId>
+            <version>${cxf.lucene.version}</version>
+            <scope>test</scope>
         </dependency>        
         <dependency>
             <groupId>org.easymock</groupId>
@@ -73,7 +93,7 @@
 		<dependency>
 			<groupId>hsqldb</groupId>
 			<artifactId>hsqldb</artifactId>
-			<version>1.8.0.10</version>
+			<version>${hsqldb.version}</version>
 			<scope>test</scope>
 		</dependency>
     </dependencies>

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionVisitor.java?rev=1397180&r1=1397179&r2=1397180&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionVisitor.java
Thu Oct 11 17:20:15 2012
@@ -29,13 +29,14 @@ public abstract class AbstractSearchCond
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractSearchConditionVisitor.class);
     
     private Map<String, String> fieldMap;
+    private Map<String, Class<?>> primitiveFieldTypeMap;
     
     protected AbstractSearchConditionVisitor(Map<String, String> fieldMap) {
         this.fieldMap = fieldMap;
     }
     
     protected String getRealPropertyName(String name) {
-        if (fieldMap != null) {
+        if (fieldMap != null && !fieldMap.isEmpty()) {
             if (fieldMap.containsKey(name)) {
                 return fieldMap.get(name);
             } else {
@@ -44,4 +45,19 @@ public abstract class AbstractSearchCond
         }
         return name;
     }
+
+    protected Class<?> getPrimitiveFieldClass(String name, Class<?> defaultCls)
{
+        Class<?> cls = null;
+        if (primitiveFieldTypeMap != null) {
+            cls = primitiveFieldTypeMap.get(name);
+        }
+        if (cls == null) {  
+            cls = defaultCls;
+        }
+        return cls;
+    }
+
+    public void setPrimitiveFieldTypeMap(Map<String, Class<?>> primitiveFieldTypeMap)
{
+        this.primitiveFieldTypeMap = primitiveFieldTypeMap;
+    }
 }

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java?rev=1397180&r1=1397179&r2=1397180&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
Thu Oct 11 17:20:15 2012
@@ -105,10 +105,10 @@ public class JPATypedQueryVisitor<T> ext
     @SuppressWarnings({ "unchecked", "rawtypes" })
     private Predicate buildPredicate(ConditionType ct, String name, Object value) {
 
-        Class<? extends Comparable> clazz = (Class<? extends Comparable>) value
-                        .getClass();
-        
         name = super.getRealPropertyName(name);
+        Class<? extends Comparable> clazz = (Class<? extends Comparable>)
+            getPrimitiveFieldClass(name, value.getClass());
+        
         
         Path<?> path = getPath(root, name);
         

Added: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java?rev=1397180&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
Thu Oct 11 17:20:15 2012
@@ -0,0 +1,219 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.lucene;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+import org.apache.cxf.jaxrs.ext.search.AbstractSearchConditionVisitor;
+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.lucene.document.DateTools;
+import org.apache.lucene.document.DateTools.Resolution;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.NumericRangeQuery;
+import org.apache.lucene.search.PhraseQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.WildcardQuery;
+
+public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T>
{
+
+    //private Analyzer analyzer;
+    private String contentsFieldName;
+    private Stack<List<Query>> queryStack = new Stack<List<Query>>();
+    public LuceneQueryVisitor() {
+        this(Collections.<String, String>emptyMap());
+    }
+    
+    public LuceneQueryVisitor(String contentsFieldAlias, String contentsFieldName) {
+        this(Collections.singletonMap(contentsFieldAlias, contentsFieldName));
+    }
+     
+    public LuceneQueryVisitor(String contentsFieldName) {
+        this(Collections.<String, String>emptyMap(), contentsFieldName);
+    }
+    
+    public LuceneQueryVisitor(Map<String, String> fieldsMap) {
+        this(fieldsMap, null);
+    }
+    
+    public LuceneQueryVisitor(Map<String, String> fieldsMap, String contentsFieldName)
{
+        super(fieldsMap);
+        this.contentsFieldName = contentsFieldName;
+        queryStack.push(new ArrayList<Query>());
+    }
+    
+    public void visit(SearchCondition<T> sc) {
+        PrimitiveStatement statement = sc.getStatement();
+        if (statement != null) {
+            if (statement.getProperty() != null) {
+                queryStack.peek().add(buildSimpleQuery(sc.getConditionType(), 
+                                         statement.getProperty(), 
+                                         statement.getValue()));
+            }
+        } else {
+            queryStack.push(new ArrayList<Query>());
+            for (SearchCondition<T> condition : sc.getSearchConditions()) {
+                condition.accept(this);
+            }
+            boolean orCondition = sc.getConditionType() == ConditionType.OR;
+            List<Query> queries = queryStack.pop();
+            queryStack.peek().add(createCompositeQuery(queries, orCondition));
+        }    
+    }
+
+    //public void setAnalyzer(Analyzer a) {
+    //    this.analyzer = a;
+    //}
+    
+    public Query getQuery() {
+        List<Query> queries = queryStack.peek();
+        return queries.isEmpty() ? null : queries.get(0);
+    }
+    
+    private Query buildSimpleQuery(ConditionType ct, String name, Object value) {
+        name = super.getRealPropertyName(name);
+        Class<?> clazz = getPrimitiveFieldClass(name, value.getClass());
+        
+        
+        Query query = null;
+        switch (ct) {
+        case EQUALS:
+            query = createEqualsQuery(clazz, name, value);
+            break;
+        case NOT_EQUALS:
+            BooleanQuery booleanQuery = new BooleanQuery();
+            booleanQuery.add(createEqualsQuery(clazz, name, value),
+                             BooleanClause.Occur.MUST_NOT);
+            query = booleanQuery;
+            break;
+        case GREATER_THAN:
+            query = createRangeQuery(clazz, name, value, ct);
+            break;
+        case GREATER_OR_EQUALS:
+            query = createRangeQuery(clazz, name, value, ct);
+            break;
+        case LESS_THAN:
+            query = createRangeQuery(clazz, name, value, ct);
+            break;
+        case LESS_OR_EQUALS:
+            query = createRangeQuery(clazz, name, value, ct);
+            break;
+        default: 
+            break;
+        }
+        return query;
+    }
+    
+    private Query createEqualsQuery(Class<?> cls, String name, Object value) {
+        Query query = null;
+        if (cls == String.class) {
+            String strValue = value.toString();
+            int wildCardIndex = strValue.indexOf('*'); 
+            
+            if (contentsFieldName == null) {
+                Term term = new Term(name, strValue);
+                
+                if (wildCardIndex == -1) {
+                    query = new TermQuery(term);
+                } else {
+                    query = new WildcardQuery(term);
+                } 
+            } else if (wildCardIndex == -1) {
+                PhraseQuery pquery = new PhraseQuery();
+                pquery.add(new Term(contentsFieldName, name));
+                pquery.add(new Term(contentsFieldName, strValue));
+                query = pquery;
+            } else {
+                BooleanQuery pquery = new BooleanQuery();
+                pquery.add(new TermQuery(new Term(contentsFieldName, name)),
+                           BooleanClause.Occur.MUST);
+                pquery.add(new WildcardQuery(new Term(contentsFieldName, strValue)),
+                           BooleanClause.Occur.MUST);
+                query = pquery;                
+            }
+        } else {
+            query = createRangeQuery(cls, name, value, ConditionType.EQUALS);
+        }
+        return query;
+    }
+    
+    private Query createRangeQuery(Class<?> cls, String name, Object value,
+                                   ConditionType type) {
+        if (String.class.isAssignableFrom(cls) || Number.class.isAssignableFrom(cls)) {
+            // If needed, long and double can be supported too
+            // Also, perhaps Strings may optionally be compared with string comparators 
+            Integer intValue = Integer.valueOf(value.toString());
+            Integer min = type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS
? null : intValue;
+            Integer max = type == ConditionType.GREATER_THAN || type == ConditionType.GREATER_OR_EQUALS

+                ? null : intValue;
+            boolean minInclusive = 
+                type == ConditionType.GREATER_OR_EQUALS || type == ConditionType.EQUALS;
+            boolean maxInclusive =
+                type == ConditionType.LESS_OR_EQUALS || type == ConditionType.EQUALS;
+            Query query = NumericRangeQuery.newIntRange(name, min, max, 
+                                                        minInclusive, maxInclusive);
+            return query;
+        } else if (Date.class.isAssignableFrom(cls)) {
+            // This code has not been tested - most likely needs to be fixed  
+            // Resolution should be configurable ?
+            String luceneDateValue = DateTools.dateToString((Date)value, Resolution.MILLISECOND);
+            String expression = null;
+            if (type == ConditionType.LESS_THAN) {
+                // what is the base date here ?
+                expression = "[" + ""
+                    + " TO " + luceneDateValue + "]";    
+            } else {
+                expression = "[" + luceneDateValue + " TO " 
+                    + DateTools.dateToString(new Date(), Resolution.MILLISECOND) + "]";
+            }
+            return parseExpression(name, expression);
+        } else {
+            return null;
+        }
+    }
+    
+    private Query createCompositeQuery(List<Query> queries, boolean orCondition) {
+        
+        BooleanClause.Occur clause = orCondition 
+            ? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST;
+        
+        BooleanQuery booleanQuery = new BooleanQuery();
+        
+        for (Query query : queries) {
+            booleanQuery.add(query, clause);
+        }
+        
+        return booleanQuery;
+    }
+    
+    protected Query parseExpression(String fieldName, String expression) {
+        //QueryParser parser = new QueryParser(Version.LUCENE_40, name, analyzer);
+        // return parse.parse(expression);
+        return null;
+    }
+}

Propchange: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java?rev=1397180&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java
Thu Oct 11 17:20:15 2012
@@ -0,0 +1,324 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.lucene;
+
+import java.util.Collections;
+
+import org.apache.cxf.jaxrs.ext.search.SearchBean;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.IntField;
+import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.Version;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LuceneQueryVisitorTest extends Assert {
+
+    private DirectoryReader ireader;
+    private IndexSearcher isearcher;
+    private Directory directory;
+    private Analyzer analyzer;
+    
+    @Before
+    public void setUp() throws Exception {
+        analyzer = new StandardAnalyzer(Version.LUCENE_40);
+        directory = new RAMDirectory();
+        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
+        IndexWriter iwriter = new IndexWriter(directory, config);
+        
+        Document doc = new Document();
+        doc.add(new Field("contents", "name=text", TextField.TYPE_STORED));
+        
+        IntField intField = new IntField("intfield", 4, Field.Store.YES);
+        doc.add(intField);
+        iwriter.addDocument(doc);
+        
+        iwriter.close();
+        ireader = DirectoryReader.open(directory);
+        isearcher = new IndexSearcher(ireader);
+    }
+    
+    @After
+    public void tearDown() throws Exception {
+        ireader.close();
+        directory.close();
+    }
+    
+    @Test
+    public void testTextContentMatchEqual() throws Exception {
+        
+        doTestTextContentMatch("ct==text");
+    }
+    
+    @Test
+    public void testTextContentMatchNotEqual() throws Exception {
+        
+        Query query = createTermQuery("contents", "ct!=text");
+        doTestNoMatch(query);
+            
+    }
+    
+    @Test
+    public void testTextContentMatchWildcardEnd() throws Exception {
+        doTestTextContentMatch("ct==tex*");
+    }
+    
+    @Test
+    public void testTextContentMatchWildcardStart() throws Exception {
+        doTestTextContentMatch("ct==*ext");
+    }
+    
+    @Test
+    public void testIntContentMatchGreater() throws Exception {
+        doTestIntContentMatch("ct=gt=3");
+    }
+    
+    @Test
+    public void testIntContentMatchGreaterWithClassFiled() throws Exception {
+        Query query = createTermQueryWithFieldClass("intfield", "ct=gt=3", Integer.class);
+        doTestIntContentMatchWithQuery(query);
+    }
+    
+    @Test
+    public void testIntContentMatchGreaterNoMatch() throws Exception {
+        Query query = createTermQuery("intfield", "ct=gt=5");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testIntContentMatchGreaterOrEqual() throws Exception {
+        doTestIntContentMatch("ct=ge=4");
+        doTestIntContentMatch("ct=ge=3");
+    }
+    
+    @Test
+    public void testIntContentMatchGreaterOrEqualNoMatch() throws Exception {
+        Query query = createTermQuery("intfield", "ct=ge=5");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testIntContentMatchLess() throws Exception {
+        doTestIntContentMatch("ct=lt=5");
+    }
+    
+    @Test
+    public void testIntContentMatchLessNoMatch() throws Exception {
+        Query query = createTermQuery("intfield", "ct=lt=3");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testIntContentMatchLessOrEqual() throws Exception {
+        doTestIntContentMatch("ct=le=4");
+        doTestIntContentMatch("ct=le=5");
+    }
+    
+    @Test
+    public void testIntContentMatchLessOrEqualNoMatch() throws Exception {
+        Query query = createTermQuery("intfield", "ct=le=3");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testIntContentMatchEquals() throws Exception {
+        Query query = createTermQueryWithFieldClass("intfield", "ct==4", Integer.class);
+        doTestIntContentMatchWithQuery(query);
+    }
+    
+    @Test
+    public void testTextAndContentMatch() throws Exception {
+        Query query = createTermQuery("contents==name;contents==text");
+        doTestTextContentMatchWithQuery(query);
+        
+    }
+    
+    @Test
+    public void testTextAndContentNoMatch() throws Exception {
+        Query query = createTermQuery("contents==bar;contents==text");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testTextOrContentMatch() throws Exception {
+        Query query = createTermQuery("contents==bar,contents==text");
+        doTestTextContentMatchWithQuery(query);
+        
+    }
+    
+    @Test
+    public void testTextOrContentNoMatch() throws Exception {
+        Query query = createTermQuery("contents==bar,contents==foo");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testIntAndTextContentMatch() throws Exception {
+        
+        Query query = createTermQueryWithFieldClass("intfield==4;contents==text", Integer.class);
+        doTestIntContentMatchWithQuery(query);
+        doTestTextContentMatchWithQuery(query);
+        
+    }
+    
+    @Test
+    public void testIntAndTextContentNoMatch() throws Exception {
+        Query query = createTermQuery("intfield==3;contents==text");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testIntOrTextContentMatch() throws Exception {
+        Query query = createTermQuery("intfield==3,contents==text");
+        doTestTextContentMatchWithQuery(query);
+        doTestIntContentMatchWithQuery(query);
+        
+    }
+    
+    @Test
+    public void testIntOrTextContentNoMatch() throws Exception {
+        Query query = createTermQuery("intfield==3,contents==bar");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testTextContentMatchEqualPhrase() throws Exception {
+        Query query = createPhraseQuery("contents", "name==text");
+        doTestTextContentMatchWithQuery(query);
+    }
+    
+    @Test
+    public void testTextContentMatchNotEqualPhrase() throws Exception {
+        
+        Query query = createPhraseQuery("contents", "name!=text");
+        doTestNoMatch(query);
+    }
+    
+    @Test
+    public void testTextContentMatchEqualPhraseWildcard() throws Exception {
+        Query query = createPhraseQuery("contents", "name==tex*");
+        doTestTextContentMatchWithQuery(query);
+    }
+    
+    private void doTestTextContentMatch(String expression) throws Exception {
+        
+        Query query = createTermQuery("contents", expression);
+        doTestTextContentMatchWithQuery(query);
+            
+    }
+    
+    private void doTestNoMatch(Query query) throws Exception {
+        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
+        assertEquals(0, hits.length);
+    }
+    
+    private void doTestTextContentMatchWithQuery(Query query) throws Exception {
+        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
+        assertEquals(1, hits.length);
+        // Iterate through the results:
+        for (int i = 0; i < hits.length; i++) {
+            Document hitDoc = isearcher.doc(hits[i].doc);
+            assertEquals("name=text", hitDoc.get("contents"));
+        }
+            
+    }
+    
+    private void doTestIntContentMatch(String expression) throws Exception {
+        
+        Query query = createTermQuery("intfield", expression);
+        doTestIntContentMatchWithQuery(query);
+            
+    }
+    
+    private void doTestIntContentMatchWithQuery(Query query) throws Exception {
+        
+        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
+        assertEquals(1, hits.length);
+        // Iterate through the results:
+        for (int i = 0; i < hits.length; i++) {
+            Document hitDoc = isearcher.doc(hits[i].doc);
+            IndexableField field = hitDoc.getField("intfield");
+            assertEquals(4, field.numericValue().intValue());
+        }
+            
+    }
+    
+    private Query createTermQuery(String expression) throws Exception {
+        SearchCondition<SearchBean> filter = 
+            new FiqlParser<SearchBean>(SearchBean.class).parse(expression);
+        LuceneQueryVisitor<SearchBean> lucene = new LuceneQueryVisitor<SearchBean>();
+        lucene.visit(filter);
+        return lucene.getQuery();
+    }
+    
+    private Query createTermQueryWithFieldClass(String expression, Class<?> cls) throws
Exception {
+        SearchCondition<SearchBean> filter = 
+            new FiqlParser<SearchBean>(SearchBean.class).parse(expression);
+        LuceneQueryVisitor<SearchBean> lucene = new LuceneQueryVisitor<SearchBean>();
+        lucene.setPrimitiveFieldTypeMap(Collections.<String, Class<?>>singletonMap("intfield",
cls));
+        lucene.visit(filter);
+        return lucene.getQuery();
+    }
+    
+    private Query createTermQuery(String fieldName, String expression) throws Exception {
+        SearchCondition<SearchBean> filter = 
+            new FiqlParser<SearchBean>(SearchBean.class).parse(expression);
+        LuceneQueryVisitor<SearchBean> lucene = 
+            new LuceneQueryVisitor<SearchBean>("ct", fieldName);
+        lucene.visit(filter);
+        return lucene.getQuery();
+    }
+    
+    private Query createTermQueryWithFieldClass(String fieldName, String expression, Class<?>
cls) 
+        throws Exception {
+        SearchCondition<SearchBean> filter = 
+            new FiqlParser<SearchBean>(SearchBean.class).parse(expression);
+        LuceneQueryVisitor<SearchBean> lucene = 
+            new LuceneQueryVisitor<SearchBean>("ct", fieldName);
+        lucene.setPrimitiveFieldTypeMap(Collections.<String, Class<?>>singletonMap(fieldName,
cls));
+        lucene.visit(filter);
+        return lucene.getQuery();
+    }
+    
+    private Query createPhraseQuery(String fieldName, String expression) throws Exception
{
+        SearchCondition<SearchBean> filter = 
+            new FiqlParser<SearchBean>(SearchBean.class).parse(expression);
+        LuceneQueryVisitor<SearchBean> lucene = 
+            new LuceneQueryVisitor<SearchBean>(fieldName);
+        lucene.visit(filter);
+        return lucene.getQuery();
+    }
+}

Propchange: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message