-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decay function support for MultiFunctionScoreQuery (#641)
* Add decay function support for MultiFunctionScoreQuery
- Loading branch information
1 parent
915da1a
commit 12d836e
Showing
12 changed files
with
684 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...om/yelp/nrtsearch/server/luceneserver/search/query/multifunction/DecayFilterFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yelp.nrtsearch.server.luceneserver.search.query.multifunction; | ||
|
||
import com.yelp.nrtsearch.server.grpc.MultiFunctionScoreQuery; | ||
import org.apache.lucene.search.Query; | ||
|
||
public abstract class DecayFilterFunction extends FilterFunction { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param filterQuery filter to use when applying this function, or null if none | ||
* @param weight weight multiple to scale the function score | ||
* @param decayFunction to score a document with a function that decays depending on the distance | ||
* between an origin point and a numeric doc field value | ||
*/ | ||
public DecayFilterFunction( | ||
Query filterQuery, float weight, MultiFunctionScoreQuery.DecayFunction decayFunction) { | ||
super(filterQuery, weight); | ||
if (decayFunction.getDecay() <= 0 || decayFunction.getDecay() >= 1) { | ||
throw new IllegalArgumentException( | ||
"decay rate should be between (0, 1) but is " + decayFunction.getDecay()); | ||
} | ||
} | ||
|
||
protected DecayFunction getDecayType(MultiFunctionScoreQuery.DecayType decayType) { | ||
return switch (decayType) { | ||
case DECAY_TYPE_GUASSIAN -> new GuassianDecayFunction(); | ||
case DECAY_TYPE_EXPONENTIAL -> new ExponentialDecayFunction(); | ||
case DECAY_TYPE_LINEAR -> new LinearDecayFunction(); | ||
default -> throw new IllegalArgumentException( | ||
decayType | ||
+ " not supported. Only exponential, guassian and linear decay functions are supported"); | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...java/com/yelp/nrtsearch/server/luceneserver/search/query/multifunction/DecayFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yelp.nrtsearch.server.luceneserver.search.query.multifunction; | ||
|
||
import org.apache.lucene.search.Explanation; | ||
|
||
public interface DecayFunction { | ||
/** | ||
* Computes the decayed score based on the provided distance, offset, and scale. | ||
* | ||
* @param distance the distance from a given origin point. | ||
* @param offset the point after which the decay starts. | ||
* @param scale scale factor that influences the rate of decay. This scale value is computed from | ||
* the user given scale using the computeScale() method. | ||
* @return the decayed score after applying the decay function | ||
*/ | ||
double computeScore(double distance, double offset, double scale); | ||
|
||
/** | ||
* Computes the adjusted scale based on a user given scale and decay rate. | ||
* | ||
* @param scale user given scale. | ||
* @param decay decay rate that decides how the score decreases. | ||
* @return adjusted scale which will be used by the computeScore() method. | ||
*/ | ||
double computeScale(double scale, double decay); | ||
|
||
/** | ||
* Provides an explanation for the computed score based on the given distance, offset, and scale. | ||
* | ||
* @param distance the distance from a given origin point. | ||
* @param offset the point after which the decay starts. | ||
* @param scale scale factor that influences the rate of decay. This scale value is computed from | ||
* the user given scale using the computeScale() method. | ||
* @return Explanation object that details the calculations involved in computing the decayed | ||
* score. | ||
*/ | ||
Explanation explainComputeScore(double distance, double offset, double scale); | ||
} |
37 changes: 37 additions & 0 deletions
37
...lp/nrtsearch/server/luceneserver/search/query/multifunction/ExponentialDecayFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yelp.nrtsearch.server.luceneserver.search.query.multifunction; | ||
|
||
import org.apache.lucene.search.Explanation; | ||
|
||
public class ExponentialDecayFunction implements DecayFunction { | ||
@Override | ||
public double computeScore(double distance, double offset, double scale) { | ||
return Math.exp(scale * Math.max(0.0, distance - offset)); | ||
} | ||
|
||
@Override | ||
public double computeScale(double scale, double decay) { | ||
return Math.log(decay) / scale; | ||
} | ||
|
||
@Override | ||
public Explanation explainComputeScore(double distance, double offset, double scale) { | ||
return Explanation.match( | ||
(float) computeScore(distance, offset, scale), | ||
"exp(" + scale + " * max(0.0, " + distance + " - " + offset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.