Search in sources :

Example 6 with IndexDefinition

use of org.springframework.data.mongodb.core.index.IndexDefinition in project spring-data-mongodb by spring-projects.

the class TextQueryTests method setUp.

@Before
public void setUp() {
    IndexOperations indexOps = template.indexOps(FullTextDoc.class);
    indexOps.dropAllIndexes();
    indexOps.ensureIndex(new IndexDefinition() {

        @Override
        public Document getIndexOptions() {
            Document options = new Document();
            options.put("weights", weights());
            options.put("name", "TextQueryTests_TextIndex");
            options.put("language_override", "lang");
            options.put("default_language", "english");
            return options;
        }

        @Override
        public Document getIndexKeys() {
            Document keys = new Document();
            keys.put("headline", "text");
            keys.put("subheadline", "text");
            keys.put("body", "text");
            return keys;
        }

        private Document weights() {
            Document weights = new Document();
            weights.put("headline", 10);
            weights.put("subheadline", 5);
            weights.put("body", 1);
            return weights;
        }
    });
}
Also used : IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) Document(org.bson.Document) Before(org.junit.Before)

Example 7 with IndexDefinition

use of org.springframework.data.mongodb.core.index.IndexDefinition in project spring-data-mongodb by spring-projects.

the class DefaultIndexOperationsIntegrationTests method shouldApplyPartialDBOFilterCorrectly.

// DATAMONGO-1467
@Test
public void shouldApplyPartialDBOFilterCorrectly() {
    assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO), is(true));
    IndexDefinition id = new Index().named("partial-with-dbo").on("k3y", Direction.ASC).partial(of(new org.bson.Document("qty", new org.bson.Document("$gte", 10))));
    indexOps.ensureIndex(id);
    IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "partial-with-dbo");
    assertThat(info.getPartialFilterExpression()).isEqualTo("{ \"qty\" : { \"$gte\" : 10 } }");
}
Also used : Document(org.bson.Document) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Document(org.bson.Document) Test(org.junit.Test)

Example 8 with IndexDefinition

use of org.springframework.data.mongodb.core.index.IndexDefinition in project spring-data-mongodb by spring-projects.

the class DefaultIndexOperationsIntegrationTests method shouldFavorExplicitMappingHintViaClass.

// DATAMONGO-1467
@Test
public void shouldFavorExplicitMappingHintViaClass() {
    assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO), is(true));
    IndexDefinition id = new Index().named("partial-with-inheritance").on("k3y", Direction.ASC).partial(of(where("age").gte(10)));
    indexOps = new DefaultIndexOperations(template.getMongoDbFactory(), this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class), new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id);
    IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "partial-with-inheritance");
    assertThat(info.getPartialFilterExpression()).isEqualTo("{ \"a_g_e\" : { \"$gte\" : 10 } }");
}
Also used : IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Test(org.junit.Test)

Example 9 with IndexDefinition

use of org.springframework.data.mongodb.core.index.IndexDefinition in project spring-data-mongodb by spring-projects.

the class DefaultIndexOperationsIntegrationTests method shouldCreateIndexWithCollationCorrectly.

// DATAMONGO-1518
@Test
public void shouldCreateIndexWithCollationCorrectly() {
    assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR), is(true));
    IndexDefinition id = new Index().named("with-collation").on("xyz", Direction.ASC).collation(Collation.of("de_AT").caseFirst(CaseFirst.off()));
    new DefaultIndexOperations(template.getMongoDbFactory(), this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class), new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id);
    Document expected = // 
    new Document("locale", "de_AT").append("caseLevel", // 
    false).append("caseFirst", // 
    "off").append("strength", // 
    3).append("numericOrdering", // 
    false).append("alternate", // 
    "non-ignorable").append("maxVariable", // 
    "punct").append("normalization", // 
    false).append("backwards", false);
    IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "with-collation");
    assertThat(info.getCollation()).isPresent();
    // version is set by MongoDB server - we remove it to avoid errors when upgrading server version.
    Document result = info.getCollation().get();
    result.remove("version");
    assertThat(result).isEqualTo(expected);
}
Also used : IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Document(org.bson.Document) Test(org.junit.Test)

Example 10 with IndexDefinition

use of org.springframework.data.mongodb.core.index.IndexDefinition in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsTests method shouldApplyPartialDBOFilterCorrectly.

// DATAMONGO-1682
@Test
public void shouldApplyPartialDBOFilterCorrectly() {
    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
    IndexDefinition id = new Index().named("partial-with-dbo").on("k3y", Direction.ASC).partial(of(new org.bson.Document("qty", new org.bson.Document("$gte", 10))));
    StepVerifier.create(indexOps.ensureIndex(id)).expectNextCount(1).verifyComplete();
    // 
    StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("partial-with-dbo"))).consumeNextWith(indexInfo -> {
        assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"qty\" : { \"$gte\" : 10 } }");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Collation(org.springframework.data.mongodb.core.query.Collation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) MongoClient(com.mongodb.reactivestreams.client.MongoClient) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Before(org.junit.Before) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Configuration(org.springframework.context.annotation.Configuration) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

IndexDefinition (org.springframework.data.mongodb.core.index.IndexDefinition)11 Test (org.junit.Test)10 Index (org.springframework.data.mongodb.core.index.Index)10 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)10 Document (org.bson.Document)8 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)7 Before (org.junit.Before)6 MongoClient (com.mongodb.reactivestreams.client.MongoClient)5 MongoClients (com.mongodb.reactivestreams.client.MongoClients)5 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)5 Predicate (java.util.function.Predicate)5 Assertions (org.assertj.core.api.Assertions)5 Assume (org.junit.Assume)5 RunWith (org.junit.runner.RunWith)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Configuration (org.springframework.context.annotation.Configuration)5 Direction (org.springframework.data.domain.Sort.Direction)5 AbstractReactiveMongoConfiguration (org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration)5 PartialIndexFilter (org.springframework.data.mongodb.core.index.PartialIndexFilter)5 Field (org.springframework.data.mongodb.core.mapping.Field)5