本文整理汇总了Java中org.apache.lucene.util.NumericUtils类的典型用法代码示例。如果您正苦于以下问题:Java NumericUtils类的具体用法?Java NumericUtils怎么用?Java NumericUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NumericUtils类属于org.apache.lucene.util包,在下文中一共展示了NumericUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createFields
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
@Override
public List<Field> createFields(String name, Number value,
boolean indexed, boolean docValued, boolean stored) {
List<Field> fields = new ArrayList<>();
if (indexed) {
fields.add(new FloatPoint(name, value.floatValue()));
}
if (docValued) {
fields.add(new SortedNumericDocValuesField(name,
NumericUtils.floatToSortableInt(value.floatValue())));
}
if (stored) {
fields.add(new StoredField(name, value.floatValue()));
}
return fields;
}
示例2: doXContentBody
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
@Override
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
super.doXContentBody(builder, includeDefaults, params);
if (includeDefaults || pathType != Defaults.PATH_TYPE) {
builder.field("path", pathType.name().toLowerCase(Locale.ROOT));
}
if (includeDefaults || fieldType().isLatLonEnabled() != GeoPointFieldMapper.Defaults.ENABLE_LATLON) {
builder.field("lat_lon", fieldType().isLatLonEnabled());
}
if (fieldType().isLatLonEnabled() && (includeDefaults || fieldType().latFieldType().numericPrecisionStep() != NumericUtils.PRECISION_STEP_DEFAULT)) {
builder.field("precision_step", fieldType().latFieldType().numericPrecisionStep());
}
if (includeDefaults || fieldType().isGeoHashEnabled() != Defaults.ENABLE_GEOHASH) {
builder.field("geohash", fieldType().isGeoHashEnabled());
}
if (includeDefaults || fieldType().isGeoHashPrefixEnabled() != Defaults.ENABLE_GEOHASH_PREFIX) {
builder.field("geohash_prefix", fieldType().isGeoHashPrefixEnabled());
}
if (fieldType().isGeoHashEnabled() && (includeDefaults || fieldType().geoHashPrecision() != Defaults.GEO_HASH_PRECISION)) {
builder.field("geohash_precision", fieldType().geoHashPrecision());
}
if (includeDefaults || ignoreMalformed.explicit()) {
builder.field(Names.IGNORE_MALFORMED, ignoreMalformed.value());
}
}
示例3: normalizeFieldValue
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public String normalizeFieldValue(String field, Object value) {
if (NumberUtils.isNumber(value.toString())) {
Number n = NumberUtils.createNumber(value.toString());
if (n instanceof Integer)
return NumericUtils.intToPrefixCoded((Integer) n);
else if (n instanceof Long)
return NumericUtils.longToPrefixCoded((Long) n);
else if (n instanceof Float)
return NumericUtils.floatToPrefixCoded((Float) n);
else if (n instanceof Double)
return NumericUtils.doubleToPrefixCoded((Double) n);
else
throw new IllegalArgumentException("Unhandled numeric type: " + n.getClass());
} else {
throw new IllegalArgumentException("Value is not a number: " + value);
}
}
示例4: testIntFieldMinMax
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public void testIntFieldMinMax() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
int numDocs = atLeast(100);
int minValue = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;
for(int i=0;i<numDocs;i++ ){
Document doc = new Document();
int num = random().nextInt();
minValue = Math.min(num, minValue);
maxValue = Math.max(num, maxValue);
doc.add(new IntField("field", num, Field.Store.NO));
w.addDocument(doc);
}
IndexReader r = w.getReader();
Terms terms = MultiFields.getTerms(r, "field");
assertEquals(minValue, NumericUtils.getMinInt(terms));
assertEquals(maxValue, NumericUtils.getMaxInt(terms));
r.close();
w.close();
dir.close();
}
示例5: testLongFieldMinMax
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public void testLongFieldMinMax() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
int numDocs = atLeast(100);
long minValue = Long.MAX_VALUE;
long maxValue = Long.MIN_VALUE;
for(int i=0;i<numDocs;i++ ){
Document doc = new Document();
long num = random().nextLong();
minValue = Math.min(num, minValue);
maxValue = Math.max(num, maxValue);
doc.add(new LongField("field", num, Field.Store.NO));
w.addDocument(doc);
}
IndexReader r = w.getReader();
Terms terms = MultiFields.getTerms(r, "field");
assertEquals(minValue, NumericUtils.getMinLong(terms));
assertEquals(maxValue, NumericUtils.getMaxLong(terms));
r.close();
w.close();
dir.close();
}
示例6: testFloatFieldMinMax
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public void testFloatFieldMinMax() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
int numDocs = atLeast(100);
float minValue = Float.POSITIVE_INFINITY;
float maxValue = Float.NEGATIVE_INFINITY;
for(int i=0;i<numDocs;i++ ){
Document doc = new Document();
float num = random().nextFloat();
minValue = Math.min(num, minValue);
maxValue = Math.max(num, maxValue);
doc.add(new FloatField("field", num, Field.Store.NO));
w.addDocument(doc);
}
IndexReader r = w.getReader();
Terms terms = MultiFields.getTerms(r, "field");
assertEquals(minValue, NumericUtils.sortableIntToFloat(NumericUtils.getMinInt(terms)), 0.0f);
assertEquals(maxValue, NumericUtils.sortableIntToFloat(NumericUtils.getMaxInt(terms)), 0.0f);
r.close();
w.close();
dir.close();
}
示例7: testDoubleFieldMinMax
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public void testDoubleFieldMinMax() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
int numDocs = atLeast(100);
double minValue = Double.POSITIVE_INFINITY;
double maxValue = Double.NEGATIVE_INFINITY;
for(int i=0;i<numDocs;i++ ){
Document doc = new Document();
double num = random().nextDouble();
minValue = Math.min(num, minValue);
maxValue = Math.max(num, maxValue);
doc.add(new DoubleField("field", num, Field.Store.NO));
w.addDocument(doc);
}
IndexReader r = w.getReader();
Terms terms = MultiFields.getTerms(r, "field");
assertEquals(minValue, NumericUtils.sortableLongToDouble(NumericUtils.getMinLong(terms)), 0.0);
assertEquals(maxValue, NumericUtils.sortableLongToDouble(NumericUtils.getMaxLong(terms)), 0.0);
r.close();
w.close();
dir.close();
}
示例8: testLongStream
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public void testLongStream() throws Exception {
final NumericTokenStream stream=new NumericTokenStream().setLongValue(lvalue);
final TermToBytesRefAttribute bytesAtt = stream.getAttribute(TermToBytesRefAttribute.class);
assertNotNull(bytesAtt);
final TypeAttribute typeAtt = stream.getAttribute(TypeAttribute.class);
assertNotNull(typeAtt);
final NumericTokenStream.NumericTermAttribute numericAtt = stream.getAttribute(NumericTokenStream.NumericTermAttribute.class);
assertNotNull(numericAtt);
final BytesRef bytes = bytesAtt.getBytesRef();
stream.reset();
assertEquals(64, numericAtt.getValueSize());
for (int shift=0; shift<64; shift+=NumericUtils.PRECISION_STEP_DEFAULT) {
assertTrue("New token is available", stream.incrementToken());
assertEquals("Shift value wrong", shift, numericAtt.getShift());
bytesAtt.fillBytesRef();
assertEquals("Term is incorrectly encoded", lvalue & ~((1L << shift) - 1L), NumericUtils.prefixCodedToLong(bytes));
assertEquals("Term raw value is incorrectly encoded", lvalue & ~((1L << shift) - 1L), numericAtt.getRawValue());
assertEquals("Type incorrect", (shift == 0) ? NumericTokenStream.TOKEN_TYPE_FULL_PREC : NumericTokenStream.TOKEN_TYPE_LOWER_PREC, typeAtt.type());
}
assertFalse("More tokens available", stream.incrementToken());
stream.end();
stream.close();
}
示例9: testIntStream
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
public void testIntStream() throws Exception {
final NumericTokenStream stream=new NumericTokenStream().setIntValue(ivalue);
final TermToBytesRefAttribute bytesAtt = stream.getAttribute(TermToBytesRefAttribute.class);
assertNotNull(bytesAtt);
final TypeAttribute typeAtt = stream.getAttribute(TypeAttribute.class);
assertNotNull(typeAtt);
final NumericTokenStream.NumericTermAttribute numericAtt = stream.getAttribute(NumericTokenStream.NumericTermAttribute.class);
assertNotNull(numericAtt);
final BytesRef bytes = bytesAtt.getBytesRef();
stream.reset();
assertEquals(32, numericAtt.getValueSize());
for (int shift=0; shift<32; shift+=NumericUtils.PRECISION_STEP_DEFAULT) {
assertTrue("New token is available", stream.incrementToken());
assertEquals("Shift value wrong", shift, numericAtt.getShift());
bytesAtt.fillBytesRef();
assertEquals("Term is incorrectly encoded", ivalue & ~((1 << shift) - 1), NumericUtils.prefixCodedToInt(bytes));
assertEquals("Term raw value is incorrectly encoded", ((long) ivalue) & ~((1L << shift) - 1L), numericAtt.getRawValue());
assertEquals("Type incorrect", (shift == 0) ? NumericTokenStream.TOKEN_TYPE_FULL_PREC : NumericTokenStream.TOKEN_TYPE_LOWER_PREC, typeAtt.type());
}
assertFalse("More tokens available", stream.incrementToken());
stream.end();
stream.close();
}
示例10: indexedToReadable
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
@Override
public String indexedToReadable(String _indexedForm) {
final BytesRef indexedForm = new BytesRef(_indexedForm);
switch (type) {
case INTEGER:
return Integer.toString( NumericUtils.prefixCodedToInt(indexedForm) );
case FLOAT:
return Float.toString( NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(indexedForm)) );
case LONG:
return Long.toString( NumericUtils.prefixCodedToLong(indexedForm) );
case DOUBLE:
return Double.toString( NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(indexedForm)) );
case DATE:
return dateField.toExternal( new Date(NumericUtils.prefixCodedToLong(indexedForm)) );
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
}
}
示例11: toObject
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
@Override
public Object toObject(SchemaField sf, BytesRef term) {
switch (type) {
case INTEGER:
return NumericUtils.prefixCodedToInt(term);
case FLOAT:
return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(term));
case LONG:
return NumericUtils.prefixCodedToLong(term);
case DOUBLE:
return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(term));
case DATE:
return new Date(NumericUtils.prefixCodedToLong(term));
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
}
}
示例12: getDocument
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
/**
* Get the document for the user group
*
* @param user group - user group to create the document from
* @return - the created document
*/
private Document getDocument(IrUserGroup userGroup)
{
Document doc = new Document();
doc.add(new Field(ID,
NumericUtils.longToPrefixCoded(userGroup.getId()),
Field.Store.YES,
Field.Index.NOT_ANALYZED));
String name = userGroup.getName();
doc.add(new Field(NAME,
name,
Field.Store.YES,
Field.Index.ANALYZED));
if(userGroup.getDescription() != null && !userGroup.getDescription().equals(""))
{
doc.add(new Field(DESCRIPTION,
userGroup.getDescription(),
Field.Store.YES,
Field.Index.ANALYZED));
}
return doc;
}
示例13: getDocument
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
/**
* Get the document for the institutional collection
*
* @param institutional collection - institutional collection to create the document from
* @return - the created document
*/
private Document getDocument(InstitutionalCollection collection)
{
Document doc = new Document();
doc.add(new Field(ID,
NumericUtils.longToPrefixCoded(collection.getId()),
Field.Store.YES,
Field.Index.NOT_ANALYZED));
String name = collection.getName();
doc.add(new Field(NAME,
name,
Field.Store.YES,
Field.Index.ANALYZED));
if(collection.getDescription() != null && !collection.getDescription().equals(""))
{
doc.add(new Field(DESCRIPTION,
collection.getDescription(),
Field.Store.YES,
Field.Index.ANALYZED));
}
return doc;
}
示例14: getCollectionFilters
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
/**
* Set up the filters for collections - this is for searching within collections.
*
* @param collection - to search within
* @return - created filter
* @throws ParseException
*/
private List<Filter> getCollectionFilters(InstitutionalCollection collection) throws ParseException
{
List<Filter> filters = new LinkedList<Filter>();
//isolate the collection root
Term t = new Term("collection_root_id", NumericUtils.longToPrefixCoded(collection.getTreeRoot().getId()));
Query subQuery = new TermQuery( t );
filters.add(new QueryWrapperFilter(subQuery));
//isolate the range of children
subQuery = NumericRangeQuery.newLongRange("collection_left_value", collection.getLeftValue(), collection.getRightValue(), true, true);
filters.add(new QueryWrapperFilter(subQuery));
return filters;
}
示例15: makeValueToQuery
import org.apache.lucene.util.NumericUtils; //导入依赖的package包/类
/**
* Makes the value to query.
* <br/>The value to query is derived from NumericUtils.longToPrefixCoded(timestamp.getTime().
* @param value to input query value
* @param isLowerBoundary true if this is a lower boundary of a range query
* @param isUpperBoundary true if this is a upper boundary of a range query
* @return the value to query
* @throws DiscoveryException if the supplied value cannot be converted
*/
@Override
protected String makeValueToQuery(String value,
boolean isLowerBoundary,
boolean isUpperBoundary)
throws DiscoveryException {
try {
PropertyValueType valueType = PropertyValueType.TIMESTAMP;
Timestamp tsValue = (Timestamp)valueType.evaluate(
value,isLowerBoundary,isUpperBoundary);
if (tsValue == null) return null;
if (isLowerBoundary) {
LOGGER.finer("Lower boundary timestamp to query: "+tsValue);
} else if (isUpperBoundary) {
LOGGER.finer("Upper boundary timestamp to query: "+tsValue);
} else {
LOGGER.finer("Timestamp to query: "+tsValue);
}
return NumericUtils.longToPrefixCoded(tsValue.getTime());
} catch (IllegalArgumentException e) {
throw new DiscoveryException("Invalid date: "+value);
}
}