Skip to content

Commit

Permalink
Make ScriptedSimilarityTests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpountz committed Aug 24, 2018
1 parent de503ed commit 8a6505d
Showing 1 changed file with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexSearcher;
Expand All @@ -46,6 +47,7 @@
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;

public class ScriptedSimilarityTests extends ESTestCase {
Expand Down Expand Up @@ -86,6 +88,16 @@ public void testBasics() throws IOException {
public double execute(double weight, ScriptedSimilarity.Query query,
ScriptedSimilarity.Field field, ScriptedSimilarity.Term term,
ScriptedSimilarity.Doc doc) {

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if (Arrays.stream(stackTraceElements).anyMatch(ste -> {
return ste.getClassName().endsWith(".TermScorer") &&
ste.getMethodName().equals("score");
}) == false) {
// this might happen when computing max scores
return Float.MAX_VALUE;
}

assertEquals(1, weight, 0);
assertNotNull(doc);
assertEquals(2f, doc.getFreq(), 0);
Expand Down Expand Up @@ -147,14 +159,13 @@ public void testInitScript() throws IOException {

@Override
public double execute(ScriptedSimilarity.Query query, ScriptedSimilarity.Field field,
ScriptedSimilarity.Term term) {
assertNotNull(field);
ScriptedSimilarity.Term term) {
assertEquals(3, field.getDocCount());
assertEquals(5, field.getSumDocFreq());
assertEquals(6, field.getSumTotalTermFreq());
assertNotNull(term);
assertEquals(2, term.getDocFreq());
assertEquals(3, term.getTotalTermFreq());
assertEquals(1, term.getDocFreq());
assertEquals(2, term.getTotalTermFreq());
assertNotNull(query);
assertEquals(3.2f, query.getBoost(), 0);
initCalled.set(true);
Expand All @@ -171,6 +182,16 @@ public double execute(ScriptedSimilarity.Query query, ScriptedSimilarity.Field f
public double execute(double weight, ScriptedSimilarity.Query query,
ScriptedSimilarity.Field field, ScriptedSimilarity.Term term,
ScriptedSimilarity.Doc doc) {

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if (Arrays.stream(stackTraceElements).anyMatch(ste -> {
return ste.getClassName().endsWith(".TermScorer") &&
ste.getMethodName().equals("score");
}) == false) {
// this might happen when computing max scores
return Float.MAX_VALUE;
}

assertEquals(28, weight, 0d);
assertNotNull(doc);
assertEquals(2f, doc.getFreq(), 0);
Expand All @@ -180,8 +201,8 @@ public double execute(double weight, ScriptedSimilarity.Query query,
assertEquals(5, field.getSumDocFreq());
assertEquals(6, field.getSumTotalTermFreq());
assertNotNull(term);
assertEquals(2, term.getDocFreq());
assertEquals(3, term.getTotalTermFreq());
assertEquals(1, term.getDocFreq());
assertEquals(2, term.getTotalTermFreq());
assertNotNull(query);
assertEquals(3.2f, query.getBoost(), 0);
called.set(true);
Expand All @@ -195,8 +216,7 @@ public double execute(double weight, ScriptedSimilarity.Query query,
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim));

Document doc = new Document();
doc.add(new TextField("f", "foo bar", Store.NO));
doc.add(new StringField("match", "no", Store.NO));
doc.add(new TextField("f", "bar baz", Store.NO));
w.addDocument(doc);

doc = new Document();
Expand All @@ -206,17 +226,13 @@ public double execute(double weight, ScriptedSimilarity.Query query,

doc = new Document();
doc.add(new TextField("f", "bar", Store.NO));
doc.add(new StringField("match", "no", Store.NO));
w.addDocument(doc);

IndexReader r = DirectoryReader.open(w);
w.close();
IndexSearcher searcher = new IndexSearcher(r);
searcher.setSimilarity(sim);
Query query = new BoostQuery(new BooleanQuery.Builder()
.add(new TermQuery(new Term("f", "foo")), Occur.SHOULD)
.add(new TermQuery(new Term("match", "yes")), Occur.FILTER)
.build(), 3.2f);
Query query = new BoostQuery(new TermQuery(new Term("f", "foo")), 3.2f);
TopDocs topDocs = searcher.search(query, 1);
assertEquals(1, topDocs.totalHits.value);
assertTrue(initCalled.get());
Expand Down

0 comments on commit 8a6505d

Please sign in to comment.