Created
September 8, 2016 11:50
-
-
Save christophstrobl/c1a8e98159a1d346dee036863ddbbe14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2016 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.springframework.data.mongodb.repository; | |
import static org.hamcrest.core.IsCollectionContaining.*; | |
import static org.hamcrest.core.IsNot.*; | |
import static org.junit.Assert.*; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.mongodb.config.AbstractMongoConfiguration; | |
import org.springframework.data.mongodb.core.MongoTemplate; | |
import org.springframework.data.mongodb.core.index.TextIndexDefinition.TextIndexDefinitionBuilder; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
import org.springframework.data.mongodb.core.query.TextCriteria; | |
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import com.mongodb.Mongo; | |
import com.mongodb.MongoClient; | |
import lombok.Data; | |
/** | |
* @author Christoph Strobl | |
*/ | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration | |
public class SpringDataMongodbFullTextIndexSetupSample { | |
@Autowired MongoTemplate template; | |
FullTextRepository repo; | |
private Physicist hawking; | |
private Physicist einstein; | |
private Musician elvis; | |
@Before | |
public void setUp() { | |
template.indexOps(Person.class).ensureIndex(new TextIndexDefinitionBuilder().onField("biography").build()); | |
this.repo = new MongoRepositoryFactory(this.template).getRepository(FullTextRepository.class); | |
hawking = new Physicist(); | |
hawking.biography = "Stephen William Hawking (born 8 January 1942) is an English theoretical physicist, cosmologist, author and Director of Research at the Centre for Theoretical Cosmology within the University of Cambridge"; | |
einstein = new Physicist(); | |
einstein.biography = "Albert Einstein (14 March 1879 – 18 April 1955) was a German-born theoretical physicist"; | |
elvis = new Musician(); | |
elvis.biography = "Elvis Aaron Presley (January 8, 1935 – August 16, 1977) was an American musician and actor. Regarded as one of the most significant cultural icons of the 20th century, he is often referred to as the King of Rock and Roll, or simply, the King."; | |
repo.save(Arrays.asList(hawking, einstein, elvis)); | |
} | |
@Test | |
public void thisShouldAcutallyWork() { | |
List<Person> findAllBy = repo.findAllBy(TextCriteria.forDefaultLanguage().matching("physicist")); | |
assertThat(findAllBy, hasItems(hawking, einstein)); | |
assertThat(findAllBy, not(hasItems(elvis))); | |
} | |
@org.springframework.context.annotation.Configuration | |
public static class Configuration extends AbstractMongoConfiguration { | |
@Override | |
protected String getDatabaseName() { | |
return "SO-39377546"; | |
} | |
@Override | |
public Mongo mongo() throws Exception { | |
return new MongoClient(); | |
} | |
} | |
@Data | |
@Document(collection = "persons") | |
abstract static class Person { | |
@Id private String id; | |
String name; | |
String biography; | |
} | |
static class Musician extends Person { | |
private String something; | |
} | |
static class Physicist extends Person { | |
private String somethingElse; | |
} | |
static interface FullTextRepository extends MongoRepository<Person, String> { | |
List<Person> findAllBy(TextCriteria textCriteria); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment