Search in sources :

Example 1 with QueryModifiers

use of com.querydsl.core.QueryModifiers in project querydsl by querydsl.

the class LuceneQueryTest method listDistinctResults.

@Test(expected = UnsupportedOperationException.class)
public void listDistinctResults() {
    query.where(year.between(1800, 2000).or(title.eq("The Lord of the Rings")));
    query.restrict(new QueryModifiers(1L, 1L));
    query.orderBy(year.asc());
    final QueryResults<Document> results = query.distinct().fetchResults();
    assertFalse(results.isEmpty());
    assertEquals("1954", results.getResults().get(0).get("year"));
    assertEquals(1, results.getLimit());
    assertEquals(1, results.getOffset());
    assertEquals(4, results.getTotal());
}
Also used : QueryModifiers(com.querydsl.core.QueryModifiers) Document(org.apache.lucene.document.Document) Test(org.junit.Test)

Example 2 with QueryModifiers

use of com.querydsl.core.QueryModifiers in project querydsl by querydsl.

the class LuceneQueryTest method listResults.

@Test
public void listResults() {
    query.where(year.between(1800, 2000));
    query.restrict(new QueryModifiers(2L, 1L));
    query.orderBy(year.asc());
    final QueryResults<Document> results = query.fetchResults();
    assertFalse(results.isEmpty());
    assertEquals("1954", results.getResults().get(0).get("year"));
    assertEquals("1990", results.getResults().get(1).get("year"));
    assertEquals(2, results.getLimit());
    assertEquals(1, results.getOffset());
    assertEquals(4, results.getTotal());
}
Also used : QueryModifiers(com.querydsl.core.QueryModifiers) Document(org.apache.lucene.document.Document) Test(org.junit.Test)

Example 3 with QueryModifiers

use of com.querydsl.core.QueryModifiers in project querydsl by querydsl.

the class AbstractLuceneQuery method oneResult.

@Nullable
private T oneResult(boolean unique) {
    try {
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), getFilter(), maxDoc).scoreDocs;
        int index = 0;
        QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
        Long offset = modifiers.getOffset();
        if (offset != null) {
            index = offset.intValue();
        }
        Long limit = modifiers.getLimit();
        if (unique && (limit == null ? scoreDocs.length - index > 1 : limit > 1 && scoreDocs.length > 1)) {
            throw new NonUniqueResultException("Unique result requested, but " + scoreDocs.length + " found.");
        } else if (scoreDocs.length > index) {
            Document document;
            if (fieldSelector != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldSelector);
            } else {
                document = searcher.doc(scoreDocs[index].doc);
            }
            return transformer.apply(document);
        } else {
            return null;
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) QueryModifiers(com.querydsl.core.QueryModifiers) QueryException(com.querydsl.core.QueryException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with QueryModifiers

use of com.querydsl.core.QueryModifiers in project querydsl by querydsl.

the class AbstractLuceneQuery method oneResult.

@Nullable
private T oneResult(boolean unique) {
    try {
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), getFilter(), maxDoc, Sort.INDEXORDER, false, false).scoreDocs;
        int index = 0;
        QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
        Long offset = modifiers.getOffset();
        if (offset != null) {
            index = offset.intValue();
        }
        Long limit = modifiers.getLimit();
        if (unique && (limit == null ? scoreDocs.length - index > 1 : limit > 1 && scoreDocs.length > 1)) {
            throw new NonUniqueResultException("Unique result requested, but " + scoreDocs.length + " found.");
        } else if (scoreDocs.length > index) {
            Document document;
            if (fieldsToLoad != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldsToLoad);
            } else {
                document = searcher.doc(scoreDocs[index].doc);
            }
            return transformer.apply(document);
        } else {
            return null;
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) QueryModifiers(com.querydsl.core.QueryModifiers) QueryException(com.querydsl.core.QueryException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with QueryModifiers

use of com.querydsl.core.QueryModifiers in project querydsl by querydsl.

the class LuceneQueryTest method listResults.

@Test
public void listResults() {
    query.where(year.between(1800, 2000));
    query.restrict(new QueryModifiers(2L, 1L));
    query.orderBy(year.asc());
    final QueryResults<Document> results = query.fetchResults();
    assertFalse(results.isEmpty());
    assertEquals("1954", results.getResults().get(0).get("year"));
    assertEquals("1990", results.getResults().get(1).get("year"));
    assertEquals(2, results.getLimit());
    assertEquals(1, results.getOffset());
    assertEquals(4, results.getTotal());
}
Also used : QueryModifiers(com.querydsl.core.QueryModifiers) Test(org.junit.Test)

Aggregations

QueryModifiers (com.querydsl.core.QueryModifiers)25 Test (org.junit.Test)11 Document (org.apache.lucene.document.Document)9 QueryFlag (com.querydsl.core.QueryFlag)7 NonUniqueResultException (com.querydsl.core.NonUniqueResultException)3 QueryException (com.querydsl.core.QueryException)3 IOException (java.io.IOException)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 Nullable (org.jetbrains.annotations.Nullable)3 QueryMixin (com.querydsl.core.support.QueryMixin)1 QEmployee (com.querydsl.sql.domain.QEmployee)1 ArrayList (java.util.ArrayList)1