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-5549: Introduce Tika Search Visitor. Added more test cases and numeric types handling
Date Wed, 02 Jul 2014 00:51:05 GMT
Repository: cxf
Updated Branches:
  refs/heads/master d21734623 -> e0036b70c


CXF-5549: Introduce Tika Search Visitor. Added more test cases and numeric types handling


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

Branch: refs/heads/master
Commit: 84cbdf6ac5b7b242c77ea70937aae7ab1cb5e306
Parents: 7fab676
Author: reta <drreta@gmail.com>
Authored: Tue Jul 1 20:44:54 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Tue Jul 1 20:44:54 2014 -0400

----------------------------------------------------------------------
 .../ext/search/lucene/LuceneQueryVisitor.java   | 59 ++++++++++--
 .../search/tika/TikaLuceneContentExtractor.java |  2 +-
 .../tika/TikaLuceneContentExtractorTest.java    | 95 ++++++++++++++++++++
 3 files changed, 149 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/84cbdf6a/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 e485380..6bee256 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
@@ -191,12 +191,18 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
         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;
-            Query query = NumericRangeQuery.newIntRange(name, min, max, 
-                                                        minInclusive, maxInclusive);
+            Query query = null;
+            
+            if (Double.class.isAssignableFrom(cls)) {
+                query = createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
+            } else if (Float.class.isAssignableFrom(cls)) {
+                query = createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
+            } else if (Long.class.isAssignableFrom(cls)) {
+                query = createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
+            } else {
+                query = createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
+            }
+        
             return query;
         } else if (Date.class.isAssignableFrom(cls)) {
             // This code has not been tested - most likely needs to be fixed  
@@ -215,6 +221,47 @@ public class LuceneQueryVisitor<T> extends AbstractSearchConditionVisitor<T,
Que
             return null;
         }
     }
+
+    private Query createIntRangeQuery(final String name, final Object value,
+            final ConditionType type, final boolean minInclusive, final boolean maxInclusive)
{        
+        final Integer intValue = Integer.valueOf(value.toString());        
+        
+        return NumericRangeQuery.newIntRange(name, getMin(type, intValue), 
+            getMax(type, intValue),  minInclusive, maxInclusive);        
+    }
+    
+    private Query createLongRangeQuery(final String name, final Object value,
+            final ConditionType type, final boolean minInclusive, final boolean maxInclusive)
{        
+        final Long longValue = Long.valueOf(value.toString());        
+        
+        return NumericRangeQuery.newLongRange(name, getMin(type, longValue), 
+            getMax(type, longValue),  minInclusive, maxInclusive);        
+    }
+    
+    private Query createDoubleRangeQuery(final String name, final Object value,
+            final ConditionType type, final boolean minInclusive, final boolean maxInclusive)
{        
+        final Double doubleValue = Double.valueOf(value.toString());        
+        
+        return NumericRangeQuery.newDoubleRange(name, getMin(type, doubleValue), 
+            getMax(type, doubleValue),  minInclusive, maxInclusive);        
+    }
+    
+    private Query createFloatRangeQuery(final String name, final Object value,
+            final ConditionType type, final boolean minInclusive, final boolean maxInclusive)
{        
+        final Float floatValue = Float.valueOf(value.toString());        
+        
+        return NumericRangeQuery.newFloatRange(name, getMin(type, floatValue), 
+            getMax(type, floatValue),  minInclusive, maxInclusive);        
+    }
+
+    private< N > N getMax(final ConditionType type, final N value) {
+        return type == ConditionType.GREATER_THAN || type == ConditionType.GREATER_OR_EQUALS

+            ? null : value;
+    }
+
+    private< N > N getMin(final ConditionType type, final N value) {
+        return type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS ?
null : value;
+    }
     
     private Query createCompositeQuery(List<Query> queries, boolean orCondition) {
         

http://git-wip-us.apache.org/repos/asf/cxf/blob/84cbdf6a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
index 09a5ba1..330ff9b 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
@@ -211,7 +211,7 @@ public class TikaLuceneContentExtractor {
                     return new FloatField(name, Float.valueOf(value), Store.YES);
                 } else if (Long.class.isAssignableFrom(type)) {
                     return new LongField(name, Long.valueOf(value), Store.YES);
-                } else if (Integer.class.isAssignableFrom(type)) {
+                } else if (Integer.class.isAssignableFrom(type) || Byte.class.isAssignableFrom(type))
{
                     return new IntField(name, Integer.valueOf(value), Store.YES);
                 }
             } else if (Date.class.isAssignableFrom(type)) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/84cbdf6a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractorTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractorTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractorTest.java
