Search in sources :

Example 1 with NumericRangeQuery

use of org.apache.lucene.search.NumericRangeQuery in project neo4j by neo4j.

the class QueryContext method sortNumeric.

/**
     * Sort the results of a numeric range query if the query in this context
     * is a {@link NumericRangeQuery}, see {@link #numericRange(String, Number, Number)},
     * Otherwise an {@link IllegalStateException} will be thrown.
     *
     * @param key the key to sort on.
     * @param reversed if the sort order should be reversed or not. {@code true}
     * for lowest first (ascending), {@code false} for highest first (descending)
     * @return a QueryContext with sorting by numeric value.
     */
public QueryContext sortNumeric(String key, boolean reversed) {
    if (!(queryOrQueryObject instanceof NumericRangeQuery)) {
        throw new IllegalStateException("Not a numeric range query");
    }
    Number number = ((NumericRangeQuery) queryOrQueryObject).getMin();
    number = number != null ? number : ((NumericRangeQuery) queryOrQueryObject).getMax();
    SortField.Type fieldType = SortField.Type.INT;
    if (number instanceof Long) {
        fieldType = SortField.Type.LONG;
    } else if (number instanceof Float) {
        fieldType = SortField.Type.FLOAT;
    } else if (number instanceof Double) {
        fieldType = SortField.Type.DOUBLE;
    }
    sort(new Sort(new SortedNumericSortField(key, fieldType, reversed)));
    return this;
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) Sort(org.apache.lucene.search.Sort) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 2 with NumericRangeQuery

use of org.apache.lucene.search.NumericRangeQuery in project neo4j-mobile-android by neo4j-contrib.

the class QueryContext method sortNumeric.

/**
     * Sort the results of a numeric range query if the query in this context
     * is a {@link NumericRangeQuery}, see {@link #numericRange(String, Number, Number)},
     * Otherwise an {@link IllegalStateException} will be thrown.
     * 
     * @param key the key to sort on.
     * @param reversed if the sort order should be reversed or not. {@code true}
     * for lowest first (ascending), {@code false} for highest first (descending)
     * @return a QueryContext with sorting by numeric value.
     */
public QueryContext sortNumeric(String key, boolean reversed) {
    if (!(queryOrQueryObject instanceof NumericRangeQuery)) {
        throw new IllegalStateException("Not a numeric range query");
    }
    Number number = ((NumericRangeQuery) queryOrQueryObject).getMin();
    number = number != null ? number : ((NumericRangeQuery) queryOrQueryObject).getMax();
    int fieldType = SortField.INT;
    if (number instanceof Long) {
        fieldType = SortField.LONG;
    } else if (number instanceof Float) {
        fieldType = SortField.FLOAT;
    } else if (number instanceof Double) {
        fieldType = SortField.DOUBLE;
    }
    sort(new Sort(new SortField(key, fieldType, reversed)));
    return this;
}
Also used : NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Example 3 with NumericRangeQuery

use of org.apache.lucene.search.NumericRangeQuery in project neo4j by neo4j.

the class LuceneDocumentStructureTest method shouldBuildQueryRepresentingMultipleProperties.

@Test
public void shouldBuildQueryRepresentingMultipleProperties() throws Exception {
    // given
    BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery(true, "Characters", 12, new Integer[] { 1, 2, 3 });
    ConstantScoreQuery boolScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(0).getQuery();
    TermQuery boolTermQuery = (TermQuery) boolScoreQuery.getQuery();
    ConstantScoreQuery stringScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(1).getQuery();
    TermQuery stringTermQuery = (TermQuery) stringScoreQuery.getQuery();
    ConstantScoreQuery numberScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(2).getQuery();
    NumericRangeQuery<Double> numericRangeQuery = (NumericRangeQuery<Double>) numberScoreQuery.getQuery();
    ConstantScoreQuery arrayScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(3).getQuery();
    TermQuery arrayTermQuery = (TermQuery) arrayScoreQuery.getQuery();
    // then
    assertEquals("true", boolTermQuery.getTerm().text());
    assertEquals("Characters", stringTermQuery.getTerm().text());
    assertEquals(12.0, numericRangeQuery.getMin());
    assertEquals(12.0, numericRangeQuery.getMax());
    assertEquals("D1.0|2.0|3.0|", arrayTermQuery.getTerm().text());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Test(org.junit.Test)

Example 4 with NumericRangeQuery

use of org.apache.lucene.search.NumericRangeQuery in project neo4j by neo4j.

the class LuceneDocumentStructureTest method shouldBuildQueryRepresentingNumberProperty.

@SuppressWarnings("unchecked")
@Test
public void shouldBuildQueryRepresentingNumberProperty() throws Exception {
    // given
    BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery(12);
    ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(0).getQuery();
    NumericRangeQuery<Double> query = (NumericRangeQuery<Double>) constantScoreQuery.getQuery();
    // then
    assertEquals(12.0, query.getMin());
    assertEquals(12.0, query.getMax());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Test(org.junit.Test)

Aggregations

NumericRangeQuery (org.apache.lucene.search.NumericRangeQuery)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 Sort (org.apache.lucene.search.Sort)2 SortField (org.apache.lucene.search.SortField)2 Test (org.junit.Test)2 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)1 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)1 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)1 TermQuery (org.apache.lucene.search.TermQuery)1