Quantcast
Viewing all articles
Browse latest Browse all 128

Find using regex taking way too long

Using the following code, I am doing a find using regex:

Code:


        public List<LogItem> executeQueryRegex(String fieldName, String searchText) {

                System.out.println("Entering executeQueryRegex");
               
                int rowCount = 0;

                MongoOperations mongoOps = mongoTemplate;

                Criteria criteria1 = Criteria.where(fieldName).regex(searchText, "i");

                Query messageQuery1 = new Query(criteria1);

                messageQuery1.fields().include("_id");
                messageQuery1.fields().include("message");
                //System.out.println("Calling: mongoOps.find");
                List<LogItem> logItems1 = mongoOps.find(messageQuery1, LogItem.class, "logItem");
                //System.out.println("Called: mongoOps.find");
                rowCount += logItems1.size();
                //System.out.println("Row Count: " + rowCount);
                //for (int index = 0; index < logItems1.size(); index++){
                //        System.out.println(logItems1.get(index).getId() + ", " + logItems1.get(index).getMessage());
                //}
                System.out.println("Row Count: " + rowCount);
               
                System.out.println("Exiting executeQueryRegex");
                return logItems1;
        }

This is the incoming parameters: "message", "^((.*?)(some text1))$"

This is taking over 6.5 seconds to retrieve 20 documents in a collection of 400,000 documents.

Running this query in Mongo shell returns very fast.

Why would Spring-data/Mongo take so much longer?

Viewing all articles
Browse latest Browse all 128

Trending Articles