Example usage for org.springframework.data.mongodb.core.index GeospatialIndex getIndexOptions

List of usage examples for org.springframework.data.mongodb.core.index GeospatialIndex getIndexOptions

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.index GeospatialIndex getIndexOptions.

Prototype

@Nullable
    public Document getIndexOptions() 

Source Link

Usage

From source file:example.springdata.mongodb.customer.CustomerRepositoryIntegrationTest.java

/**
 * Test case to show the usage of the geo-spatial APIs to lookup people within a given distance of a reference point.
 *//*from w  w  w.  ja  v a  2 s .  c  o m*/
@Test
public void exposesGeoSpatialFunctionality() {

    GeospatialIndex indexDefinition = new GeospatialIndex("address.location");
    indexDefinition.getIndexOptions().put("min", -180);
    indexDefinition.getIndexOptions().put("max", 180);

    operations.indexOps(Customer.class).ensureIndex(indexDefinition);

    Customer ollie = new Customer("Oliver", "Gierke");
    ollie.setAddress(new Address(new Point(52.52548, 13.41477)));
    ollie = repository.save(ollie);

    Point referenceLocation = new Point(52.51790, 13.41239);
    Distance oneKilometer = new Distance(1, Metrics.KILOMETERS);

    GeoResults<Customer> result = repository.findByAddressLocationNear(referenceLocation, oneKilometer);

    assertThat(result.getContent(), hasSize(1));

    Distance distanceToFirstStore = result.getContent().get(0).getDistance();
    assertThat(distanceToFirstStore.getMetric(), is(Metrics.KILOMETERS));
    assertThat(distanceToFirstStore.getValue(), closeTo(0.862, 0.001));
}

From source file:org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.java

protected void checkForIndexes(final MongoPersistentEntity<?> entity) {
    final Class<?> type = entity.getType();
    if (!classesSeen.containsKey(type)) {
        if (log.isDebugEnabled()) {
            log.debug("Analyzing class " + type + " for index information.");
        }//w w w .j a  v a  2  s.c  om

        // Make sure indexes get created
        if (type.isAnnotationPresent(CompoundIndexes.class)) {
            CompoundIndexes indexes = type.getAnnotation(CompoundIndexes.class);
            for (CompoundIndex index : indexes.value()) {

                String indexColl = StringUtils.hasText(index.collection()) ? index.collection()
                        : entity.getCollection();
                DBObject definition = (DBObject) JSON.parse(index.def());

                ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(),
                        index.sparse());

                if (log.isDebugEnabled()) {
                    log.debug("Created compound index " + index);
                }
            }
        }

        entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
            public void doWithPersistentProperty(MongoPersistentProperty persistentProperty) {

                Field field = persistentProperty.getField();

                if (field.isAnnotationPresent(Indexed.class)) {

                    Indexed index = field.getAnnotation(Indexed.class);
                    String name = index.name();

                    if (!StringUtils.hasText(name)) {
                        name = persistentProperty.getFieldName();
                    } else {
                        if (!name.equals(field.getName()) && index.unique() && !index.sparse()) {
                            // Names don't match, and sparse is not true. This situation will generate an error on the server.
                            if (log.isWarnEnabled()) {
                                log.warn("The index name " + name + " doesn't match this property name: "
                                        + field.getName()
                                        + ". Setting sparse=true on this index will prevent errors when inserting documents.");
                            }
                        }
                    }

                    String collection = StringUtils.hasText(index.collection()) ? index.collection()
                            : entity.getCollection();
                    int direction = index.direction() == IndexDirection.ASCENDING ? 1 : -1;
                    DBObject definition = new BasicDBObject(persistentProperty.getFieldName(), direction);

                    ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse());

                    if (log.isDebugEnabled()) {
                        log.debug("Created property index " + index);
                    }

                } else if (field.isAnnotationPresent(GeoSpatialIndexed.class)) {

                    GeoSpatialIndexed index = field.getAnnotation(GeoSpatialIndexed.class);

                    GeospatialIndex indexObject = new GeospatialIndex(persistentProperty.getFieldName());
                    indexObject.withMin(index.min()).withMax(index.max());
                    indexObject.named(StringUtils.hasText(index.name()) ? index.name() : field.getName());

                    String collection = StringUtils.hasText(index.collection()) ? index.collection()
                            : entity.getCollection();
                    mongoDbFactory.getDb().getCollection(collection).ensureIndex(indexObject.getIndexKeys(),
                            indexObject.getIndexOptions());

                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Created %s for entity %s in collection %s! ", indexObject,
                                entity.getType(), collection));
                    }
                }
            }
        });

        classesSeen.put(type, true);
    }
}