Announcement Announcement Module
Collapse
No announcement yet.
Find using regex taking way too long Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #2
    Any chance you can put this into an executable test case? I suspect some indexes not being set or applied correctly. Are you sure you query the same collection on the shell that you query using the code?

    Comment


    • #3
      I have found that this is an issue with mongodb and using regex functionality.

      Comment

      Working...
      X