Example usage for org.springframework.data.mongodb.core MongoTemplate indexOps

List of usage examples for org.springframework.data.mongodb.core MongoTemplate indexOps

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoTemplate indexOps.

Prototype

public IndexOperations indexOps(Class<?> entityClass) 

Source Link

Usage

From source file:com.harpatec.examples.Main.java

/**
 * Load the Spring Integration Application Context
 * //  ww  w. j a  va  2  s  . c o m
 * @param args - command line arguments
 * @throws InterruptedException
 * @throws IOException
 * @throws JsonMappingException
 * @throws JsonGenerationException
 */
public static void main(final String... args)
        throws InterruptedException, JsonGenerationException, JsonMappingException, IOException {

    final AbstractApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:META-INF/spring/integration/*-context.xml");

    context.registerShutdownHook();

    LOGGER.debug("Dropping the collection of MessageRecords");
    MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
    mongoTemplate.dropCollection(MessageRecord.class);
    mongoTemplate.indexOps(MessageRecord.class).ensureIndex(new Index().on("key", Order.ASCENDING).unique());
    mongoTemplate.indexOps(MessageRecord.class).ensureIndex(new Index().on("completionTime", Order.ASCENDING));

    RabbitTemplate inboundTemplate = (RabbitTemplate) context.getBean("amqpTemplateInbound");
    Map<String, Object> messageMap = new HashMap<String, Object>();
    messageMap.put("count", "4");

    LOGGER.debug("Submitting first message which should pass DuplicateMessageFilter ok.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(5 * 1000);
    LOGGER.debug("Submitting a duplicate message which should get caught by the DuplicateMessageFilter.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(5 * 1000);
    messageMap.put("count", "0");
    LOGGER.debug("Submitting a message which will not go all the way through the message flow.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(5 * 1000);
    messageMap.put("count", "1");
    messageMap.put("fail", "true");
    LOGGER.debug("Submitting a message which should signal that an Exception should be thrown.");
    submitMessage(inboundTemplate, messageMap);

    Thread.sleep(6 * 60 * 1000);

    System.exit(0);

}

From source file:example.springdata.mongodb.fluent.ApplicationConfiguration.java

@Bean
CommandLineRunner init(MongoTemplate template) {

    return (args) -> {

        if (template.collectionExists(COLLECTION)) {
            template.dropCollection(COLLECTION);
        }/* w  ww . j av a 2  s. c o  m*/

        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:com.seajas.search.profiler.service.repository.RepositoryService.java

/**
 * Default constructor.//from w ww .  jav  a 2s . c om
 *
 * @param dbFactory
 */
@Autowired
public RepositoryService(final MongoDbFactory dbFactory, final MongoTemplate mongoTemplate) {
    this.gridFs = new GridFS(dbFactory.getDb());

    // Initialize the indexes only if there aren't too many documents in the store yet

    Long currentCount = mongoTemplate.count(new Query(), CompositeEntry.class);

    // Determine whether the given indexes have been created, and initialize them if necessary

    if (currentCount <= MAX_INDEX_CREATION_COUNT)
        for (String index : INDEXES)
            mongoTemplate.indexOps(CompositeEntry.class).ensureIndex(new Index().on(index, Order.ASCENDING));

    this.mongoTemplate = mongoTemplate;
}