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

View File

@ -1,5 +1,7 @@
package com.baeldung.boot.atlassearch.web; package com.baeldung.boot.atlassearch.web;
import static com.mongodb.client.model.search.SearchPath.fieldPath;
import java.util.Collection; import java.util.Collection;
import org.bson.Document; import org.bson.Document;
@ -33,7 +35,7 @@ public class MovieAtlasSearchController {
@GetMapping("90s/{skip}/{limit}/with/{keywords}") @GetMapping("90s/{skip}/{limit}/with/{keywords}")
Document getMoviesUsingScoreBoost(@PathVariable int skip, @PathVariable int limit, @PathVariable String 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}") @PostMapping("90s/{skip}/{limit}/with/{keywords}")