1. A computer-implemented method comprising: receiving a query; identifying an original term occurring in the query; determining a plurality of substitution contexts for the original term occurring in the query, wherein a substitution context specifies a context in which the original term in the query is replaced by a substitute term, and wherein a substitution context includes one or more context terms and an indication of a position in the query of the original term relative to the one or more context terms; generating a context hierarchy of the plurality of substitution contexts, wherein any conditions of a parent context in the context hierarchy also apply to each of one or more child contexts of the parent context in the context hierarchy, and wherein each child context in the context hierarchy includes all of the terms of a parent context of the child context as well as an additional term that does not occur in the parent context of the child context; determining a score for each substitution context of the plurality of substitution contexts including comparing a particular substitution context to its parent context in the context hierarchy; ranking the plurality of substitution contexts based on the score of each substitution context; and determining one or more substitute terms for the original term occurring in the query in a context specified by a highest-ranking substitution context of the plurality of substitution contexts.