mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
#101 improved Lucene sorting options
This commit is contained in:
parent
9c0dd3a207
commit
6d5116a2e0
@ -72,6 +72,9 @@ SimpleProjectable<T> {
|
||||
|
||||
@Nullable
|
||||
private Filter filter;
|
||||
|
||||
@Nullable
|
||||
private Sort querySort;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public AbstractLuceneQuery(LuceneSerializer serializer, Searcher searcher,
|
||||
@ -163,7 +166,7 @@ SimpleProjectable<T> {
|
||||
final List<OrderSpecifier<?>> orderBys = metadata.getOrderBy();
|
||||
final Long queryLimit = metadata.getModifiers().getLimit();
|
||||
final Long queryOffset = metadata.getModifiers().getOffset();
|
||||
Sort sort = null;
|
||||
Sort sort = querySort;
|
||||
int limit;
|
||||
final int offset = queryOffset != null ? queryOffset.intValue() : 0;
|
||||
try {
|
||||
@ -177,7 +180,7 @@ SimpleProjectable<T> {
|
||||
if (queryLimit != null && queryLimit.intValue() < limit) {
|
||||
limit = queryLimit.intValue();
|
||||
}
|
||||
if (!orderBys.isEmpty()) {
|
||||
if (sort == null && !orderBys.isEmpty()) {
|
||||
sort = serializer.toSort(orderBys);
|
||||
}
|
||||
|
||||
@ -282,6 +285,12 @@ SimpleProjectable<T> {
|
||||
public <P> Q set(ParamExpression<P> param, P value) {
|
||||
return queryMixin.set(param, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Q sort(Sort sort) {
|
||||
this.querySort = sort;
|
||||
return (Q)this;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private T oneResult(boolean unique) {
|
||||
|
||||
@ -530,7 +530,7 @@ public class LuceneSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
public Sort toSort(List<OrderSpecifier<?>> orderBys) {
|
||||
public Sort toSort(List<? extends OrderSpecifier<?>> orderBys) {
|
||||
List<SortField> sorts = new ArrayList<SortField>(orderBys.size());
|
||||
for (OrderSpecifier<?> order : orderBys) {
|
||||
if (!(order.getTarget() instanceof Path<?>)) {
|
||||
|
||||
@ -24,6 +24,7 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -31,16 +32,17 @@ import java.util.Locale;
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.MapFieldSelector;
|
||||
import org.apache.lucene.document.NumericField;
|
||||
import org.apache.lucene.document.Field.Index;
|
||||
import org.apache.lucene.document.Field.Store;
|
||||
import org.apache.lucene.document.MapFieldSelector;
|
||||
import org.apache.lucene.document.NumericField;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
|
||||
import org.apache.lucene.search.DuplicateFilter;
|
||||
import org.apache.lucene.search.Filter;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Searcher;
|
||||
import org.apache.lucene.search.Sort;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
import org.apache.lucene.util.Version;
|
||||
import org.junit.After;
|
||||
@ -288,6 +290,23 @@ public class LuceneQueryTest {
|
||||
assertEquals("1990", documents.get(2).get("year"));
|
||||
assertEquals("1990", documents.get(3).get("year"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void List_Sort() {
|
||||
Sort sort = LuceneSerializer.DEFAULT.toSort(Collections.singletonList(year.asc()));
|
||||
|
||||
query.where(year.between(1800, 2000));
|
||||
//query.orderBy(year.asc());
|
||||
query.sort(sort);
|
||||
final List<Document> documents = query.list();
|
||||
assertFalse(documents.isEmpty());
|
||||
assertEquals(4, documents.size());
|
||||
assertEquals("1864", documents.get(0).get("year"));
|
||||
assertEquals("1954", documents.get(1).get("year"));
|
||||
assertEquals("1990", documents.get(2).get("year"));
|
||||
assertEquals("1990", documents.get(3).get("year"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void List_Distinct_Property(){
|
||||
@ -320,6 +339,7 @@ public class LuceneQueryTest {
|
||||
assertEquals("1954", documents.get(2).get("year"));
|
||||
assertEquals("1864", documents.get(3).get("year"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void List_Sorted_Descending_By_Gross() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user