Search in sources :

Example 1 with Index

use of dev.morphia.annotations.Index in project morphia by mongodb.

the class IndexHelperTest method calculateBadKeys.

@Test
public void calculateBadKeys() {
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    Index index = indexBuilder().fields(fieldBuilder().value("texting").type(IndexType.TEXT).weight(1).build(), fieldBuilder().value("nest").type(IndexType.DESC).build()).build();
    try {
        getIndexHelper().calculateKeys(model, index);
        fail("Validation should have failed on the bad key");
    } catch (MappingException e) {
    // all good
    }
    index = indexBuilder().fields(fieldBuilder().value("texting").type(IndexType.TEXT).weight(1).build(), fieldBuilder().value("nest").type(IndexType.DESC).build()).options(indexOptionsBuilder().disableValidation(true).build()).build();
    getIndexHelper().calculateKeys(model, index);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Index(dev.morphia.annotations.Index) MappingException(dev.morphia.mapping.MappingException) Test(org.testng.annotations.Test)

Example 2 with Index

use of dev.morphia.annotations.Index in project morphia by mongodb.

the class IndexHelperTest method normalizeIndexed.

@Test
public void normalizeIndexed() {
    Indexed indexed = indexedBuilder().value(IndexDirection.DESC).options(indexOptionsBuilder().name("index_name").background(true).expireAfterSeconds(42).sparse(true).unique(true).build()).build();
    Index converted = getIndexHelper().convert(indexed, "oldstyle");
    assertEquals(converted.options().name(), "index_name");
    assertTrue(converted.options().background());
    assertTrue(converted.options().sparse());
    assertTrue(converted.options().unique());
    assertEquals(fieldBuilder().value("oldstyle").type(IndexType.DESC).build(), converted.fields()[0]);
}
Also used : Index(dev.morphia.annotations.Index) Indexed(dev.morphia.annotations.Indexed) Test(org.testng.annotations.Test)

Example 3 with Index

use of dev.morphia.annotations.Index in project morphia by mongodb.

the class IndexHelperTest method convertTextIndex.

@Test
public void convertTextIndex() {
    Text text = textBuilder().value(4).options(indexOptionsBuilder().name("index_name").background(true).expireAfterSeconds(42).sparse(true).unique(true).build()).build();
    Index index = getIndexHelper().convert(text, "search_field");
    assertEquals(index.options().name(), "index_name");
    assertTrue(index.options().background());
    assertTrue(index.options().sparse());
    assertTrue(index.options().unique());
    assertEquals(fieldBuilder().value("search_field").type(IndexType.TEXT).weight(4).build(), index.fields()[0]);
}
Also used : Text(dev.morphia.annotations.Text) Index(dev.morphia.annotations.Index) Test(org.testng.annotations.Test)

Example 4 with Index

use of dev.morphia.annotations.Index in project morphia by mongodb.

the class IndexHelperTest method index.

@Test
public void index() {
    MongoCollection<Document> collection = getDatabase().getCollection("indexes");
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    IndexOptions options = indexOptionsBuilder().name("index_name").background(true).collation(collation().build()).disableValidation(true).language("en").languageOverride("de").sparse(true).unique(true).build();
    Index index = indexBuilder().fields(fieldBuilder().value("indexName").build(), fieldBuilder().value("text").type(IndexType.DESC).build()).options(options).build();
    getIndexHelper().createIndex(collection, model, index);
    List<Document> indexInfo = getIndexInfo(IndexedClass.class);
    for (Document document : indexInfo) {
        if (document.get("name").equals("indexName")) {
            checkIndex(document);
            assertEquals(document.get("default_language"), "en");
            assertEquals(document.get("language_override"), "de");
            assertEquals(document.get("collation"), new Document().append("locale", "en").append("caseLevel", true).append("caseFirst", "upper").append("strength", 5).append("numericOrdering", true).append("alternate", "shifted").append("maxVariable", "space").append("backwards", true).append("normalization", true).append("version", "57.1"));
        }
    }
}
Also used : IndexOptions(dev.morphia.annotations.IndexOptions) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Index(dev.morphia.annotations.Index) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 5 with Index

use of dev.morphia.annotations.Index in project morphia by mongodb.

the class IndexHelperTest method weightsOnNonTextIndex.

@Test(expectedExceptions = MappingException.class)
public void weightsOnNonTextIndex() {
    MongoCollection<Document> indexes = getDatabase().getCollection("indexes");
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    Index index = indexBuilder().fields(fieldBuilder().value("name").weight(10).build()).build();
    getIndexHelper().createIndex(indexes, model, index);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Index(dev.morphia.annotations.Index) Document(org.bson.Document) Test(org.testng.annotations.Test)

Aggregations

Index (dev.morphia.annotations.Index)7 Test (org.testng.annotations.Test)7 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)5 Document (org.bson.Document)4 IndexOptions (dev.morphia.annotations.IndexOptions)1 Indexed (dev.morphia.annotations.Indexed)1 Text (dev.morphia.annotations.Text)1 MappingException (dev.morphia.mapping.MappingException)1