Your question is broad, and has a lot of potential answers...
1) Lucene has a very configurable Scoring, that allows a lot of
customiztaion -- much of the scoring formula can be tweaked just by
changing the "Similarity" class used, other more complex things can be
achieved by writing your own Query classes
2) Solr allows for a *lot* of customization using "plugins" where just
about any class you can imagine (including Similarity, custom
RequestHandlers, and new Query clases) can be loaded from a JAR you
provide at runtime...
3) Solr has a special type of query called a FunctionQuery which makes
writing special Query Scoring based on numeric Document Fields really easy
... some very complicated things can be done right out of the box using
the Function Parsing supported by the SolrQueryParser...
...but more complicated things (like distance searching) would require you
to write a simple ValueSource definining your equation, and using that
ValueSource in a FunctionQuery you constructi na custom RequestHandler.
using FunctionQuery has been discussed on several Lucene lists in the
past, there have even been some fairly in depth discussion about using
it for Geo based scoring...