@Override public <T> void ensureIndexes(final Class<T> clazz, final boolean background) { indexHelper.createIndex(getMongoCollection(clazz), mapper.getMappedClass(clazz), background); }
@Override public <T> void ensureIndexes(final String collection, final Class<T> clazz, final boolean background) { indexHelper.createIndex(getMongoCollection(collection, clazz, null), mapper.getMappedClass(clazz), background); }
void createIndex(final MongoCollection collection, final MappedClass mc, final boolean background) { if (!mc.isInterface() && !mc.isAbstract()) { for (Index index : collectIndexes(mc, Collections.<MappedClass>emptyList())) { createIndex(collection, mc, index, background); } } }
@Override public void ensureIndexes(final boolean background) { for (final MappedClass mc : mapper.getMappedClasses()) { indexHelper.createIndex(getMongoCollection(mc.getClazz()), mc, background); } }
@Override @Deprecated public <T> void ensureIndex(final String collection, final Class<T> clazz, final String name, final String fields, final boolean unique, final boolean dropDupsOnCreate) { if (dropDupsOnCreate) { LOG.warn("Support for dropDups has been removed from the server. Please remove this setting."); } indexHelper.createIndex(getMongoCollection(collection, clazz, null), getMapper().getMappedClass(clazz), new IndexBuilder() .fields(fields) .name(name) .unique(unique), false); }
@Test(expected = MappingException.class) public void weightsOnNonTextIndex() { MongoCollection<Document> indexes = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); IndexBuilder index = new IndexBuilder() .fields(new FieldBuilder() .value("name") .weight(10)); indexHelper.createIndex(indexes, mappedClass, index, false); }
@Test public void indexedPartialFilters() { MongoCollection<Document> collection = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); Indexed indexed = new IndexedBuilder() .options(new IndexOptionsBuilder() .partialFilter("{ name : { $gt : 13 } }")); indexHelper.createIndex(collection, mappedClass, indexHelper.convert(indexed, "text"), false); findPartialIndex(BasicDBObject.parse(indexed.options().partialFilter())); }
@Test public void wildcardTextIndex() { MongoCollection<Document> indexes = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); IndexBuilder index = new IndexBuilder() .fields(new FieldBuilder() .value("$**") .type(IndexType.TEXT)); indexHelper.createIndex(indexes, mappedClass, index, false); List<DBObject> wildcard = getDb().getCollection("indexes").getIndexInfo(); boolean found = false; for (DBObject dbObject : wildcard) { found |= dbObject.get("name").equals("$**_text"); } assertTrue("Should have found the wildcard index", found); }
Mapper mapper = getMorphia().getMapper(); indexHelper.createIndex(collection, mapper.getMappedClass(IndexedClass.class), false); List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class) .getIndexInfo(); indexHelper.createIndex(collection, mapper.getMappedClass(AbstractParent.class), false); indexInfo = getDs().getCollection(AbstractParent.class).getIndexInfo(); assertTrue("Shouldn't find any indexes: " + indexInfo, indexInfo.isEmpty());
@Test public void textPartialFilters() { MongoCollection<Document> collection = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); Text text = new TextBuilder() .value(4) .options(new IndexOptionsBuilder() .partialFilter("{ name : { $gt : 13 } }")); indexHelper.createIndex(collection, mappedClass, indexHelper.convert(text, "text"), false); findPartialIndex(BasicDBObject.parse(text.options().partialFilter())); }
@Test public void indexPartialFilters() { MongoCollection<Document> collection = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); Index index = new IndexBuilder() .fields(new FieldBuilder().value("text")) .options(new IndexOptionsBuilder() .partialFilter("{ name : { $gt : 13 } }")); indexHelper.createIndex(collection, mappedClass, index, false); findPartialIndex(BasicDBObject.parse(index.options().partialFilter())); } @Test
@Test public void oldIndexForm() { MongoCollection<Document> indexes = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); indexes.drop(); Index index = new IndexBuilder() .name("index_name") .background(true) .disableValidation(true) .expireAfterSeconds(42) .sparse(true) .unique(true) .value("indexName, -text"); indexHelper.createIndex(indexes, mappedClass, index, false); List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class) .getIndexInfo(); for (DBObject dbObject : indexInfo) { if (dbObject.get("name").equals("index_indexName")) { checkIndex(dbObject); } } }
.type(IndexType.DESC)) .options(indexOptions()); indexHelper.createIndex(indexes, mappedClass, index, false); List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class) .getIndexInfo();