Example usage for org.springframework.data.mongodb.core.index Index on

List of usage examples for org.springframework.data.mongodb.core.index Index on

Introduction

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

Prototype

public Index on(String key, Direction direction) 

Source Link

Usage

From source file:org.springframework.data.mongodb.repository.support.IndexEnsuringQueryCreationListener.java

public void onCreation(PartTreeMongoQuery query) {

    PartTree tree = query.getTree();/* www.  j  av a  2  s.c  o  m*/
    Index index = new Index();
    index.named(query.getQueryMethod().getName());
    Sort sort = tree.getSort();

    for (Part part : tree.getParts()) {
        if (GEOSPATIAL_TYPES.contains(part.getType())) {
            return;
        }
        String property = part.getProperty().toDotPath();
        Order order = toOrder(sort, property);
        index.on(property, order);
    }

    // Add fixed sorting criteria to index
    if (sort != null) {
        for (Sort.Order order : sort) {
            index.on(order.getProperty(), QueryUtils.toOrder(order));
        }
    }

    MongoEntityInformation<?, ?> metadata = query.getQueryMethod().getEntityInformation();
    operations.indexOps(metadata.getCollectionName()).ensureIndex(index);
    LOG.debug(String.format("Created %s!", index));
}