use of org.apache.lucene.document.DoublePoint in project lucene-solr by apache.
the class DistanceFacetsExample method index.
/** Build the example index. */
public void index() throws IOException {
IndexWriter writer = new IndexWriter(indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
// TODO: we could index in radians instead ... saves all the conversions in getBoundingBoxFilter
// Add documents with latitude/longitude location:
// we index these both as DoublePoints (for bounding box/ranges) and as NumericDocValuesFields (for scoring)
Document doc = new Document();
doc.add(new DoublePoint("latitude", 40.759011));
doc.add(new NumericDocValuesField("latitude", Double.doubleToRawLongBits(40.759011)));
doc.add(new DoublePoint("longitude", -73.9844722));
doc.add(new NumericDocValuesField("longitude", Double.doubleToRawLongBits(-73.9844722)));
writer.addDocument(doc);
doc = new Document();
doc.add(new DoublePoint("latitude", 40.718266));
doc.add(new NumericDocValuesField("latitude", Double.doubleToRawLongBits(40.718266)));
doc.add(new DoublePoint("longitude", -74.007819));
doc.add(new NumericDocValuesField("longitude", Double.doubleToRawLongBits(-74.007819)));
writer.addDocument(doc);
doc = new Document();
doc.add(new DoublePoint("latitude", 40.7051157));
doc.add(new NumericDocValuesField("latitude", Double.doubleToRawLongBits(40.7051157)));
doc.add(new DoublePoint("longitude", -74.0088305));
doc.add(new NumericDocValuesField("longitude", Double.doubleToRawLongBits(-74.0088305)));
writer.addDocument(doc);
// Open near-real-time searcher
searcher = new IndexSearcher(DirectoryReader.open(writer));
writer.close();
}
use of org.apache.lucene.document.DoublePoint in project carbondata by apache.
the class LuceneDataMapWriter method addField.
private boolean addField(Document doc, ColumnPage page, int rowId, Field.Store store) {
// get field name
String fieldName = page.getColumnSpec().getFieldName();
// get field type
DataType type = page.getDataType();
if (type == DataTypes.BYTE) {
// byte type , use int range to deal with byte, lucene has no byte type
byte value = page.getByte(rowId);
IntRangeField field = new IntRangeField(fieldName, new int[] { Byte.MIN_VALUE }, new int[] { Byte.MAX_VALUE });
field.setIntValue(value);
doc.add(field);
// if need store it , add StoredField
if (store == Field.Store.YES) {
doc.add(new StoredField(fieldName, (int) value));
}
} else if (type == DataTypes.SHORT) {
// short type , use int range to deal with short type, lucene has no short type
short value = page.getShort(rowId);
IntRangeField field = new IntRangeField(fieldName, new int[] { Short.MIN_VALUE }, new int[] { Short.MAX_VALUE });
field.setShortValue(value);
doc.add(field);
// if need store it , add StoredField
if (store == Field.Store.YES) {
doc.add(new StoredField(fieldName, (int) value));
}
} else if (type == DataTypes.INT) {
// int type , use int point to deal with int type
int value = page.getInt(rowId);
doc.add(new IntPoint(fieldName, new int[] { value }));
// if need store it , add StoredField
if (store == Field.Store.YES) {
doc.add(new StoredField(fieldName, value));
}
} else if (type == DataTypes.LONG) {
// long type , use long point to deal with long type
long value = page.getLong(rowId);
doc.add(new LongPoint(fieldName, new long[] { value }));
// if need store it , add StoredField
if (store == Field.Store.YES) {
doc.add(new StoredField(fieldName, value));
}
} else if (type == DataTypes.FLOAT) {
float value = page.getFloat(rowId);
doc.add(new FloatPoint(fieldName, new float[] { value }));
if (store == Field.Store.YES) {
doc.add(new FloatPoint(fieldName, value));
}
} else if (type == DataTypes.DOUBLE) {
double value = page.getDouble(rowId);
doc.add(new DoublePoint(fieldName, new double[] { value }));
if (store == Field.Store.YES) {
doc.add(new DoublePoint(fieldName, value));
}
} else if (type == DataTypes.STRING) {
byte[] value = page.getBytes(rowId);
// TODO: how to get string value
String strValue = null;
try {
strValue = new String(value, 2, value.length - 2, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
doc.add(new TextField(fieldName, strValue, store));
} else if (type == DataTypes.DATE) {
// TODO: how to get data value
} else if (type == DataTypes.TIMESTAMP) {
// TODO: how to get
} else if (type == DataTypes.BOOLEAN) {
boolean value = page.getBoolean(rowId);
IntRangeField field = new IntRangeField(fieldName, new int[] { 0 }, new int[] { 1 });
field.setIntValue(value ? 1 : 0);
doc.add(field);
if (store == Field.Store.YES) {
doc.add(new StoredField(fieldName, value ? 1 : 0));
}
} else {
LOGGER.error("unsupport data type " + type);
throw new RuntimeException("unsupported data type " + type);
}
return true;
}
use of org.apache.lucene.document.DoublePoint in project cxf by apache.
the class TikaLuceneContentExtractor method addField.
private static void addField(final Document document, final LuceneDocumentMetadata documentMetadata, final String name, final String value) {
final Class<?> type = documentMetadata.getFieldType(name);
final ParamConverterProvider provider = documentMetadata.getFieldTypeConverter();
if (type != null) {
if (Number.class.isAssignableFrom(type)) {
if (Double.class.isAssignableFrom(type)) {
Double number = ParamConverterUtils.getValue(Double.class, provider, value);
document.add(new DoublePoint(name, number));
document.add(new StoredField(name, number));
} else if (Float.class.isAssignableFrom(type)) {
Float number = ParamConverterUtils.getValue(Float.class, provider, value);
document.add(new FloatPoint(name, number));
document.add(new StoredField(name, number));
} else if (Long.class.isAssignableFrom(type)) {
Long number = ParamConverterUtils.getValue(Long.class, provider, value);
document.add(new LongPoint(name, number));
document.add(new StoredField(name, number));
} else if (Integer.class.isAssignableFrom(type) || Byte.class.isAssignableFrom(type)) {
Integer number = ParamConverterUtils.getValue(Integer.class, provider, value);
document.add(new IntPoint(name, number));
document.add(new StoredField(name, number));
} else {
document.add(new StringField(name, value, Store.YES));
}
return;
} else if (Date.class.isAssignableFrom(type)) {
final Date date = ParamConverterUtils.getValue(Date.class, provider, value);
final Field field;
if (date != null) {
field = new StringField(name, ParamConverterUtils.getString(Date.class, provider, date), Store.YES);
} else {
field = new StringField(name, value, Store.YES);
}
document.add(field);
return;
}
}
document.add(new StringField(name, value, Store.YES));
}
use of org.apache.lucene.document.DoublePoint in project janusgraph by JanusGraph.
the class LuceneIndex method buildIndexFields.
private List<IndexableField> buildIndexFields(final Document doc, final KeyInformation.StoreRetriever information) {
List<IndexableField> fields = new ArrayList<>();
for (IndexableField field : doc.getFields()) {
String fieldName = field.name();
if (fieldName.equals(DOCID)) {
continue;
}
KeyInformation ki = information.get(getOrigFieldName(fieldName));
boolean isPossibleSortIndex = ki.getCardinality() == Cardinality.SINGLE;
Class<?> dataType = ki.getDataType();
if (AttributeUtils.isWholeNumber(dataType)) {
long value = field.numericValue().longValue();
fields.add(new LongPoint(fieldName, value));
if (isPossibleSortIndex) {
fields.add(new NumericDocValuesField(fieldName, value));
}
} else if (AttributeUtils.isDecimal(dataType)) {
double value = field.numericValue().doubleValue();
fields.add(new DoublePoint(fieldName, value));
if (isPossibleSortIndex) {
fields.add(new DoubleDocValuesField(fieldName, value));
}
} else if (AttributeUtils.isString(dataType)) {
final Mapping mapping = Mapping.getMapping(ki);
if ((mapping == Mapping.STRING || mapping == Mapping.TEXTSTRING) && isPossibleSortIndex) {
fields.add(new SortedDocValuesField(fieldName, new BytesRef(field.stringValue())));
}
} else if (AttributeUtils.isGeo(dataType)) {
if (log.isTraceEnabled())
log.trace("Updating geo-indexes for key {}", fieldName);
Shape shape;
try {
shape = Geoshape.fromWkt(field.stringValue().substring(GEOID.length())).getShape();
} catch (java.text.ParseException e) {
throw new IllegalArgumentException("Geoshape was not parsable", e);
}
final SpatialStrategy spatialStrategy = getSpatialStrategy(fieldName, ki);
Collections.addAll(fields, spatialStrategy.createIndexableFields(shape));
} else if (dataType.equals(Date.class) || dataType.equals(Instant.class)) {
long value = field.numericValue().longValue();
fields.add(new LongPoint(fieldName, value));
if (isPossibleSortIndex) {
fields.add(new NumericDocValuesField(fieldName, value));
}
} else if (dataType.equals(Boolean.class)) {
fields.add(new IntPoint(fieldName, field.numericValue().intValue() == 1 ? 1 : 0));
if (isPossibleSortIndex) {
fields.add(new NumericDocValuesField(fieldName, field.numericValue().intValue()));
}
}
}
return fields;
}
Aggregations