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;
}
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;
}
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());
}
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());
}
Aggregations