BAEL 6701 - MongoDB - Atlas Search using the Java Driver and Spring Data (#15069)

* research 1

* new search methods

* first draft

* removing old code

* updated to parent-boot-3

* mongo review 2
This commit is contained in:
Ulisses Lima 2023-11-05 02:45:44 -03:00 committed by GitHub
parent 89744aff6c
commit 0fc112e613
2 changed files with 26 additions and 26 deletions

View File

@ -11,13 +11,12 @@ import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.client.model.Projections.metaSearchScore;
import static com.mongodb.client.model.search.SearchCollector.facet;
import static com.mongodb.client.model.search.SearchCount.total;
import static com.mongodb.client.model.search.SearchFacet.combineToBson;
import static com.mongodb.client.model.search.SearchFacet.numberFacet;
import static com.mongodb.client.model.search.SearchFacet.stringFacet;
import static com.mongodb.client.model.search.SearchOperator.compound;
import static com.mongodb.client.model.search.SearchOperator.numberRange;
import static com.mongodb.client.model.search.SearchOperator.of;
import static com.mongodb.client.model.search.SearchOperator.text;
import static com.mongodb.client.model.search.SearchOptions.searchOptions;
import static com.mongodb.client.model.search.SearchPath.fieldPath;
@ -67,7 +66,7 @@ public class MovieAtlasSearchService {
builder.append("]");
LogManager.getLogger(MovieAtlasSearchService.class)
.debug(builder.toString());
.debug(builder.toString());
}
public Document late90sMovies(int skip, int limit, String keywords, SearchScore modifier) {
@ -130,7 +129,7 @@ public class MovieAtlasSearchService {
debug(pipeline);
return collection.aggregate(pipeline)
.first();
.first();
}
public Collection<Document> moviesByKeywords(String keywords) {
@ -150,34 +149,33 @@ public class MovieAtlasSearchService {
debug(pipeline);
return collection.aggregate(pipeline)
.into(new ArrayList<Document>());
.into(new ArrayList<>());
}
public Document genresThroughTheDecades(String genre) {
List<Bson> pipeline = asList(
searchMeta(of(
new Document("facet",
new Document("operator",
text(
fieldPath("genres"), genre
)
).append("facets", combineToBson(asList(
stringFacet("genresFacet",
fieldPath("genres")
).numBuckets(5),
numberFacet("yearFacet",
fieldPath("year"),
asList(1900, 1930, 1960, 1990, 2020)
)
)))
)),
searchOptions()
.index(config.getFacetIndex())
searchMeta(
facet(
text(
fieldPath("genres"), genre
),
asList(
stringFacet("genresFacet",
fieldPath("genres")
).numBuckets(5),
numberFacet("yearFacet",
fieldPath("year"),
asList(1900, 1930, 1960, 1990, 2020)
)
)
),
searchOptions()
.index(config.getFacetIndex())
)
);
debug(pipeline);
return collection.aggregate(pipeline)
.first();
.first();
}
}

View File

@ -1,5 +1,7 @@
package com.baeldung.boot.atlassearch.web;
import static com.mongodb.client.model.search.SearchPath.fieldPath;
import java.util.Collection;
import org.bson.Document;
@ -33,7 +35,7 @@ public class MovieAtlasSearchController {
@GetMapping("90s/{skip}/{limit}/with/{keywords}")
Document getMoviesUsingScoreBoost(@PathVariable int skip, @PathVariable int limit, @PathVariable String keywords) {
return service.late90sMovies(skip, limit, keywords, SearchScore.boost(2));
return service.late90sMovies(skip, limit, keywords, SearchScore.boost(fieldPath("imdb.votes")));
}
@PostMapping("90s/{skip}/{limit}/with/{keywords}")