mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -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:
		| @@ -89,7 +89,7 @@ class MatchingModel(models.Model): | ||||
|             search_kwargs = {"flags": re.IGNORECASE} | ||||
|  | ||||
|         if self.matching_algorithm == self.MATCH_ALL: | ||||
|             for word in self.match.split(" "): | ||||
|             for word in self._split_match(): | ||||
|                 search_result = re.search( | ||||
|                     r"\b{}\b".format(word), text, **search_kwargs) | ||||
|                 if not search_result: | ||||
| @@ -97,7 +97,7 @@ class MatchingModel(models.Model): | ||||
|             return True | ||||
|  | ||||
|         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): | ||||
|                     return True | ||||
|             return False | ||||
| @@ -121,6 +121,19 @@ class MatchingModel(models.Model): | ||||
|  | ||||
|         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): | ||||
|  | ||||
|         self.match = self.match.lower() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ishirav
					ishirav