One of the most important aspects of a GIS-equipped database is that it provides the user with the capability to quickly query for all points within some arbitrary geographic area that match some additional criteria. (E.g. "Find me the nearest 3 restaurants to this point on a map.")

Can anyone point me to a theoretical discussion of the algorithms involved? I want to learn how they work.

Ultimately, I want to apply the same capability to generalized sets of numerical data - a large cloud of points in an arbitrary, n-dimensional, non-euclidean space. For instance, a person's face can be characterized as a vector of numbers: [distance between eyes, distance from eye to mouth, width of face, length of face, etc.]. I want to film sidewalk traffic, estimate the features of each persons face, and then be able to make queries to the data later such as "given this person's face, find me the 100 most similar faces." 

Is there currently any existing software that provides the ability to search over these generalized spaces?