Skip to content

Commit

Permalink
remove unneeded collection instance (we can use the overload of aggre…
Browse files Browse the repository at this point in the history
…gate that takes a result class type)
  • Loading branch information
luketn committed Dec 10, 2024
1 parent 390c648 commit 65c5d02
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/main/java/com/mycodefu/mongodb/ImageDataAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,19 @@
import com.mycodefu.model.Image;
import com.mycodefu.model.ImageSearchResult;
import com.mycodefu.mongodb.atlas.MongoConnection;
import com.mycodefu.service.SimpleServer;
import org.bson.BsonDocumentReader;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.conversions.Bson;
import org.bson.json.JsonWriterSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.search.SearchFacet.stringFacet;
import static com.mongodb.client.model.search.SearchPath.fieldPath;
import static com.mycodefu.mongodb.atlas.MongoConnection.codecRegistry;
import static com.mycodefu.mongodb.atlas.MongoConnection.database_name;

public class ImageDataAccess implements AutoCloseable {
Expand All @@ -37,13 +32,11 @@ public class ImageDataAccess implements AutoCloseable {

private final MongoClient mongoClient;
private final MongoCollection<Image> imageCollection;
private final MongoCollection<ImageSearchResult> imageCollectionForAggregate;

public ImageDataAccess(MongoClient mongoClient, String databaseName, String collectionName) {
this.mongoClient = mongoClient;
MongoDatabase database = mongoClient.getDatabase(databaseName);
this.imageCollection = database.getCollection(collectionName, Image.class);
this.imageCollectionForAggregate = database.getCollection(collectionName, ImageSearchResult.class);
}

public static ImageDataAccess getInstance() {
Expand Down Expand Up @@ -79,7 +72,12 @@ public ImageSearchResult search(String text, Integer page, Boolean hasPerson, Li
skip = page * pageSize;
}
if (text != null) {
clauses.add(SearchOperator.text(fieldPath("caption"), text));
clauses.add(SearchOperator
.text(
fieldPath("caption"),
text
)
);
}
if (hasPerson != null) {
clauses.add(equals("hasPerson", hasPerson));
Expand Down Expand Up @@ -137,14 +135,7 @@ public ImageSearchResult search(String text, Integer page, Boolean hasPerson, Li
}
}

ArrayList<ImageSearchResult> results = imageCollectionForAggregate.aggregate(aggregateStages).into(new ArrayList<>());

if (results.isEmpty()) {
log.trace("No results from search");
return null;
}

ImageSearchResult imageSearchResult = results.getFirst();
ImageSearchResult imageSearchResult = imageCollection.aggregate(aggregateStages, ImageSearchResult.class).first();

if (log.isTraceEnabled()) {
String json = JsonUtil.writeToString(imageSearchResult);
Expand Down

0 comments on commit 65c5d02

Please sign in to comment.