mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-04 03:27:12 -05:00 
			
		
		
		
	Support search terms that contain multiple words in ANY/ALL matching modes, by surrounding the terms with double quotes.
This commit is contained in:
		
							parent
							
								
									af4623e605
								
							
						
					
					
						commit
						6a36a4ec97
					
				@ -89,7 +89,7 @@ class MatchingModel(models.Model):
 | 
				
			|||||||
            search_kwargs = {"flags": re.IGNORECASE}
 | 
					            search_kwargs = {"flags": re.IGNORECASE}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.matching_algorithm == self.MATCH_ALL:
 | 
					        if self.matching_algorithm == self.MATCH_ALL:
 | 
				
			||||||
            for word in self.match.split(" "):
 | 
					            for word in self._split_match():
 | 
				
			||||||
                search_result = re.search(
 | 
					                search_result = re.search(
 | 
				
			||||||
                    r"\b{}\b".format(word), text, **search_kwargs)
 | 
					                    r"\b{}\b".format(word), text, **search_kwargs)
 | 
				
			||||||
                if not search_result:
 | 
					                if not search_result:
 | 
				
			||||||
@ -97,7 +97,7 @@ class MatchingModel(models.Model):
 | 
				
			|||||||
            return True
 | 
					            return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.matching_algorithm == self.MATCH_ANY:
 | 
					        if self.matching_algorithm == self.MATCH_ANY:
 | 
				
			||||||
            for word in self.match.split(" "):
 | 
					            for word in self._split_match():
 | 
				
			||||||
                if re.search(r"\b{}\b".format(word), text, **search_kwargs):
 | 
					                if re.search(r"\b{}\b".format(word), text, **search_kwargs):
 | 
				
			||||||
                    return True
 | 
					                    return True
 | 
				
			||||||
            return False
 | 
					            return False
 | 
				
			||||||
@ -121,6 +121,19 @@ class MatchingModel(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        raise NotImplementedError("Unsupported matching algorithm")
 | 
					        raise NotImplementedError("Unsupported matching algorithm")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _split_match(self):
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
 | 
					        Splits the match to invidual keywords, getting rid of unecessary spaces
 | 
				
			||||||
 | 
					        and grouping quoted words together.
 | 
				
			||||||
 | 
					        Example:
 | 
				
			||||||
 | 
					        '  some random  words "with   quotes  " and   spaces'
 | 
				
			||||||
 | 
					            ==>
 | 
				
			||||||
 | 
					        ['some', 'random', 'words', 'with quotes', 'and', 'spaces']
 | 
				
			||||||
 | 
					        '''
 | 
				
			||||||
 | 
					        findterms = re.compile(r'"([^"]+)"|(\S+)').findall
 | 
				
			||||||
 | 
					        normspace = re.compile(r'\s{2,}').sub
 | 
				
			||||||
 | 
					        return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(self.match)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def save(self, *args, **kwargs):
 | 
					    def save(self, *args, **kwargs):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.match = self.match.lower()
 | 
					        self.match = self.match.lower()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user