List of usage examples for org.apache.lucene.spatial SpatialStrategy createIndexableFields
public abstract Field[] createIndexableFields(Shape shape);
From source file:org.codice.ddf.spatial.geocoding.index.GeoNamesLuceneIndexer.java
License:Open Source License
private void addDocument(final IndexWriter indexWriter, final GeoEntry geoEntry, final SpatialStrategy strategy) throws IOException { final Document document = new Document(); document.add(new TextField(GeoNamesLuceneConstants.NAME_FIELD, geoEntry.getName(), Field.Store.YES)); document.add(new StoredField(GeoNamesLuceneConstants.LATITUDE_FIELD, geoEntry.getLatitude())); document.add(new StoredField(GeoNamesLuceneConstants.LONGITUDE_FIELD, geoEntry.getLongitude())); document.add(new StringField(GeoNamesLuceneConstants.FEATURE_CODE_FIELD, geoEntry.getFeatureCode(), Field.Store.YES));// ww w . j a v a 2 s . co m document.add(new TextField(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD, geoEntry.getCountryCode(), Field.Store.YES)); document.add(new StoredField(GeoNamesLuceneConstants.POPULATION_FIELD, geoEntry.getPopulation())); // This DocValues field is used for sorting by population. document.add(new NumericDocValuesField(GeoNamesLuceneConstants.POPULATION_DOCVALUES_FIELD, geoEntry.getPopulation())); document.add(new TextField(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD, geoEntry.getAlternateNames(), Field.Store.NO)); // Add each entry's spatial information for fast spatial filtering. final Shape point = SPATIAL_CONTEXT.getShapeFactory().pointXY(geoEntry.getLongitude(), geoEntry.getLatitude()); for (IndexableField field : strategy.createIndexableFields(point)) { document.add(field); } final float boost = calculateBoost(geoEntry); document.add(new FloatDocValuesField(GeoNamesLuceneConstants.BOOST_FIELD, boost)); indexWriter.addDocument(document); }
From source file:org.eclipse.rdf4j.sail.lucene.LuceneDocument.java
License:Open Source License
@Override public void addGeoProperty(String field, String value) { LuceneIndex.addStoredOnlyPredicateField(field, value, doc); try {/* ww w. ja va2 s. c om*/ SpatialStrategy geoStrategy = geoStrategyMapper.apply(field); Shape shape = geoStrategy.getSpatialContext().readShapeFromWkt(value); for (IndexableField f : geoStrategy.createIndexableFields(shape)) { doc.add(f); } } catch (ParseException e) { // ignore } }
From source file:org.janusgraph.diskstorage.lucene.LuceneIndex.java
License:Apache License
private void addToDocument(String store, String docID, Document doc, List<IndexEntry> content, Map<String, Shape> geoFields, KeyInformation.IndexRetriever information) { Preconditions.checkNotNull(doc);/*w w w . j a v a 2 s. c om*/ for (final IndexEntry e : content) { Preconditions.checkArgument(!e.hasMetaData(), "Lucene index does not support indexing meta data: %s", e); if (log.isTraceEnabled()) log.trace("Adding field [{}] on document [{}]", e.field, docID); if (doc.getField(e.field) != null) doc.removeFields(e.field); if (e.value instanceof Number) { final Field field; final Field sortField; if (AttributeUtil.isWholeNumber((Number) e.value)) { field = new LongPoint(e.field, ((Number) e.value).longValue()); sortField = new NumericDocValuesField(e.field, ((Number) e.value).longValue()); } else { //double or float field = new DoublePoint(e.field, ((Number) e.value).doubleValue()); sortField = new DoubleDocValuesField(e.field, ((Number) e.value).doubleValue()); } doc.add(field); doc.add(sortField); } else if (AttributeUtil.isString(e.value)) { final String str = (String) e.value; final Mapping mapping = Mapping.getMapping(store, e.field, information); final Field field; final Field sortField; switch (mapping) { case DEFAULT: case TEXT: // lowering the case for case insensitive text search field = new TextField(e.field, str.toLowerCase(), Field.Store.YES); sortField = null; break; case STRING: // if this field uses a custom analyzer, it must be stored as a TextField // (or the analyzer, even if it is a KeywordAnalyzer won't be used) field = new TextField(e.field, str, Field.Store.YES); sortField = new SortedDocValuesField(e.field, new BytesRef(str)); break; default: throw new IllegalArgumentException("Illegal mapping specified: " + mapping); } doc.add(field); if (sortField != null) { doc.add(sortField); } } else if (e.value instanceof Geoshape) { final Shape shape = ((Geoshape) e.value).getShape(); geoFields.put(e.field, shape); doc.add(new StoredField(e.field, GEOID + e.value.toString())); } else if (e.value instanceof Date) { doc.add(new LongPoint(e.field, (((Date) e.value).getTime()))); doc.add(new NumericDocValuesField(e.field, (((Date) e.value).getTime()))); } else if (e.value instanceof Instant) { doc.add(new LongPoint(e.field, (((Instant) e.value).toEpochMilli()))); doc.add(new NumericDocValuesField(e.field, (((Instant) e.value).toEpochMilli()))); } else if (e.value instanceof Boolean) { doc.add(new IntPoint(e.field, ((Boolean) e.value) ? 1 : 0)); } else if (e.value instanceof UUID) { //Solr stores UUIDs as strings, we we do the same. final Field field = new StringField(e.field, e.value.toString(), Field.Store.YES); doc.add(field); } else { throw new IllegalArgumentException("Unsupported type: " + e.value); } } for (final Map.Entry<String, Shape> geo : geoFields.entrySet()) { if (log.isTraceEnabled()) log.trace("Updating geo-indexes for key {}", geo.getKey()); final KeyInformation ki = information.get(store, geo.getKey()); final SpatialStrategy spatialStrategy = getSpatialStrategy(geo.getKey(), ki); for (final IndexableField f : spatialStrategy.createIndexableFields(geo.getValue())) { if (doc.getField(f.name()) != null) { doc.removeFields(f.name()); } doc.add(f); if (spatialStrategy instanceof PointVectorStrategy) { doc.add(new DoubleDocValuesField(f.name(), f.numericValue() == null ? null : f.numericValue().doubleValue())); } } } }
From source file:org.openspaces.spatial.spi.LuceneSpatialQueryExtensionManager.java
License:Open Source License
protected Document createDocumentIfNeeded(LuceneSpatialTypeIndex luceneHolder, SpaceServerEntry entry) { Document doc = null;//from ww w.j av a 2s. co m for (String path : luceneHolder.getQueryExtensionInfo().getPaths()) { final Object fieldValue = entry.getPathValue(path); if (fieldValue instanceof Shape) { final SpatialStrategy strategy = _luceneConfiguration.getStrategy(path); final Field[] fields = strategy.createIndexableFields(toShape(fieldValue)); if (doc == null) doc = new Document(); for (Field field : fields) doc.add(field); } } if (doc != null) { //cater for uid & version doc.add(new Field(XAP_ID, entry.getUid(), XAP_ID_TYPE)); doc.add(new Field(XAP_ID_VERSION, concat(entry.getUid(), entry.getVersion()), XAP_ID_VERSION_TYPE)); } return doc; }