sandeep
sandeep

Reputation: 61

Does spring-data-mongodb supports Atlas search? need an example of it

I am trying to find how to use mongo Atlas search indexes, from java application, which is using spring-data-mongodb to query the data, can anyone share an example for it

what i found was as code as below, but that is used for MongoDB Text search, though it is working, but not sure whether it is using Atlas search defined index.

TextQuery textQuery = TextQuery.queryText(new TextCriteria().matchingAny(text)).sortByScore();
    textQuery.fields().include("cast").include("title").include("id");
    List<Movies> movies = mongoOperations
            .find(textQuery, Movies.class);

I want smaple java code using spring-data-mongodb for below query:

[
  {
    $search: {
      index: 'cast-fullplot',
      text: {
        query: 'sandeep',
        path: {
          'wildcard': '*'
        }
      }
    }
  }
]

It will be helpful if anyone can explain how MongoDB Text Search is different from Mongo Atlas Search and correct way of using Atalas Search with the help of java spring-data-mongodb.

How to code below with spring-data-mongodb:

Arrays.asList(new Document("$search", 
    new Document("index", "cast-fullplot")
            .append("text", 
    new Document("query", "sandeep")
                .append("path", 
    new Document("wildcard", "*")))), 
    new Document())

Upvotes: 4

Views: 1294

Answers (1)

Vitor Vidal
Vitor Vidal

Reputation: 116

Yes, spring-data-mongo supports the aggregation pipeline, which you'll use to execute your query.

You need to define a document list, with the steps defined in your query, in the correct order. Atlas Search must be the first step in the pipeline, as it stands. You can translate your query to the aggregation pipeline using the Mongo Atlas interface, they have an option to export the pipeline array in the language of your choosing. Then, you just need to execute the query and map the list of responses to your entity class.

You can see an example below:

public class SearchRepositoryImpl implements SearchRepositoryCustom {

private final MongoClient mongoClient;

public SearchRepositoryImpl(MongoClient mongoClient) {
    this.mongoClient = mongoClient;
}

@Override
public List<SearchEntity> searchByFilter(String text) {
    // You can add codec configuration in your database object. This might be needed to map
    // your object to the mongodb data
    MongoDatabase database = mongoClient.getDatabase("aggregation");
    MongoCollection<Document> collection = database.getCollection("restaurants");

    List<Document> pipeline = List.of(new Document("$search", new Document("index", "default2")
            .append("text", new Document("query", "Many people").append("path", new Document("wildcard", "*")))));

    List<SearchEntity> searchEntityList = new ArrayList<>();
    collection.aggregate(pipeline, SearchEntity.class).forEach(searchEntityList::add);

    return searchEntityList;
}

}

Upvotes: 1

Related Questions