List of usage examples for org.springframework.data.mongodb.core.index GeospatialIndex GeospatialIndex
public GeospatialIndex(String field)
From source file:example.springdata.mongodb.fluent.ApplicationConfiguration.java
@Bean CommandLineRunner init(MongoTemplate template) { return (args) -> { if (template.collectionExists(COLLECTION)) { template.dropCollection(COLLECTION); }/*from w w w . j av a2s . com*/ GeospatialIndex index = new GeospatialIndex("homePlanet.coordinates") // .typed(GeoSpatialIndexType.GEO_2DSPHERE) // .named("planet-coordinate-idx"); template.createCollection(COLLECTION); template.indexOps(SWCharacter.class).ensureIndex(index); Planet alderaan = new Planet("alderaan", new Point(-73.9667, 40.78)); Planet stewjon = new Planet("stewjon", new Point(-73.9836, 40.7538)); Planet tatooine = new Planet("tatooine", new Point(-73.9928, 40.7193)); Jedi anakin = new Jedi("anakin", "skywalker"); anakin.setHomePlanet(tatooine); Jedi luke = new Jedi("luke", "skywalker"); luke.setHomePlanet(tatooine); Jedi leia = new Jedi("leia", "organa"); leia.setHomePlanet(alderaan); Jedi obiWan = new Jedi("obi-wan", "kenobi"); obiWan.setHomePlanet(stewjon); Human han = new Human("han", "solo"); template.save(anakin, COLLECTION); template.save(luke, COLLECTION); template.save(leia, COLLECTION); template.save(obiWan, COLLECTION); template.save(han, COLLECTION); }; }
From source file:com.traffitruck.service.MongoDAO.java
@Autowired public MongoDAO(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; // Creates an index on the specified field if the index does not already exist mongoTemplate.indexOps(Load.class) .ensureIndex(new GeospatialIndex("sourceLocation").typed(GeoSpatialIndexType.GEO_2DSPHERE)); mongoTemplate.indexOps(Load.class) .ensureIndex(new GeospatialIndex("destinationLocation").typed(GeoSpatialIndexType.GEO_2DSPHERE)); mongoTemplate.indexOps(Truck.class).ensureIndex(new Index("licensePlateNumber", Direction.ASC).unique()); mongoTemplate.indexOps(LoadsUser.class).ensureIndex(new Index("username", Direction.ASC).unique()); mongoTemplate.indexOps(LoadsUser.class).ensureIndex(new Index("email", Direction.ASC).unique()); }
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 .j a v a 2 s. co 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.openinfinity.tagcloud.domain.repository.TargetRepositoryMongoDBImpl.java
@Override public void dropCollection() { mongoTemplate.indexOps(Target.class).dropAllIndexes(); mongoTemplate.dropCollection(Target.class); mongoTemplate.indexOps(Target.class).ensureIndex(new GeospatialIndex("location")); }
From source file:org.springframework.data.mongodb.core.geo.GeoSpatialTests.java
@Before public void setUp() throws Exception { template.setWriteConcern(WriteConcern.FSYNC_SAFE); template.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location")); indexCreated();//from ww w . j a va 2 s. c o m addVenues(); }
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."); }/*from w w w. ja va 2 s.com*/ // 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); } }