Skip to content

Commit 27a045f

Browse files
committed
Better cache of weights by scorer
1 parent 051ea58 commit 27a045f

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

common/src/java/com/github/oeuvres/alix/lucene/terms/FieldStats.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ public final class FieldStats implements ReferenceStats
162162
* Written once and then read-only; declared volatile for safe publication.
163163
*/
164164
private volatile double[] termWeights;
165-
165+
/** The scorer used to calculate termeights */
166+
private volatile TermScorer termWeightsScorer;
166167
/** Number of distinct terms in the field. */
167168
private final int vocabSize;
168169

@@ -266,7 +267,9 @@ public void buildWeights(final IndexReader reader, final TermScorer scorer) thro
266267
throw new IllegalStateException(
267268
"Field '" + field + "' was not indexed with term frequencies");
268269
}
269-
270+
// weights already calculated with same scorer
271+
if (scorer.equals(termWeightsScorer)) return;
272+
270273
scorer.corpus(fieldTokens, fieldDocs);
271274

272275
final double[] weights = new double[vocabSize];

common/src/java/com/github/oeuvres/alix/lucene/terms/TermScorer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ public String toString()
149149
{
150150
return this.getClass().getSimpleName();
151151
}
152+
153+
@Override
154+
public boolean equals(final Object o) {
155+
return o != null && o.getClass() == this.getClass();
156+
}
157+
158+
@Override
159+
public int hashCode() {
160+
return getClass().hashCode();
161+
}
162+
152163
/**
153164
* Signed G-test contribution against the corpus expectation.
154165
*
@@ -270,5 +281,17 @@ public String toString()
270281
{
271282
return "BM25 " + idfExp;
272283
}
284+
285+
@Override
286+
public boolean equals(final Object o) {
287+
if (this == o) return true;
288+
if (!(o instanceof BM25 other)) return false;
289+
return Double.compare(this.idfExp, other.idfExp) == 0;
290+
}
291+
292+
@Override
293+
public int hashCode() {
294+
return Double.hashCode(idfExp);
295+
}
273296
}
274297
}

web/src/main/java/com/github/oeuvres/alix/web/Op.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ else switch (format) {
7373
protected void page(LuceneIndex index,
7474
HttpServletRequest req, HttpServletResponse resp) throws IOException
7575
{
76-
AlixServlet.sendError(resp, 406, name() + ": page not implemented");
76+
AlixServlet.sendError(resp, 406, name() + ": default html not implemented");
7777
}
7878

7979
/** Structured JSON. */
@@ -87,7 +87,7 @@ protected void json(LuceneIndex index,
8787
protected void html(LuceneIndex index,
8888
HttpServletRequest req, HttpServletResponse resp) throws IOException
8989
{
90-
AlixServlet.sendError(resp, 406, name() + ": html not implemented");
90+
AlixServlet.sendError(resp, 406, name() + ": html fragment not implemented");
9191
}
9292

9393
/** JSON Lines — one object per line. */

web/src/main/java/com/github/oeuvres/alix/web/OpTerms.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public final class OpTerms extends Op
5656

5757
/** Default BM25 IDF exponent. */
5858
private static final double DEFAULT_IDF_EXP = 1d;
59+
5960

6061
@Override
6162
public String name() { return "terms"; }
@@ -73,8 +74,8 @@ protected void json(
7374
TopTerms topTerms;
7475
final int topK = pars.getInt("top", TOP_RANGE, DEFAULT_TOP);
7576
final String q = pars.getString("q", null);
77+
final double idfExp = pars.getDouble("idfexp", DEFAULT_IDF_EXP);
7678

77-
final double idfExp;
7879
final long t0 = System.nanoTime();
7980

8081
if (q != null) {
@@ -91,7 +92,6 @@ protected void json(
9192
"terms: field '" + field + "' not found or not a text field");
9293
return;
9394
}
94-
idfExp = pars.getDouble("idfExp", DEFAULT_IDF_EXP);
9595
final TermScorer scorer = new TermScorer.BM25(idfExp);
9696
FieldStats fieldStats = fluc.fieldStats();
9797
fieldStats.buildWeights(lucene.reader(), scorer);
@@ -108,15 +108,15 @@ protected void json(
108108
jw.name("meta");
109109
jw.beginObject();
110110
jw.name("time").value(qTime);
111-
jw.name("params");
111+
jw.name("params"); // start params
112112
jw.beginObject();
113113
jw.name("field").value(field);
114114
jw.name("top").value(topK);
115+
jw.name("idfexp").value(idfExp);
115116
if (q != null) {
116117
jw.name("q").value(q);
117118
}
118119
else {
119-
jw.name("idfExp").value(idfExp);
120120
}
121121
jw.endObject(); // params
122122
jw.endObject(); // meta

0 commit comments

Comments
 (0)