index 5574806..2f87eb0 100644
--- a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractorTest.java
+++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractorTest.java
@@ -102,6 +102,101 @@ public class TikaLuceneContentExtractorTest extends Assert {
         assertEquals(0, getHits("modified=lt=2007-09-01T09:02:31", documentMetadata.getFieldTypes()).length);
     }
     
+    @Test
+    public void testExtractedTextContentMatchesTypesAndIntegerSearchCriteria() throws Exception
{
+        final LuceneDocumentMetadata documentMetadata = new LuceneDocumentMetadata("contents")
+            .withField("xmpTPg:NPages", Integer.class);
+        
+        final Document document = extractor.extract(
+            getClass().getResourceAsStream("/files/testPDF.pdf"), documentMetadata);
+        assertNotNull("Document should not be null", document);
+        
+        writer.addDocument(document);
+        writer.commit();
+
+        assertEquals(1, getHits("xmpTPg:NPages=gt=0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages==1", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages=ge=1", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=gt=1", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=lt=1", documentMetadata.getFieldTypes()).length);
+    }
+    
+    @Test
+    public void testExtractedTextContentMatchesTypesAndByteSearchCriteria() throws Exception
{
+        final LuceneDocumentMetadata documentMetadata = new LuceneDocumentMetadata("contents")
+            .withField("xmpTPg:NPages", Byte.class);
+        
+        final Document document = extractor.extract(
+            getClass().getResourceAsStream("/files/testPDF.pdf"), documentMetadata);
+        assertNotNull("Document should not be null", document);
+        
+        writer.addDocument(document);
+        writer.commit();
+
+        assertEquals(1, getHits("xmpTPg:NPages=gt=0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages==1", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages=ge=1", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=gt=1", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=lt=1", documentMetadata.getFieldTypes()).length);
+    }
+    
+    @Test
+    public void testExtractedTextContentMatchesTypesAndLongSearchCriteria() throws Exception
{
+        final LuceneDocumentMetadata documentMetadata = new LuceneDocumentMetadata("contents")
+            .withField("xmpTPg:NPages", Long.class);
+        
+        final Document document = extractor.extract(
+            getClass().getResourceAsStream("/files/testPDF.pdf"), documentMetadata);
+        assertNotNull("Document should not be null", document);
+        
+        writer.addDocument(document);
+        writer.commit();
+
+        assertEquals(1, getHits("xmpTPg:NPages=gt=0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages==1", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages=ge=1", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=gt=1", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=lt=1", documentMetadata.getFieldTypes()).length);
+    }
+    
+    @Test
+    public void testExtractedTextContentMatchesTypesAndDoubleSearchCriteria() throws Exception
{
+        final LuceneDocumentMetadata documentMetadata = new LuceneDocumentMetadata("contents")
+            .withField("xmpTPg:NPages", Double.class);
+        
+        final Document document = extractor.extract(
+            getClass().getResourceAsStream("/files/testPDF.pdf"), documentMetadata);
+        assertNotNull("Document should not be null", document);
+        
+        writer.addDocument(document);
+        writer.commit();
+
+        assertEquals(1, getHits("xmpTPg:NPages=gt=0.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages==1.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages=ge=1.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=gt=1.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=lt=1.0", documentMetadata.getFieldTypes()).length);
+    }
+    
+    @Test
+    public void testExtractedTextContentMatchesTypesAndFloatSearchCriteria() throws Exception
{
+        final LuceneDocumentMetadata documentMetadata = new LuceneDocumentMetadata("contents")
+            .withField("xmpTPg:NPages", Float.class);
+        
+        final Document document = extractor.extract(
+            getClass().getResourceAsStream("/files/testPDF.pdf"), documentMetadata);
+        assertNotNull("Document should not be null", document);
+        
+        writer.addDocument(document);
+        writer.commit();
+
+        assertEquals(1, getHits("xmpTPg:NPages=gt=0.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages==1.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(1, getHits("xmpTPg:NPages=ge=1.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=gt=1.0", documentMetadata.getFieldTypes()).length);
+        assertEquals(0, getHits("xmpTPg:NPages=lt=1.0", documentMetadata.getFieldTypes()).length);
+    }
+    
     private ScoreDoc[] getHits(final String expression) throws IOException {
         return getHits(expression, new HashMap<String, Class<?>>());
     }


Mime
View raw message