blogPosts = template.find(query, BlogPost.class);
+ printResult(blogPosts, criteria);
+ }
+
+ /**
+ * Creates the mongodb text index for {@link BlogPost}.
+ *
+ *
+ *
+ * db.collection.ensureIndex(
+ * {
+ * "title" : "text"
+ * "content" : "text"
+ * "categories" : "text",
+ * },
+ * {
+ * weights : {
+ * "title" : 3,
+ * "content" : 2
+ * }
+ * }
+ * )
+ *
+ *
+ */
+ private void createIndex() {
+
+ TextIndexDefinition textIndex = new TextIndexDefinitionBuilder()//
+ .onField("title", 3F) //
+ .onField("content", 2F) //
+ .onField("categories") //
+ .build();
+
+ template.indexOps(BlogPost.class).ensureIndex(textIndex);
+ }
+
+ private void loadTestData() throws Exception {
+
+ BlogPostInitializer initializer = new BlogPostInitializer(MongoTestConfiguration.BLOG_POST_ATOM_FEED_SOURCE);
+ initializer.initialize(this.template);
+ }
+
+}
diff --git a/mongodb/text-search/src/test/java/example/springdata/mongodb/util/BlogPostInitializer.java b/mongodb/text-search/src/test/java/example/springdata/mongodb/util/BlogPostInitializer.java
new file mode 100644
index 00000000..91e6fe07
--- /dev/null
+++ b/mongodb/text-search/src/test/java/example/springdata/mongodb/util/BlogPostInitializer.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2014 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 example.springdata.mongodb.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.convert.converter.Converter;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+import com.sun.syndication.feed.atom.Category;
+import com.sun.syndication.feed.atom.Content;
+import com.sun.syndication.feed.atom.Entry;
+import com.sun.syndication.feed.atom.Feed;
+
+import example.springdata.mongodb.textsearch.BlogPost;
+
+/**
+ * @author Christoph Strobl
+ */
+public class BlogPostInitializer implements InitializingBean {
+
+ private final String url;
+ private final RestTemplate restTemplate;
+ private final Converter converter;
+
+ @Autowired MongoTemplate mongoTemplate;
+
+ public BlogPostInitializer(String url) {
+
+ restTemplate = new RestTemplate();
+ this.converter = new EntryConverter();
+ this.url = url;
+ }
+
+ public void initialize(MongoTemplate mongoTemplate) {
+
+ ResponseEntity feed = restTemplate.getForEntity(url, Feed.class);
+ if (feed.hasBody()) {
+ for (Object entry : feed.getBody().getEntries()) {
+ if (entry instanceof Entry) {
+ mongoTemplate.save(converter.convert((Entry) entry));
+ }
+ }
+ }
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ initialize(this.mongoTemplate);
+ }
+
+ /**
+ * {@link Converter} implementation capable of converting atom feed {@link Entry} into {@link BlogPost}.
+ *
+ * @author Christoph Strobl
+ */
+ static class EntryConverter implements Converter {
+
+ @Override
+ public BlogPost convert(Entry source) {
+
+ BlogPost post = new BlogPost();
+
+ post.setId(source.getId());
+ post.setTitle(source.getTitle());
+
+ for (Object content : source.getContents()) {
+ if (content instanceof Content) {
+ post.setContent(((Content) content).getValue());
+ }
+ }
+
+ List categories = new ArrayList();
+ for (Object category : source.getCategories()) {
+ if (category instanceof Category) {
+ categories.add(((Category) category).getLabel());
+ }
+ }
+ post.setCategories(categories);
+
+ return post;
+ }
+ }
+
+}
diff --git a/mongodb/text-search/src/test/java/example/springdata/mongodb/util/ConsoleResultPrinter.java b/mongodb/text-search/src/test/java/example/springdata/mongodb/util/ConsoleResultPrinter.java
new file mode 100644
index 00000000..c76577a8
--- /dev/null
+++ b/mongodb/text-search/src/test/java/example/springdata/mongodb/util/ConsoleResultPrinter.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2014 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 example.springdata.mongodb.util;
+
+import java.util.Collection;
+
+import org.springframework.data.mongodb.core.query.TextCriteria;
+
+import example.springdata.mongodb.textsearch.BlogPost;
+
+/**
+ * Just a little helper for showing {@link BlogPost}s output on the console.
+ *
+ * @author Christoph Strobl
+ */
+public class ConsoleResultPrinter {
+
+ public static void printResult(Collection blogPosts, TextCriteria criteria) {
+
+ System.out.println(String.format("XXXXXXXXXXXX -- Found %s blogPosts matching '%s' --XXXXXXXXXXXX",
+ blogPosts.size(), criteria != null ? criteria.getCriteriaObject() : ""));
+
+ for (BlogPost blogPost : blogPosts) {
+ System.out.println(blogPost);
+ }
+ System.out.println("XXXXXXXXXXXX -- XXXXXXXXXXXX -- XXXXXXXXXXXX\r\n");
+ }
+
+}
diff --git a/mongodb/text-search/src/test/resources/logback.xml b/mongodb/text-search/src/test/resources/logback.xml
new file mode 100644
index 00000000..61c86dd9
--- /dev/null
+++ b/mongodb/text-search/src/test/resources/logback.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+ %d %5p %40.40c:%4L - %m%n
+
+
+
+
+
+
+
+
\ No newline at end of file