Skip to content

Commit 084242e

Browse files
committed
Can now use search options in ML12 for BM25, Zero, and Random.
Also adds a setting for BM25 Length Weight. Added a couple of tests. These tests are of limited use here, but they do verify that "bm25" and "zero" are acceptable scoring options.
1 parent 57e9764 commit 084242e

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/PlanBuilderBaseImpl.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ static class PlanSearchOptionsImpl implements PlanSearchOptions {
6363
private PlanBuilderBaseImpl pb;
6464
private XsDoubleVal qualityWeight;
6565
private ScoreMethod scoreMethod;
66+
private XsDoubleVal lengthWeight;
6667
PlanSearchOptionsImpl(PlanBuilderBaseImpl pb) {
6768
this.pb = pb;
6869
}
6970
PlanSearchOptionsImpl(PlanBuilderBaseImpl pb, XsDoubleVal qualityWeight, ScoreMethod scoreMethod) {
70-
this(pb);
71-
this.qualityWeight = qualityWeight;
72-
this.scoreMethod = scoreMethod;
71+
new PlanSearchOptionsImpl(pb, qualityWeight, scoreMethod, null);
7372
}
73+
PlanSearchOptionsImpl(PlanBuilderBaseImpl pb, XsDoubleVal qualityWeight,
74+
ScoreMethod scoreMethod, XsDoubleVal lengthWeight) {
75+
this(pb);
76+
this.qualityWeight = qualityWeight;
77+
this.scoreMethod = scoreMethod;
78+
this.lengthWeight = lengthWeight;
79+
}
7480

7581
@Override
7682
public XsDoubleVal getQualityWeight() {
@@ -80,19 +86,28 @@ public XsDoubleVal getQualityWeight() {
8086
public ScoreMethod getScoreMethod() {
8187
return scoreMethod;
8288
}
89+
public XsDoubleVal getLengthWeight() {
90+
return lengthWeight;
91+
}
8392
@Override
8493
public PlanSearchOptions withQualityWeight(double qualityWeight) {
8594
return withQualityWeight(pb.xs.doubleVal(qualityWeight));
8695
}
8796
@Override
8897
public PlanSearchOptions withQualityWeight(XsDoubleVal qualityWeight) {
89-
return new PlanSearchOptionsImpl(pb, qualityWeight, getScoreMethod());
98+
return new PlanSearchOptionsImpl(pb, qualityWeight, getScoreMethod(), getLengthWeight());
9099
}
91100
@Override
92101
public PlanSearchOptions withScoreMethod(ScoreMethod scoreMethod) {
93-
return new PlanSearchOptionsImpl(pb, getQualityWeight(), scoreMethod);
102+
return new PlanSearchOptionsImpl(pb, getQualityWeight(), scoreMethod, getLengthWeight());
94103
}
95-
Map<String,String> makeMap() {
104+
105+
@Override
106+
public PlanSearchOptions withBm25LengthWeight(double lengthWeight) {
107+
return new PlanSearchOptionsImpl(pb, getQualityWeight(), getScoreMethod(), pb.xs.doubleVal(lengthWeight));
108+
}
109+
110+
Map<String,String> makeMap() {
96111
if (qualityWeight == null && scoreMethod == null) return null;
97112

98113
Map<String, String> map = new HashMap<String, String>();
@@ -102,6 +117,9 @@ Map<String,String> makeMap() {
102117
if (scoreMethod != null) {
103118
map.put("scoreMethod", scoreMethod.name().toLowerCase());
104119
}
120+
if (lengthWeight != null) {
121+
map.put("lengthWeight", String.valueOf(lengthWeight));
122+
}
105123
return map;
106124
}
107125
}

marklogic-client-api/src/main/java/com/marklogic/client/type/PlanSearchOptions.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.marklogic.client.type;
1717

18-
// IMPORTANT: Do not edit. This file is generated.
19-
2018
/**
2119
* An option controlling the scoring and weighting of fromSearch()
2220
* for a row pipeline.
@@ -27,7 +25,8 @@ public interface PlanSearchOptions {
2725
PlanSearchOptions withQualityWeight(double qualityWeight);
2826
PlanSearchOptions withQualityWeight(XsDoubleVal qualityWeight);
2927
PlanSearchOptions withScoreMethod(ScoreMethod scoreMethod);
28+
PlanSearchOptions withBm25LengthWeight(double lengthWeight);
3029
enum ScoreMethod {
31-
LOGTFIDF, LOGTF, SIMPLE;
30+
LOGTFIDF, LOGTF, SIMPLE, BM25, ZERO, RANDOM;
3231
}
3332
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.marklogic.client.test.junit5;
2+
3+
import com.marklogic.client.test.Common;
4+
import com.marklogic.client.test.MarkLogicVersion;
5+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
6+
import org.junit.jupiter.api.extension.ExecutionCondition;
7+
import org.junit.jupiter.api.extension.ExtensionContext;
8+
9+
public class RequiresML12 implements ExecutionCondition {
10+
11+
private static MarkLogicVersion markLogicVersion;
12+
13+
@Override
14+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
15+
if (markLogicVersion == null) {
16+
markLogicVersion = Common.getMarkLogicVersion();
17+
}
18+
return markLogicVersion.getMajor() >= 12 ?
19+
ConditionEvaluationResult.enabled("MarkLogic is version 12 or higher") :
20+
ConditionEvaluationResult.disabled("MarkLogic is version 11 or lower");
21+
}
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.marklogic.client.test.rows;
2+
3+
import com.marklogic.client.row.RowRecord;
4+
import com.marklogic.client.test.junit5.RequiresML12;
5+
import com.marklogic.client.type.PlanSearchOptions;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
9+
import java.util.List;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
@ExtendWith(RequiresML12.class)
14+
class OpticPlanSearchOptionsTest extends AbstractOpticUpdateTest {
15+
16+
@Test
17+
void bm25() {
18+
PlanSearchOptions options = op.searchOptions()
19+
.withScoreMethod(PlanSearchOptions.ScoreMethod.BM25)
20+
.withBm25LengthWeight(20.5);
21+
List<RowRecord> rows = resultRows(
22+
op.fromSearch(op.cts.wordQuery("contents"), null, null, options)
23+
.offsetLimit(0, 5)
24+
);
25+
assertEquals(5, rows.size());
26+
}
27+
28+
@Test
29+
void zero() {
30+
PlanSearchOptions options = op.searchOptions().withScoreMethod(PlanSearchOptions.ScoreMethod.ZERO);
31+
List<RowRecord> rows = resultRows(
32+
op.fromSearch(op.cts.wordQuery("contents"), null, null, options)
33+
.offsetLimit(0, 5)
34+
);
35+
assertEquals(5, rows.size());
36+
rows.forEach(row -> {
37+
assertEquals(0, row.getInt("score"), "The score for every row should be 0.");
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)