Skip to content

Commit 561295c

Browse files
authored
Merge pull request #1673 from BillFarber/task/fromSearchDocs
Can now use search options with op.fromSearchDocs.
2 parents 3cff591 + 4ca28f1 commit 561295c

File tree

3 files changed

+102
-35
lines changed

3 files changed

+102
-35
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/expression/PlanBuilderBase.java

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public interface PlanBuilderBase {
3232
/**
3333
* Provides a convenience for matching documents and constructing rows with the score,
34-
* document uri, and document content. The convenience is equivalent to chaining
34+
* document URI, and document content. The convenience is equivalent to chaining
3535
* {@link PlanBuilder#fromSearch(CtsQueryExpr)},
3636
* {@link PlanBuilder.ModifyPlan#joinDocUri(String, String)},
3737
* and {@link PlanBuilder.ModifyPlan#joinDoc(String, String)}.
@@ -43,7 +43,7 @@ public interface PlanBuilderBase {
4343
PlanBuilder.AccessPlan fromSearchDocs(CtsQueryExpr query);
4444
/**
4545
* Provides a convenience for matching documents and constructing rows with the score,
46-
* document uri, and document content. The convenience is equivalent to chaining
46+
* document URI, and document content. The convenience is equivalent to chaining
4747
* {@link PlanBuilder#fromSearch(CtsQueryExpr)},
4848
* {@link PlanBuilder.ModifyPlan#joinDocUri(String, String)},
4949
* and {@link PlanBuilder.ModifyPlan#joinDoc(String, String)}.
@@ -54,12 +54,25 @@ public interface PlanBuilderBase {
5454
* @return a ModifyPlan object
5555
*/
5656
PlanBuilder.AccessPlan fromSearchDocs(CtsQueryExpr query, String qualifierName);
57+
/**
58+
* Provides a convenience for matching documents and constructing rows with the score,
59+
* document URI, and document content. The convenience is equivalent to chaining
60+
* {@link PlanBuilder#fromSearch(CtsQueryExpr)},
61+
* {@link PlanBuilder.ModifyPlan#joinDocUri(String, String)},
62+
* and {@link PlanBuilder.ModifyPlan#joinDoc(String, String)}.
63+
* <p>The documents can be ordered by the score and limited for the most relevant
64+
* documents.</p>
65+
* @param query The cts.query expression for matching the documents.
66+
* @param qualifierName Specifies a name for qualifying the column names similar to a view name.
67+
* @return a ModifyPlan object
68+
*/
69+
PlanBuilder.AccessPlan fromSearchDocs(CtsQueryExpr query, String qualifierName, PlanSearchOptions options);
5770
/**
5871
* Supports document matching and relevance by constructing rows with the document fragment id and
5972
* columns for relevance factors. Typically, the plan will join the rows on the document fragment id
6073
* with the content of documents or with rows, triples, or lexicons indexed on the documents (where
6174
* the lexicons include range indexes, the document URI lexicon, and the collection lexicon).
62-
* <p>The documents can be ordered by the score or other relevance factors and limited for the
75+
* <p>The documents can be ordered by the score or other relevance factors and limited for the
6376
* most relevant documents.</p>
6477
* <p>By default, the rows provide fragmentId and score columns.
6578
* </p>
@@ -72,7 +85,7 @@ public interface PlanBuilderBase {
7285
* columns for relevance factors. Typically, the plan will join the rows on the document fragment id
7386
* with the content of documents or with rows, triples, or lexicons indexed on the documents (where
7487
* the lexicons include range indexes, the document URI lexicon, and the collection lexicon).
75-
* <p>The documents can be ordered by the score or other relevance factors and limited for the
88+
* <p>The documents can be ordered by the score or other relevance factors and limited for the
7689
* most relevant documents.</p>
7790
* <p>The list of possible columns to project consists of fragmentId, confidence, fitness,
7891
* quality, and score. The columns other than the fragmentId provide the same relevance factors
@@ -93,7 +106,7 @@ public interface PlanBuilderBase {
93106
* columns for relevance factors. Typically, the plan will join the rows on the document fragment id
94107
* with the content of documents or with rows, triples, or lexicons indexed on the documents (where
95108
* the lexicons include range indexes, the document URI lexicon, and the collection lexicon).
96-
* <p>The documents can be ordered by the score or other relevance factors and limited for the
109+
* <p>The documents can be ordered by the score or other relevance factors and limited for the
97110
* most relevant documents.</p>
98111
* <p>The list of possible columns to project consists of fragmentId, confidence, fitness,
99112
* quality, and score. The columns other than the fragmentId provide the same relevance factors
@@ -115,7 +128,7 @@ public interface PlanBuilderBase {
115128
* columns for relevance factors. Typically, the plan will join the rows on the document fragment id
116129
* with the content of documents or with rows, triples, or lexicons indexed on the documents (where
117130
* the lexicons include range indexes, the document URI lexicon, and the collection lexicon).
118-
* <p>The documents can be ordered by the score or other relevance factors and limited for the
131+
* <p>The documents can be ordered by the score or other relevance factors and limited for the
119132
* most relevant documents.</p>
120133
* <p>The list of possible columns to project consists of fragmentId, confidence, fitness,
121134
* quality, and score. The columns other than the fragmentId provide the same relevance factors
@@ -135,7 +148,7 @@ public interface PlanBuilderBase {
135148
PlanBuilder.AccessPlan fromSearch(CtsQueryExpr query, PlanExprColSeq columns, XsStringVal qualifierName, PlanSearchOptions options);
136149

137150
/**
138-
* Constructs a literal row set as in the SQL VALUES or SPARQL VALUES statements.
151+
* Constructs a literal row set as in the SQL VALUES or SPARQL VALUES statements.
139152
* @param rows This parameter provides any number of objects in which the key is a column name string identifying the column and the value is a literal with the value of the column.
140153
* @return an AccessPlan object
141154
*/
@@ -169,7 +182,7 @@ public interface PlanBuilderBase {
169182
PlanBuilder.AccessPlan fromDocUris(CtsQueryExpr querydef, String qualifierName);
170183
/**
171184
* Convenience method for constructing an {@code AccessPlan} based on a given set of URIs.
172-
*
185+
*
173186
* @param uris one or more URIs to pass into a {@code cts.documentQuery}
174187
* @return an AccessPlanObject
175188
*/
@@ -422,7 +435,7 @@ public interface PlanBuilderBase {
422435

423436
/**
424437
* Construct a mapping of document descriptor column names to columns in the plan. The available set of
425-
* document descriptor names are: uri, doc, collections, permissions, metadata, quality, and temporalCollection.
438+
* document descriptor names are: uri, doc, collections, permissions, metadata, quality, and temporalCollection.
426439
* Use this when mapping to non-standard column names.
427440
*
428441
* @param descriptorColumnMapping contains the mapping for column names to String values.
@@ -448,7 +461,7 @@ public interface PlanBuilderBase {
448461

449462
/**
450463
* Build a transform definition for use with {@code transformDoc}.
451-
*
464+
*
452465
* @param path the path (URI) of either a *.mjs or *.xslt module in a modules database
453466
* @return a new {@code TransformDef}
454467
*/
@@ -734,19 +747,19 @@ interface AccessPlanBase {
734747
*/
735748
interface ExportablePlanBase {
736749
/**
737-
* This method exports the plan to an AST (Abstract Sytax Tree)
750+
* This method exports the plan to an AST (Abstract Sytax Tree)
738751
* using the specified JSON handle.
739752
* @param handle a handle for reading the AST as JSON
740753
* @param <T> the type of the handle for reading the AST as JSON
741754
* @return the JSON handle populated with the AST
742755
*/
743756
<T extends JSONReadHandle> T export(T handle);
744757
/**
745-
* This method exports the plan to an AST (Abstract Sytax Tree)
758+
* This method exports the plan to an AST (Abstract Sytax Tree)
746759
* using the specified JSON handle.
747760
* <p>
748761
* The IO class must have been registered before creating the database client.
749-
* By default, the provided handles that implement
762+
* By default, the provided handles that implement
750763
* {@link com.marklogic.client.io.marker.ContentHandle ContentHandle} are registered.
751764
* <p>
752765
* <a href="../../../../overview-summary.html#ShortcutMethods">Learn more about shortcut methods</a>
@@ -887,19 +900,19 @@ interface ModifyPlanBase {
887900

888901
/**
889902
* This method returns a subset of the rows in the result set by returning the specified number of rows.
890-
* @param length The number of rows to return.
903+
* @param length The number of rows to return.
891904
* @return a ModifyPlan object
892905
*/
893906
PlanBuilder.ModifyPlan limit(long length);
894907
/**
895908
* This method returns a subset of the rows in the result set by returning the specified number of rows.
896-
* @param length The number of rows to return.
909+
* @param length The number of rows to return.
897910
* @return a ModifyPlan object
898911
*/
899912
PlanBuilder.ModifyPlan limit(XsLongVal length);
900913
/**
901914
* This method returns a subset of the rows in the result set by returning the specified number of rows.
902-
* @param length The number of rows to return.
915+
* @param length The number of rows to return.
903916
* @return a ModifyPlan object
904917
*/
905918
PlanBuilder.ModifyPlan limit(PlanParamExpr length);
@@ -916,38 +929,38 @@ interface ModifyPlanBase {
916929
PlanBuilder.ModifyPlan lockForUpdate(PlanColumn uriColumn);
917930
/**
918931
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the number specified by the prototype.limit method.
919-
* @param start The number of rows to skip.
932+
* @param start The number of rows to skip.
920933
* @return a ModifyPlan object
921934
*/
922935
PlanBuilder.ModifyPlan offset(long start);
923936
/**
924937
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the number specified by the prototype.limit method.
925-
* @param start The number of rows to skip.
938+
* @param start The number of rows to skip.
926939
* @return a ModifyPlan object
927940
*/
928941
PlanBuilder.ModifyPlan offset(XsLongVal start);
929942
/**
930943
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the number specified by the prototype.limit method.
931-
* @param start The number of rows to skip.
944+
* @param start The number of rows to skip.
932945
* @return a ModifyPlan object
933946
*/
934947
PlanBuilder.ModifyPlan offset(PlanParamExpr start);
935948
/**
936-
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the length limit. The offset for the next subset of rows is start + length.
937-
* @param start The number of rows to skip.
938-
* @param length The number of rows to return.
949+
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the length limit. The offset for the next subset of rows is start + length.
950+
* @param start The number of rows to skip.
951+
* @param length The number of rows to return.
939952
* @return a ModifyPlan object
940953
*/
941954
PlanBuilder.ModifyPlan offsetLimit(long start, long length);
942955
/**
943-
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the length limit. The offset for the next subset of rows is start + length.
944-
* @param start The number of rows to skip.
945-
* @param length The number of rows to return.
956+
* This method returns a subset of the rows in the result set by skipping the number of rows specified by start and returning the remaining rows up to the length limit. The offset for the next subset of rows is start + length.
957+
* @param start The number of rows to skip.
958+
* @param length The number of rows to return.
946959
* @return a ModifyPlan object
947960
*/
948961
PlanBuilder.ModifyPlan offsetLimit(XsLongVal start, XsLongVal length);
949962
/**
950-
* Removes (deletes) any document with a URI matching the value of the "uri" column in at least one row in the
963+
* Removes (deletes) any document with a URI matching the value of the "uri" column in at least one row in the
951964
* pipeline.
952965
* @return a ModifyPlan object
953966
*/
@@ -960,9 +973,9 @@ interface ModifyPlanBase {
960973
*/
961974
PlanBuilder.ModifyPlan remove(PlanColumn uriColumn);
962975
/**
963-
* Removes (deletes) any temporal document with a URI matching the value of the given column in at least one
964-
* row in the pipeline. Results in each temporal document being marked as deleted.
965-
*
976+
* Removes (deletes) any temporal document with a URI matching the value of the given column in at least one
977+
* row in the pipeline. Results in each temporal document being marked as deleted.
978+
*
966979
* @param temporalCollection the name of the temporal collection containing URIs to remove
967980
* @param uriColumn the column containing URIs to be removed
968981
* @return a ModifyPlan object
@@ -971,20 +984,20 @@ interface ModifyPlanBase {
971984
/**
972985
* Applies the given transformation to the content in the given column in each row. A {@code TransformDef}
973986
* can be constructed via {@code PlanBuilder#transformDef(String)}.
974-
*
987+
*
975988
* @param docColumn the column containing content to be transformed.
976989
* @param transformDef defines a transform for using with the {@code transformDoc} operator.
977990
* @return a ModifyPlan object
978991
*/
979992
PlanBuilder.ModifyPlan transformDoc(PlanColumn docColumn, TransformDef transformDef);
980993
/**
981994
* This method restricts the row set to rows matched by the boolean expression. Use boolean composers such as op.and and op.or to combine multiple expressions.
982-
* @param condition The boolean expression on which to match.
995+
* @param condition The boolean expression on which to match.
983996
* @return a ModifyPlan object
984997
*/
985998
PlanBuilder.ModifyPlan where(ServerExpression condition);
986999
/**
987-
* This method restricts the row set to rows from the documents matched by the cts.query expression.
1000+
* This method restricts the row set to rows from the documents matched by the cts.query expression.
9881001
* @param condition The cts.query expression for matching the documents.
9891002
* @return a ModifyPlan object
9901003
*/

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,18 @@ public PlanBuilder.AccessPlan fromSearchDocs(CtsQueryExpr query) {
6060
}
6161
@Override
6262
public PlanBuilder.AccessPlan fromSearchDocs(CtsQueryExpr query, String qualifierName) {
63-
return new AccessPlanSubImpl(
64-
this, "op", "from-search-docs", new Object[]{query, (qualifierName == null) ? null : xs.string(qualifierName)}
65-
);
63+
return fromSearchDocs(query, null, null);
64+
}
65+
@Override
66+
public PlanBuilder.AccessPlan fromSearchDocs(CtsQueryExpr query, String qualifierName, PlanSearchOptions options) {
67+
return new AccessPlanSubImpl(
68+
this, "op", "from-search-docs",
69+
new Object[]{
70+
query,
71+
(qualifierName == null) ? null : xs.string(qualifierName),
72+
asArg(makeMap(options))
73+
}
74+
);
6675
}
6776

6877
@Override
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.marklogic.client.test.rows;
2+
3+
import com.marklogic.client.FailedRequestException;
4+
import com.marklogic.client.expression.PlanBuilder;
5+
import com.marklogic.client.row.RowRecord;
6+
import com.marklogic.client.test.junit5.RequiresML12;
7+
import com.marklogic.client.type.PlanSearchOptions;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
@ExtendWith(RequiresML12.class)
16+
class FromSearchDocsWithOptionsTest extends AbstractOpticUpdateTest {
17+
18+
@Test
19+
void bm25() {
20+
// Note that this does not actually test that the scoring is correct.
21+
// It only tests that including the BM25 scoring option and a valid bm25LengthWeight do not cause any problems.
22+
rowManager.withUpdate(false);
23+
PlanSearchOptions options = op.searchOptions()
24+
.withScoreMethod(PlanSearchOptions.ScoreMethod.BM25)
25+
.withBm25LengthWeight(0.25);
26+
List<RowRecord> rows = resultRows(
27+
op.fromSearchDocs(op.cts.wordQuery("contents"), null, options)
28+
.offsetLimit(0, 5)
29+
);
30+
assertEquals(5, rows.size());
31+
}
32+
33+
@Test
34+
void qualityWeight() {
35+
// Note that this does not actually test that the scoring is correct.
36+
// It only tests that including a valid qualityWeight value does not cause any problems.
37+
rowManager.withUpdate(false);
38+
PlanSearchOptions options = op.searchOptions().withScoreMethod(PlanSearchOptions.ScoreMethod.LOGTFIDF).withQualityWeight(0.75F);
39+
List<RowRecord> rows = resultRows(
40+
op.fromSearchDocs(op.cts.wordQuery("contents"), null, options)
41+
.offsetLimit(0, 5)
42+
);
43+
assertEquals(5, rows.size());
44+
}
45+
}

0 commit comments

Comments
 (0)