mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Simplify search code for keyword searches.
We use an array now to build up the list of search operands and then consolidate the special search handling after the loop (which means setting the flag, putting two more columns in the query, and using ' '.join to build the string).
This commit is contained in:
		@@ -565,28 +565,31 @@ def add_narrow_conditions(user_profile: UserProfile,
 | 
				
			|||||||
    if first_visible_message_id > 0:
 | 
					    if first_visible_message_id > 0:
 | 
				
			||||||
        query = query.where(inner_msg_id_col >= first_visible_message_id)
 | 
					        query = query.where(inner_msg_id_col >= first_visible_message_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    is_search = False
 | 
					    is_search = False  # for now
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if narrow is None:
 | 
					    if narrow is None:
 | 
				
			||||||
        return (query, is_search)
 | 
					        return (query, is_search)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Build the query for the narrow
 | 
					    # Build the query for the narrow
 | 
				
			||||||
    builder = NarrowBuilder(user_profile, inner_msg_id_col)
 | 
					    builder = NarrowBuilder(user_profile, inner_msg_id_col)
 | 
				
			||||||
    search_term = {}  # type: Dict[str, Any]
 | 
					    search_operands = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # As we loop through terms, builder does most of the work to extend
 | 
				
			||||||
 | 
					    # our query, but we need to collect the search operands and handle
 | 
				
			||||||
 | 
					    # them after the loop.
 | 
				
			||||||
    for term in narrow:
 | 
					    for term in narrow:
 | 
				
			||||||
        if term['operator'] == 'search':
 | 
					        if term['operator'] == 'search':
 | 
				
			||||||
            if not is_search:
 | 
					            search_operands.append(term['operand'])
 | 
				
			||||||
                search_term = term
 | 
					 | 
				
			||||||
                query = query.column(column("subject")).column(column("rendered_content"))
 | 
					 | 
				
			||||||
                is_search = True
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                # Join the search operators if there are multiple of them
 | 
					 | 
				
			||||||
                search_term['operand'] += ' ' + term['operand']
 | 
					 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            query = builder.add_term(query, term)
 | 
					            query = builder.add_term(query, term)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if is_search:
 | 
					    if search_operands:
 | 
				
			||||||
 | 
					        is_search = True
 | 
				
			||||||
 | 
					        query = query.column(column("subject")).column(column("rendered_content"))
 | 
				
			||||||
 | 
					        search_term = dict(
 | 
				
			||||||
 | 
					            operator='search',
 | 
				
			||||||
 | 
					            operand=' '.join(search_operands)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        query = builder.add_term(query, search_term)
 | 
					        query = builder.add_term(query, search_term)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return (query, is_search)
 | 
					    return (query, is_search)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user