Example usage for org.apache.lucene.search Sort INDEXORDER

List of usage examples for org.apache.lucene.search Sort INDEXORDER

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort INDEXORDER.

Prototype

Sort INDEXORDER

To view the source code for org.apache.lucene.search Sort INDEXORDER.

Click Source Link

Document

Represents sorting by index order.

Usage

From source file:com.querydsl.lucene4.AbstractLuceneQuery.java

License:Apache License

@Nullable
private T oneResult(boolean unique) {
    try {//from  w  ww  .j a v a2 s.  c  o m
        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 e) {
        throw new QueryException(e);
    } catch (IllegalArgumentException e) {
        throw new QueryException(e);
    }
}

From source file:com.querydsl.lucene5.AbstractLuceneQuery.java

License:Apache License

private long innerCount() {
    try {//ww w . j  av  a  2  s .  com
        final int maxDoc = searcher.getIndexReader().maxDoc();
        if (maxDoc == 0) {
            return 0;
        }
        return searcher.search(createQuery(), maxDoc, Sort.INDEXORDER, false, false).totalHits;
    } catch (IOException e) {
        throw new QueryException(e);
    } catch (IllegalArgumentException e) {
        throw new QueryException(e);
    }
}

From source file:com.querydsl.lucene5.AbstractLuceneQuery.java

License:Apache License

@Override
public CloseableIterator<T> iterate() {
    final QueryMetadata metadata = queryMixin.getMetadata();
    final List<OrderSpecifier<?>> orderBys = metadata.getOrderBy();
    final Integer queryLimit = metadata.getModifiers().getLimitAsInteger();
    final Integer queryOffset = metadata.getModifiers().getOffsetAsInteger();
    Sort sort = querySort;/*from   w  w w  .j a  va 2 s  . co m*/
    int limit;
    final int offset = queryOffset != null ? queryOffset : 0;
    try {
        limit = maxDoc();
        if (limit == 0) {
            return new EmptyCloseableIterator<T>();
        }
    } catch (IOException e) {
        throw new QueryException(e);
    } catch (IllegalArgumentException e) {
        throw new QueryException(e);
    }
    if (queryLimit != null && queryLimit < limit) {
        limit = queryLimit;
    }
    if (sort == null && !orderBys.isEmpty()) {
        sort = serializer.toSort(orderBys);
    }

    try {
        ScoreDoc[] scoreDocs;
        int sumOfLimitAndOffset = limit + offset;
        if (sumOfLimitAndOffset < 1) {
            throw new QueryException(
                    "The given limit (" + limit + ") and offset (" + offset + ") cause an integer overflow.");
        }
        if (sort != null) {
            scoreDocs = searcher.search(createQuery(),
                    // sumOfLimitAndOffset).scoreDocs;
                    sumOfLimitAndOffset, sort, false, false).scoreDocs;
        } else {
            scoreDocs = searcher.search(createQuery(), sumOfLimitAndOffset, Sort.INDEXORDER, false,
                    false).scoreDocs;
        }
        if (offset < scoreDocs.length) {
            return new ResultIterator<T>(scoreDocs, offset, searcher, fieldsToLoad, transformer);
        }
        return new EmptyCloseableIterator<T>();
    } catch (final IOException e) {
        throw new QueryException(e);
    }
}

From source file:com.querydsl.lucene5.AbstractLuceneQuery.java

License:Apache License

@Nullable
private T oneResult(boolean unique) {
    try {/*  www  .  ja va 2  s. c o m*/
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), 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 e) {
        throw new QueryException(e);
    } catch (IllegalArgumentException e) {
        throw new QueryException(e);
    }
}

From source file:com.sindicetech.siren.search.node.TestNodeNumericRangeQuery32.java

License:Open Source License

/** test for both constant score and boolean query, the other tests only use the constant score mode */
private void testRange(final int precisionStep) throws Exception {
    final String field = "field" + precisionStep;
    final int count = 3000;
    final int lower = (distance * 3 / 2) + startOffset, upper = lower + count * distance + (distance / 3);

    final Query dq = twq(1)
            .with(child(must(nmqInt(field, precisionStep, lower, upper, true, true)
                    .setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE).bound(2, 2))))
            .getLuceneProxyQuery();//  w  ww  .j av a  2  s  . c  o  m

    TopDocs topDocs;
    String type;

    type = " (constant score boolean rewrite)";
    topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);

    final ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count" + type, count, sd.length);
    Document doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc" + type, 2 * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc" + type, (1 + count) * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
}

From source file:com.sindicetech.siren.search.node.TestNodeNumericRangeQuery32.java

License:Open Source License

