Skip to content

Commit

Permalink
Fix compile test and add java doc
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chu <[email protected]>
  • Loading branch information
noCharger committed Aug 3, 2023
1 parent 6121570 commit 421dabc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.opensearch.common.settings.Settings;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.ScriptPlugin;
Expand Down Expand Up @@ -120,20 +121,23 @@ public boolean isResultDeterministic() {
@Override
public LeafFactory newFactory(
Map<String, Object> params,
SearchLookup lookup
SearchLookup lookup,
IndexSearcher indexSearcher
) {
return new PureDfLeafFactory(params, lookup);
return new PureDfLeafFactory(params, lookup, indexSearcher);
}
}

private static class PureDfLeafFactory implements LeafFactory {
private final Map<String, Object> params;
private final SearchLookup lookup;

private final IndexSearcher indexSearcher;
private final String field;
private final String term;

private PureDfLeafFactory(
Map<String, Object> params, SearchLookup lookup) {
Map<String, Object> params, SearchLookup lookup, IndexSearcher indexSearcher) {
if (params.containsKey("field") == false) {
throw new IllegalArgumentException(
"Missing parameter [field]");
Expand All @@ -144,6 +148,7 @@ private PureDfLeafFactory(
}
this.params = params;
this.lookup = lookup;
this.indexSearcher = indexSearcher;
field = params.get("field").toString();
term = params.get("term").toString();
}
Expand All @@ -163,7 +168,7 @@ public ScoreScript newInstance(LeafReaderContext context)
* the field and/or term don't exist in this segment,
* so always return 0
*/
return new ScoreScript(params, lookup, context) {
return new ScoreScript(params, lookup, indexSearcher, context) {
@Override
public double execute(
ExplanationHolder explanation
Expand All @@ -172,7 +177,7 @@ public double execute(
}
};
}
return new ScoreScript(params, lookup, context) {
return new ScoreScript(params, lookup, indexSearcher, context) {
int currentDocid = -1;
@Override
public void setDocument(int docid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.io.IOException;
import java.util.Map;

/**
* Abstract class representing a term frequency function.
*/
public abstract class TermFrequencyFunction {

protected final String field;
Expand All @@ -35,6 +38,9 @@ public TermFrequencyFunction(String field, String term, int docId, Map<Object, O

public abstract Object execute(LeafReaderContext readerContext) throws IOException;

/**
* Factory class to create term frequency functions.
*/
public static class TermFrequencyFunctionFactory {
public static TermFrequencyFunction createFunction(
TermFrequencyFunctionNamesEnum functionName,
Expand All @@ -58,6 +64,9 @@ public static TermFrequencyFunction createFunction(
}
}

/**
* TermFreqFunction computes the term frequency in a field.
*/
public static class TermFreqFunction extends TermFrequencyFunction {

public TermFreqFunction(String field, String term, int docId, Map<Object, Object> context) {
Expand All @@ -71,6 +80,9 @@ public Integer execute(LeafReaderContext readerContext) throws IOException {
}
}

/**
* TFFunction computes the term frequency-inverse document frequency (tf-idf) in a field.
*/
public static class TFFunction extends TermFrequencyFunction {

public TFFunction(String field, String term, int docId, Map<Object, Object> context) {
Expand All @@ -84,6 +96,9 @@ public Float execute(LeafReaderContext readerContext) throws IOException {
}
}

/**
* TotalTermFreq computes the total term frequency in a field.
*/
public static class TotalTermFreq extends TermFrequencyFunction {

public TotalTermFreq(String field, String term, int docId, Map<Object, Object> context) {
Expand All @@ -98,6 +113,9 @@ public Long execute(LeafReaderContext readerContext) throws IOException {
}
}

/**
* SumTotalTermFreq computes the sum of total term frequencies within a field.
*/
public static class SumTotalTermFreq extends TermFrequencyFunction {

public SumTotalTermFreq(String field, String term, int docId, Map<Object, Object> context) {
Expand All @@ -112,6 +130,9 @@ public Long execute(LeafReaderContext readerContext) throws IOException {
}
}

/**
* Enum representing the names of term frequency functions.
*/
public enum TermFrequencyFunctionNamesEnum {
TERM_FREQ("termFreq"),
TF("tf"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public Map<String, ScriptDocValues<?>> getDoc() {
}

public Object getTermFrequency(TermFrequencyFunctionNamesEnum functionName, String field, String val) throws IOException {
// Fetch data from local cache
Map<Object, Object> context = new HashMap<>() {
{
put("searcher", indexSearcher);
Expand Down
20 changes: 20 additions & 0 deletions server/src/main/java/org/opensearch/script/ScoreScriptUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public static double sigmoid(double value, double k, double a) {
return Math.pow(value, a) / (Math.pow(k, a) + Math.pow(value, a));
}

/**
* Retrieves the term frequency within a field for a specific term.
*
* @opensearch.internal
*/
public static final class TermFreq {
private final ScoreScript scoreScript;

Expand All @@ -89,6 +94,11 @@ public int termFreq(String field, String term) {
}
}

/**
* Calculates the term frequency-inverse document frequency (tf-idf) for a specific term within a field.
*
* @opensearch.internal
*/
public static final class TF {
private final ScoreScript scoreScript;

Expand All @@ -105,6 +115,11 @@ public float tf(String field, String term) {
}
}

/**
* Retrieves the total term frequency within a field for a specific term.
*
* @opensearch.internal
*/
public static final class TotalTermFreq {
private final ScoreScript scoreScript;

Expand All @@ -121,6 +136,11 @@ public long totalTermFreq(String field, String term) {
}
}

/**
* Retrieves the sum of total term frequencies within a field.
*
* @opensearch.internal
*/
public static final class SumTotalTermFreq {
private final ScoreScript scoreScript;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private ScoreScript.LeafFactory newFactory(
) {
SearchLookup lookup = mock(SearchLookup.class);
LeafSearchLookup leafLookup = mock(LeafSearchLookup.class);
IndexSearcher indexSearcher = mock(IndexSearcher.class);
when(lookup.getLeafSearchLookup(any())).thenReturn(leafLookup);
return new ScoreScript.LeafFactory() {
@Override
Expand All @@ -193,7 +194,7 @@ public boolean needs_score() {

@Override
public ScoreScript newInstance(LeafReaderContext ctx) throws IOException {
return new ScoreScript(script.getParams(), lookup, leafReaderContext) {
return new ScoreScript(script.getParams(), lookup, indexSearcher, leafReaderContext) {
@Override
public double execute(ExplanationHolder explanation) {
return function.apply(explanation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package org.opensearch.script;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Scorable;
import org.opensearch.index.query.IntervalFilterScript;
import org.opensearch.index.similarity.ScriptedSimilarity.Doc;
Expand Down Expand Up @@ -624,7 +625,7 @@ public MockScoreScript(MockDeterministicScript script) {
}

@Override
public ScoreScript.LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup) {
public ScoreScript.LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup, IndexSearcher indexSearcher) {
return new ScoreScript.LeafFactory() {
@Override
public boolean needs_score() {
Expand All @@ -634,7 +635,7 @@ public boolean needs_score() {
@Override
public ScoreScript newInstance(LeafReaderContext ctx) throws IOException {
Scorable[] scorerHolder = new Scorable[1];
return new ScoreScript(params, lookup, ctx) {
return new ScoreScript(params, lookup, indexSearcher, ctx) {
@Override
public double execute(ExplanationHolder explanation) {
Map<String, Object> vars = new HashMap<>(getParams());
Expand Down

0 comments on commit 421dabc

Please sign in to comment.