Class VectorDistanceFunction
java.lang.Object
org.apache.lucene.queries.function.ValueSource
org.apache.solr.search.function.distance.VectorDistanceFunction
- Direct Known Subclasses:
SquaredEuclideanFunction
public class VectorDistanceFunction
extends org.apache.lucene.queries.function.ValueSource
Calculate the p-norm for a Vector. See http://en.wikipedia.org/wiki/Lp_space
Common cases:
- 0 = Sparseness calculation
- 1 = Manhattan distance
- 2 = Euclidean distance
- Integer.MAX_VALUE = infinite norm
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected floatprotected floatprotected org.apache.lucene.queries.function.valuesource.MultiValueSourceprotected org.apache.lucene.queries.function.valuesource.MultiValueSource -
Constructor Summary
ConstructorsConstructorDescriptionVectorDistanceFunction(float power, org.apache.lucene.queries.function.valuesource.MultiValueSource source1, org.apache.lucene.queries.function.valuesource.MultiValueSource source2) -
Method Summary
Modifier and TypeMethodDescriptionvoidcreateWeight(Map<Object, Object> context, org.apache.lucene.search.IndexSearcher searcher) protected doubledistance(int doc, org.apache.lucene.queries.function.FunctionValues dv1, org.apache.lucene.queries.function.FunctionValues dv2) Calculate the distancestatic doubledistSquaredCartesian(double[] vec1, double[] vec2) The square of the cartesian Distance.booleanorg.apache.lucene.queries.function.FunctionValuesinthashCode()protected Stringname()static doublevectorDistance(double[] vec1, double[] vec2, double power) Calculate the p-norm (i.e.static doublevectorDistance(double[] vec1, double[] vec2, double power, double oneOverPower) Calculate the p-norm (i.e.Methods inherited from class org.apache.lucene.queries.function.ValueSource
asDoubleValuesSource, asLongValuesSource, fromDoubleValuesSource, getSortField, newContext, toString
-
Field Details
-
source1
protected org.apache.lucene.queries.function.valuesource.MultiValueSource source1 -
source2
protected org.apache.lucene.queries.function.valuesource.MultiValueSource source2 -
power
protected float power -
oneOverPower
protected float oneOverPower
-
-
Constructor Details
-
VectorDistanceFunction
public VectorDistanceFunction(float power, org.apache.lucene.queries.function.valuesource.MultiValueSource source1, org.apache.lucene.queries.function.valuesource.MultiValueSource source2)
-
-
Method Details
-
name
-
distance
protected double distance(int doc, org.apache.lucene.queries.function.FunctionValues dv1, org.apache.lucene.queries.function.FunctionValues dv2) throws IOException Calculate the distance- Parameters:
doc- The current docdv1- The values from the first MultiValueSourcedv2- The values from the second MultiValueSource- Returns:
- The distance
- Throws:
IOException
-
vectorDistance
public static double vectorDistance(double[] vec1, double[] vec2, double power) Calculate the p-norm (i.e. length) between two vectors.See Lp space
- Parameters:
vec1- The first vectorvec2- The second vectorpower- The power (2 for cartesian distance, 1 for manhattan, etc.)- Returns:
- The length.
- See Also:
-
vectorDistance
public static double vectorDistance(double[] vec1, double[] vec2, double power, double oneOverPower) Calculate the p-norm (i.e. length) between two vectors.- Parameters:
vec1- The first vectorvec2- The second vectorpower- The power (2 for cartesian distance, 1 for manhattan, etc.)oneOverPower- If you've pre-calculated oneOverPower and cached it, use this method to save one division operation overvectorDistance(double[], double[], double).- Returns:
- The length.
-
distSquaredCartesian
public static double distSquaredCartesian(double[] vec1, double[] vec2) The square of the cartesian Distance. Not really a distance, but useful if all that matters is comparing the result to another one.- Parameters:
vec1- The first pointvec2- The second point- Returns:
- The squared cartesian distance
-
getValues
public org.apache.lucene.queries.function.FunctionValues getValues(Map<Object, Object> context, org.apache.lucene.index.LeafReaderContext readerContext) throws IOException- Specified by:
getValuesin classorg.apache.lucene.queries.function.ValueSource- Throws:
IOException
-
createWeight
public void createWeight(Map<Object, Object> context, org.apache.lucene.search.IndexSearcher searcher) throws IOException- Overrides:
createWeightin classorg.apache.lucene.queries.function.ValueSource- Throws:
IOException
-
equals
- Specified by:
equalsin classorg.apache.lucene.queries.function.ValueSource
-
hashCode
public int hashCode()- Specified by:
hashCodein classorg.apache.lucene.queries.function.ValueSource
-
description
- Specified by:
descriptionin classorg.apache.lucene.queries.function.ValueSource
-