@@ -17,8 +17,6 @@ package example.springdata.mongodb.textsearch;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.index.TextIndexed;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
@@ -31,17 +29,7 @@ import org.springframework.data.mongodb.core.mapping.TextScore;
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Document
|
||||
@Data
|
||||
public class BlogPost {
|
||||
public record BlogPost(@Id String id, @TextIndexed(weight = 3) String title, @TextIndexed(weight = 2) String content,
|
||||
@TextIndexed List<String> categories, @TextScore Float score) {
|
||||
|
||||
private @Id String id;
|
||||
private @TextIndexed(weight = 3) String title;
|
||||
private @TextIndexed(weight = 2) String content;
|
||||
private @TextIndexed List<String> categories;
|
||||
private @TextScore Float score;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BlogPost [id=" + id + ", score=" + score + ", title=" + title + ", categories=" + categories + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class MongoTestConfiguration {
|
||||
|
||||
public @Bean Jackson2RepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||
|
||||
Jackson2RepositoryPopulatorFactoryBean factoryBean = new Jackson2RepositoryPopulatorFactoryBean();
|
||||
var factoryBean = new Jackson2RepositoryPopulatorFactoryBean();
|
||||
factoryBean.setResources(new Resource[] { new ClassPathResource("spring-blog.atom.json") });
|
||||
return factoryBean;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class MongoTestConfiguration {
|
||||
@PostConstruct
|
||||
private void postConstruct() {
|
||||
|
||||
IndexResolver resolver = IndexResolver.create(operations.getConverter().getMappingContext());
|
||||
var resolver = IndexResolver.create(operations.getConverter().getMappingContext());
|
||||
|
||||
resolver.resolveIndexFor(BlogPost.class).forEach(operations.indexOps(BlogPost.class)::ensureIndex);
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ public class TextSearchRepositoryTests {
|
||||
@Test
|
||||
public void findAllBlogPostsWithRelease() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("release");
|
||||
List<BlogPost> blogPosts = repo.findAllBy(criteria);
|
||||
var criteria = TextCriteria.forDefaultLanguage().matchingAny("release");
|
||||
var blogPosts = repo.findAllBy(criteria);
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
@@ -59,8 +59,8 @@ public class TextSearchRepositoryTests {
|
||||
@Test
|
||||
public void findAllBlogPostsWithReleaseButHeyIDoWantTheEngineeringStuff() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("release").notMatching("engineering");
|
||||
List<BlogPost> blogPosts = repo.findAllBy(criteria);
|
||||
var criteria = TextCriteria.forDefaultLanguage().matchingAny("release").notMatching("engineering");
|
||||
var blogPosts = repo.findAllBy(criteria);
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
@@ -71,8 +71,8 @@ public class TextSearchRepositoryTests {
|
||||
@Test
|
||||
public void findAllBlogPostsByPhrase() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingPhrase("release candidate");
|
||||
List<BlogPost> blogPosts = repo.findAllBy(criteria);
|
||||
var criteria = TextCriteria.forDefaultLanguage().matchingPhrase("release candidate");
|
||||
var blogPosts = repo.findAllBy(criteria);
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
@@ -83,8 +83,8 @@ public class TextSearchRepositoryTests {
|
||||
@Test
|
||||
public void findAllBlogPostsByPhraseSortByScore() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingPhrase("release candidate");
|
||||
List<BlogPost> blogPosts = repo.findAllByOrderByScoreDesc(criteria);
|
||||
var criteria = TextCriteria.forDefaultLanguage().matchingPhrase("release candidate");
|
||||
var blogPosts = repo.findAllByOrderByScoreDesc(criteria);
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
|
||||
@@ -59,8 +59,8 @@ public class TextSearchTemplateTests {
|
||||
@Test
|
||||
public void findAllBlogPostsWithRelease() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("release");
|
||||
List<BlogPost> blogPosts = operations.find(query(criteria), BlogPost.class);
|
||||
var criteria = TextCriteria.forDefaultLanguage().matchingAny("release");
|
||||
var blogPosts = operations.find(query(criteria), BlogPost.class);
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
@@ -71,13 +71,13 @@ public class TextSearchTemplateTests {
|
||||
@Test
|
||||
public void findAllBlogPostsByPhraseSortByScore() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingPhrase("release");
|
||||
var criteria = TextCriteria.forDefaultLanguage().matchingPhrase("release");
|
||||
|
||||
TextQuery query = new TextQuery(criteria);
|
||||
var query = new TextQuery(criteria);
|
||||
query.setScoreFieldName("score");
|
||||
query.sortByScore();
|
||||
|
||||
List<BlogPost> blogPosts = operations.find(query, BlogPost.class);
|
||||
var blogPosts = operations.find(query, BlogPost.class);
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
|
||||
@@ -50,12 +50,12 @@ public enum BlogPostInitializer {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void loadFromClasspathSource(MongoOperations operations) throws Exception {
|
||||
|
||||
Jackson2ResourceReader reader = new Jackson2ResourceReader();
|
||||
var reader = new Jackson2ResourceReader();
|
||||
|
||||
Object source = reader.readFrom(new ClassPathResource("spring-blog.atom.json"), this.getClass().getClassLoader());
|
||||
var source = reader.readFrom(new ClassPathResource("spring-blog.atom.json"), this.getClass().getClassLoader());
|
||||
|
||||
if (source instanceof Iterable) {
|
||||
((Iterable) source).forEach(element -> operations.save(element));
|
||||
((Iterable) source).forEach(operations::save);
|
||||
} else {
|
||||
operations.save(source);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class ConsoleResultPrinter {
|
||||
System.out.println(String.format("XXXXXXXXXXXX -- Found %s blogPosts matching '%s' --XXXXXXXXXXXX",
|
||||
blogPosts.size(), criteria != null ? criteria.getCriteriaObject() : ""));
|
||||
|
||||
for (BlogPost blogPost : blogPosts) {
|
||||
for (var blogPost : blogPosts) {
|
||||
System.out.println(blogPost);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user