From 30ee4ec22f342eb0fac9e7e4cf973295135f0208 Mon Sep 17 00:00:00 2001 From: sdeschenes Date: Wed, 20 May 2015 13:05:41 -0400 Subject: [PATCH] Test are working as expected --- querydsl-lucene5/pom.xml | 9 +- .../querydsl/lucene5/AbstractLuceneQuery.java | 27 ++++- .../querydsl/lucene5/LuceneSerializer.java | 3 +- .../com/querydsl/lucene5/LuceneQueryTest.java | 100 ++++++++++++++---- 4 files changed, 114 insertions(+), 25 deletions(-) diff --git a/querydsl-lucene5/pom.xml b/querydsl-lucene5/pom.xml index 27126a6cd..a8c3cabdb 100644 --- a/querydsl-lucene5/pom.xml +++ b/querydsl-lucene5/pom.xml @@ -95,7 +95,14 @@ - + + + maven-compiler-plugin + + 1.7 + 1.7 + + org.apache.felix maven-bundle-plugin diff --git a/querydsl-lucene5/src/main/java/com/querydsl/lucene5/AbstractLuceneQuery.java b/querydsl-lucene5/src/main/java/com/querydsl/lucene5/AbstractLuceneQuery.java index 4238dcc70..c19354169 100644 --- a/querydsl-lucene5/src/main/java/com/querydsl/lucene5/AbstractLuceneQuery.java +++ b/querydsl-lucene5/src/main/java/com/querydsl/lucene5/AbstractLuceneQuery.java @@ -22,6 +22,7 @@ import java.util.Set; import javax.annotation.Nullable; import org.apache.lucene.document.Document; +import org.apache.lucene.index.DocValues; import org.apache.lucene.sandbox.queries.DuplicateFilter; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; @@ -105,7 +106,7 @@ public abstract class AbstractLuceneQuery if (maxDoc == 0) { return 0; } - return searcher.search(createQuery(), getFilter(), maxDoc, + return searcher.search(createSearchedQuery(), maxDoc, Sort.INDEXORDER, false, false).totalHits; } catch (IOException e) { throw new QueryException(e); @@ -226,10 +227,11 @@ public abstract class AbstractLuceneQuery + ") cause an integer overflow."); } if (sort != null) { - scoreDocs = searcher.search(createQuery(), getFilter(), + scoreDocs = searcher.search(createSearchedQuery(), +// sumOfLimitAndOffset).scoreDocs; sumOfLimitAndOffset, sort, false, false).scoreDocs; } else { - scoreDocs = searcher.search(createQuery(), getFilter(), + scoreDocs = searcher.search(createSearchedQuery(), sumOfLimitAndOffset, Sort.INDEXORDER, false, false).scoreDocs; } if (offset < scoreDocs.length) { @@ -242,6 +244,21 @@ public abstract class AbstractLuceneQuery } } + private Query createSearchedQuery() { + Query returnedQuery = null; + Query originalQuery = createQuery(); + Filter filter = getFilter(); + if (filter != null) { + BooleanQuery booleanQuery = new BooleanQuery(); + booleanQuery.add(originalQuery, Occur.MUST); + booleanQuery.add(filter, Occur.FILTER); + returnedQuery = booleanQuery; + } else { + returnedQuery = originalQuery; + } + return returnedQuery; + } + private List innerList() { return new IteratorAdapter(iterate()).asList(); } @@ -329,8 +346,8 @@ public abstract class AbstractLuceneQuery if (maxDoc == 0) { return null; } - final ScoreDoc[] scoreDocs = searcher.search(createQuery(), - getFilter(), maxDoc, Sort.INDEXORDER, false, false).scoreDocs; + final ScoreDoc[] scoreDocs = searcher.search(createSearchedQuery(), maxDoc, + Sort.INDEXORDER, false, false).scoreDocs; int index = 0; QueryModifiers modifiers = queryMixin.getMetadata().getModifiers(); Long offset = modifiers.getOffset(); diff --git a/querydsl-lucene5/src/main/java/com/querydsl/lucene5/LuceneSerializer.java b/querydsl-lucene5/src/main/java/com/querydsl/lucene5/LuceneSerializer.java index b1e36359b..b483fdccd 100644 --- a/querydsl-lucene5/src/main/java/com/querydsl/lucene5/LuceneSerializer.java +++ b/querydsl-lucene5/src/main/java/com/querydsl/lucene5/LuceneSerializer.java @@ -37,6 +37,7 @@ import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; +import org.apache.lucene.search.SortedNumericSortField; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.WildcardQuery; @@ -592,7 +593,7 @@ public class LuceneSerializer { boolean reverse = !order.isAscending(); Path path = getPath(order.getTarget()); if (Number.class.isAssignableFrom(type)) { - sorts.add(new SortField(toField(path), sortFields.get(type), + sorts.add(new SortedNumericSortField(toField(path), sortFields.get(type), reverse)); } else { sorts.add(new SortField(toField(path), SortField.Type.STRING, diff --git a/querydsl-lucene5/src/test/java/com/querydsl/lucene5/LuceneQueryTest.java b/querydsl-lucene5/src/test/java/com/querydsl/lucene5/LuceneQueryTest.java index 31e211531..838ba9a7d 100644 --- a/querydsl-lucene5/src/test/java/com/querydsl/lucene5/LuceneQueryTest.java +++ b/querydsl-lucene5/src/test/java/com/querydsl/lucene5/LuceneQueryTest.java @@ -33,11 +33,14 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.DoubleField; import org.apache.lucene.document.Field; -import org.apache.lucene.document.Field.Index; import org.apache.lucene.document.Field.Store; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.IntField; +import org.apache.lucene.document.NumericDocValuesField; +import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; @@ -46,6 +49,7 @@ import org.apache.lucene.search.Filter; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Sort; import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.util.BytesRef; import org.junit.After; import org.junit.Before; import org.junit.Ignore; @@ -80,17 +84,77 @@ public class LuceneQueryTest { private RAMDirectory idx; private IndexWriter writer; private IndexSearcher searcher; + private Document doc = new Document(); + private Field titleField = null; + private SortedDocValuesField titleSortedField; + private TextField authorField = null; + private SortedDocValuesField authorSortedField; + private TextField textField = null; + private IntField yearField = null; + private DoubleField grossField = null; + private SortedDocValuesField textSortedField; + private NumericDocValuesField yearSortedField; private Document createDocument(final String docTitle, final String docAuthor, final String docText, final int docYear, final double docGross) { - final Document doc = new Document(); + // Reusing field for performance + if (titleField == null) { + titleField = new TextField("title", docTitle, Store.YES); + doc.add(titleField); + titleSortedField = new SortedDocValuesField("title", new BytesRef( + docTitle)); + doc.add(titleSortedField); + } else { + titleField.setStringValue(docTitle); + titleSortedField.setBytesValue(new BytesRef(docTitle)); + } + if (authorField == null) { + authorField = new TextField("author", docAuthor, Store.YES); + doc.add(authorField); + authorSortedField = new SortedDocValuesField("author", + new BytesRef(docAuthor)); + doc.add(authorSortedField); - doc.add(new TextField("title", docTitle, Store.YES)); - doc.add(new TextField("author", docAuthor, Store.YES)); - doc.add(new TextField("text", docText, Store.YES)); - doc.add(new IntField("year", docYear, Store.YES)); - doc.add(new DoubleField("gross", docGross, Store.YES)); + } else { + authorField.setStringValue(docAuthor); + authorSortedField.setBytesValue(new BytesRef(docAuthor)); + } + if (textField == null) { + textField = new TextField("text", docText, Store.YES); + doc.add(textField); + textSortedField = new SortedDocValuesField("text", new BytesRef( + docText)); + doc.add(textSortedField); + } else { + textField.setStringValue(docText); + textSortedField.setBytesValue(new BytesRef(docText)); + } + if (yearField == null) { +// FieldType numericSortedStoredFieldType = new FieldType( +// IntField.TYPE_STORED); +// numericSortedStoredFieldType +// .setDocValuesType(DocValuesType.SORTED_NUMERIC); + yearField = new IntField("year", docYear, Store.YES); + doc.add(yearField); + yearSortedField = new NumericDocValuesField("year", docYear); + doc.add(yearSortedField); + } else { + yearField.setIntValue(docYear); + yearSortedField.setLongValue(docYear); + } + + if (grossField == null) { + FieldType numericSortedStoredFieldType = new FieldType( + DoubleField.TYPE_STORED); + numericSortedStoredFieldType + .setDocValuesType(DocValuesType.SORTED_NUMERIC); + grossField = new DoubleField("gross", docGross, + numericSortedStoredFieldType); + doc.add(grossField); + } else { + grossField.setDoubleValue(docGross); + } return doc; } @@ -204,16 +268,16 @@ public class LuceneQueryTest { Document d1 = new Document(); Document d2 = new Document(); Document d3 = new Document(); - d1.add(new Field("sort", "a\u00c4", Store.YES, Index.NOT_ANALYZED)); - d2.add(new Field("sort", "ab", Store.YES, Index.NOT_ANALYZED)); - d3.add(new Field("sort", "aa", Store.YES, Index.NOT_ANALYZED)); + d1.add(new TextField("sort", "a\u00c4", Store.YES)); + d2.add(new TextField("sort", "ab", Store.YES)); + d3.add(new TextField("sort", "aa", Store.YES)); writer = createWriter(idx); writer.addDocument(d1); writer.addDocument(d2); writer.addDocument(d3); writer.close(); - IndexReader reader = DirectoryReader.open(idx); + IndexReader reader = DirectoryReader.open(writer, true); searcher = new IndexSearcher(reader); query = new LuceneQuery( new LuceneSerializer(true, true, Locale.ENGLISH), searcher); @@ -565,8 +629,8 @@ public class LuceneQueryTest { @Ignore public void UniqueResult_Finds_No_Results_Because_No_Documents_In_Index() throws IOException { - searcher = createMockBuilder(IndexSearcher.class).addMockedMethod( - "maxDoc").createMock(); + IndexSearcher searcher = createMockBuilder(IndexSearcher.class) + .addMockedMethod("maxDoc").createMock(); query = new LuceneQuery(new LuceneSerializer(true, true), searcher); expect(searcher.getIndexReader().maxDoc()).andReturn(0); replay(searcher); @@ -578,8 +642,8 @@ public class LuceneQueryTest { @Ignore public void UniqueResult_Sorted_Index_Problem_In_Max_Doc() throws IOException { - searcher = createMockBuilder(IndexSearcher.class).addMockedMethod( - "maxDoc").createMock(); + IndexSearcher searcher = createMockBuilder(IndexSearcher.class) + .addMockedMethod("maxDoc").createMock(); query = new LuceneQuery(new LuceneSerializer(true, true), searcher); expect(searcher.getIndexReader().maxDoc()).andThrow( new IllegalArgumentException()); @@ -593,8 +657,8 @@ public class LuceneQueryTest { @Ignore public void Count_Returns_0_Because_No_Documents_In_Index() throws IOException { - searcher = createMockBuilder(IndexSearcher.class).addMockedMethod( - "maxDoc").createMock(); + IndexSearcher searcher = createMockBuilder(IndexSearcher.class) + .addMockedMethod("maxDoc").createMock(); query = new LuceneQuery(new LuceneSerializer(true, true), searcher); expect(searcher.getIndexReader().maxDoc()).andReturn(0); replay(searcher); @@ -734,7 +798,7 @@ public class LuceneQueryTest { writer = createWriter(idx); writer.close(); IndexReader reader = DirectoryReader.open(idx); - searcher = new IndexSearcher(reader); + IndexSearcher searcher = new IndexSearcher(reader); query = new LuceneQuery(new LuceneSerializer(true, true), searcher); assertTrue(query.fetch().isEmpty()); }