@Test
public void testInverseRange() throws Exception {
    Query dq = twq(1)/*  w  w w . java 2 s. c om*/
            .with(child(must(nmqInt("field8", 8, 1000, -1000, true, true)
                    .setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE))))
            .getLuceneProxyQuery();

    TopDocs topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    assertEquals("A inverse range should return the EMPTY_DOCIDSET instance", 0, topDocs.totalHits);

    dq = twq(1)
            .with(child(must(nmqInt("field8", 8, Integer.MAX_VALUE, null, false, false)
                    .setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE))))
            .getLuceneProxyQuery();
    topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    assertEquals("A exclusive range starting with Integer.MAX_VALUE should return the EMPTY_DOCIDSET instance",
            0, topDocs.totalHits);

    dq = twq(1)
            .with(child(must(nmqInt("field8", 8, null, Integer.MIN_VALUE, false, false)
                    .setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE))))
            .getLuceneProxyQuery();
    topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    assertEquals("A exclusive range ending with Integer.MIN_VALUE should return the EMPTY_DOCIDSET instance", 0,
            topDocs.totalHits);
}

From source file:com.sindicetech.siren.search.node.TestNodeNumericRangeQuery32.java

License:Open Source License

private void testLeftOpenRange(final int precisionStep) throws Exception {
    final String field = "field" + precisionStep;
    final int count = 3000;
    final int upper = (count - 1) * distance + (distance / 3) + startOffset;

    Query dq = twq(1).with(child(must(nmqInt(field, precisionStep, null, upper, true, true).bound(2, 2))))
            .getLuceneProxyQuery();/*from w  ww.  j a v  a2  s.com*/

    TopDocs topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", count, sd.length);
    Document doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc", startOffset, Integer.parseInt(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (count - 1) * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));

    dq = twq(1).with(child(must(nmqInt(field, precisionStep, null, upper, false, true).bound(2, 2))))
            .getLuceneProxyQuery();
    topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", count, sd.length);
    doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc", startOffset, Integer.parseInt(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (count - 1) * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
}

From source file:com.sindicetech.siren.search.node.TestNodeNumericRangeQuery32.java

License:Open Source License

private void testRightOpenRange(final int precisionStep) throws Exception {
    final String field = "field" + precisionStep;
    final int count = 3000;
    final int lower = (count - 1) * distance + (distance / 3) + startOffset;

    Query dq = twq(1).with(child(must(nmqInt(field, precisionStep, lower, null, true, true).bound(2, 2))))
            .getLuceneProxyQuery();//from   ww  w.jav  a2 s  .c o m

    TopDocs topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", noDocs - count, sd.length);
    Document doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc", count * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (noDocs - 1) * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));

    dq = twq(1).with(child(must(nmqInt(field, precisionStep, lower, null, true, false).bound(2, 2))))
            .getLuceneProxyQuery();
    topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", noDocs - count, sd.length);
    doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc", count * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (noDocs - 1) * distance + startOffset,
            Integer.parseInt(getLiteralValue(doc.get(field))));
}

From source file:com.sindicetech.siren.search.node.TestNodeNumericRangeQuery64.java

License:Open Source License

/** test for constant score + boolean query + filter, the other tests only use the constant score mode */
private void testRange(final int precisionStep) throws Exception {
    final String field = "field" + precisionStep;
    final int count = 3000;
    final long lower = (distance * 3 / 2) + startOffset, upper = lower + count * distance + (distance / 3);

    final Query dq = twq(1)
            .with(child(must(nmqLong(field, precisionStep, lower, upper, true, true)
                    .setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE).bound(2, 2))))
            .getLuceneProxyQuery();/* w ww .j  av a  2s  .c  o m*/

    final String type = " (constant score boolean rewrite)";

    final TopDocs topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);

    final ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count" + type, count, sd.length);
    Document doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc" + type, 2 * distance + startOffset,
            Long.parseLong(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc" + type, (1 + count) * distance + startOffset,
            Long.parseLong(getLiteralValue(doc.get(field))));

}

From source file:com.sindicetech.siren.search.node.TestNodeNumericRangeQuery64.java

License:Open Source License

private void testLeftOpenRange(final int precisionStep) throws Exception {
    final String field = "field" + precisionStep;
    final int count = 3000;
    final long upper = (count - 1) * distance + (distance / 3) + startOffset;

    Query dq = twq(1).with(child(must(nmqLong(field, precisionStep, null, upper, true, true).bound(2, 2))))
            .getLuceneProxyQuery();//  www. java 2  s . com

    TopDocs topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", count, sd.length);
    Document doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc", startOffset, Long.parseLong(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (count - 1) * distance + startOffset,
            Long.parseLong(getLiteralValue(doc.get(field))));

    dq = twq(1).with(child(must(nmqLong(field, precisionStep, null, upper, false, true).bound(2, 2))))
            .getLuceneProxyQuery();
    topDocs = index.searcher.search(dq, null, noDocs, Sort.INDEXORDER);
    sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", count, sd.length);
    doc = index.searcher.doc(sd[0].doc);
    assertEquals("First doc", startOffset, Long.parseLong(getLiteralValue(doc.get(field))));
    doc = index.searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (count - 1) * distance + startOffset,
            Long.parseLong(getLiteralValue(doc.get(field))));
}