Skip to content

Commit 9da93fc

Browse files
llingllinggit
lling
authored andcommitted
Add CtsQueryBuilder for search in Java API marklogic#1260
1 parent 81aae5f commit 9da93fc

20 files changed

+596
-189
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/document/DocumentManager.java

+27-26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.marklogic.client.FailedRequestException;
2020
import com.marklogic.client.ForbiddenUserException;
2121
import com.marklogic.client.io.Format;
22+
import com.marklogic.client.query.SearchQueryDefinition;
2223
import com.marklogic.client.util.RequestLogger;
2324
import com.marklogic.client.ResourceNotFoundException;
2425
import com.marklogic.client.Transaction;
@@ -582,7 +583,7 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
582583
DocumentPage readMetadata(Transaction transaction, String... uris);
583584

584585
/**
585-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long) QueryManager.search}
586+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long) QueryManager.search}
586587
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
587588
* documents in each DocumentPage. If setMetadataCategories has
588589
* been called, populates metadata for each result in the format specified by
@@ -591,10 +592,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
591592
* @param start the offset of the first document in the page (where 1 is the first result)
592593
* @return the DocumentPage of matching documents and metadata
593594
*/
594-
DocumentPage search(QueryDefinition querydef, long start);
595+
DocumentPage search(SearchQueryDefinition querydef, long start);
595596

596597
/**
597-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, String) QueryManager.search}
598+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, String) QueryManager.search}
598599
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
599600
* documents in each DocumentPage. If setMetadataCategories has
600601
* been called, populates metadata for each result in the format specified by
@@ -604,10 +605,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
604605
* @param forestName a forest to limit this search
605606
* @return the DocumentPage of matching documents and metadata
606607
*/
607-
DocumentPage search(QueryDefinition querydef, long start, String forestName);
608+
DocumentPage search(SearchQueryDefinition querydef, long start, String forestName);
608609

609610
/**
610-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long) QueryManager.search}
611+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long) QueryManager.search}
611612
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
612613
* documents in each DocumentPage. If setMetadataCategories has
613614
* been called, populates metadata for each result in the format specified by
@@ -620,11 +621,11 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
620621
* @return the DocumentPage of matching documents and metadata
621622
*/
622623
/* Hide the following for now because the API isn't yet fully fleshed-out
623-
DocumentPage search(QueryDefinition querydef, long start, long serverTimestamp);
624+
DocumentPage search(SearchQueryDefinition querydef, long start, long serverTimestamp);
624625
*/
625626

626627
/**
627-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction) QueryManager.search}
628+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, Transaction) QueryManager.search}
628629
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
629630
* documents in each DocumentPage. If setMetadataCategories has
630631
* been called, populates metadata for each result in the format specified by
@@ -634,10 +635,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
634635
* @param transaction an open transaction for matching documents
635636
* @return the DocumentPage of matching documents and metadata
636637
*/
637-
DocumentPage search(QueryDefinition querydef, long start, Transaction transaction);
638+
DocumentPage search(SearchQueryDefinition querydef, long start, Transaction transaction);
638639

639640
/**
640-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction, String) QueryManager.search}
641+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, Transaction, String) QueryManager.search}
641642
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
642643
* documents in each DocumentPage. If setMetadataCategories has
643644
* been called, populates metadata for each result in the format specified by
@@ -648,10 +649,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
648649
* @param forestName a forest to limit this search
649650
* @return the DocumentPage of matching documents and metadata
650651
*/
651-
DocumentPage search(QueryDefinition querydef, long start, Transaction transaction, String forestName);
652+
DocumentPage search(SearchQueryDefinition querydef, long start, Transaction transaction, String forestName);
652653

653654
/**
654-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction) QueryManager.search}
655+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, Transaction) QueryManager.search}
655656
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
656657
* documents in each DocumentPage. If setMetadataCategories has
657658
* been called, populates metadata for each result in the format specified by
@@ -665,11 +666,11 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
665666
* @return the DocumentPage of matching documents and metadata
666667
*/
667668
/* Hide the following for now because the API isn't yet fully fleshed-out
668-
DocumentPage search(QueryDefinition querydef, long start, long serverTimestamp, Transaction transaction);
669+
DocumentPage search(SearchQueryDefinition querydef, long start, long serverTimestamp, Transaction transaction);
669670
*/
670671

671672
/**
672-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long) QueryManager.search}
673+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long) QueryManager.search}
673674
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
674675
* documents in each DocumentPage. If searchHandle is not null,
675676
* requests a search response and populates searchHandle with it. If setMetadataCategories has
@@ -682,10 +683,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
682683
* {@link #setNonDocumentFormat setNonDocumentFormat}
683684
* @return the DocumentPage of matching documents and metadata
684685
*/
685-
DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle);
686+
DocumentPage search(SearchQueryDefinition querydef, long start, SearchReadHandle searchHandle);
686687

687688
/**
688-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, String) QueryManager.search}
689+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, String) QueryManager.search}
689690
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
690691
* documents in each DocumentPage. If searchHandle is not null,
691692
* requests a search response and populates searchHandle with it. If setMetadataCategories has
@@ -699,10 +700,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
699700
* @param forestName a forest to limit this search
700701
* @return the DocumentPage of matching documents and metadata
701702
*/
702-
DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, String forestName);
703+
DocumentPage search(SearchQueryDefinition querydef, long start, SearchReadHandle searchHandle, String forestName);
703704

704705
/**
705-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction)}
706+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, Transaction)}
706707
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
707708
* documents in each DocumentPage. If searchHandle is not null,
708709
* requests a search response and populates searchHandle with it. If setMetadataCategories has
@@ -716,10 +717,10 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
716717
* @param transaction an open transaction for matching documents
717718
* @return the DocumentPage of matching documents and metadata
718719
*/
719-
DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction);
720+
DocumentPage search(SearchQueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction);
720721

721722
/**
722-
* Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction, String)}
723+
* Just like {@link QueryManager#search(SearchQueryDefinition, SearchReadHandle, long, Transaction, String)}
723724
* but return complete documents via iterable DocumentPage. Retrieves up to getPageLength()
724725
* documents in each DocumentPage. If searchHandle is not null,
725726
* requests a search response and populates searchHandle with it. If setMetadataCategories has
@@ -734,7 +735,7 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
734735
* @param forestName a forest to limit this search
735736
* @return the DocumentPage of matching documents and metadata
736737
*/
737-
DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction, String forestName);
738+
DocumentPage search(SearchQueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction, String forestName);
738739

739740
/** Get the maximum number of records to return in a page from calls to {@link #search search}
740741
* @return the maximum number of records to return in a page from calls to
@@ -751,7 +752,7 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
751752

752753
/**
753754
* Returns the format (if set) for the search response from
754-
* {@link #search(QueryDefinition, long, SearchReadHandle) search} and
755+
* {@link #search(SearchQueryDefinition, long, SearchReadHandle) search} and
755756
* metadata available from {@link DocumentRecord#getMetadata(DocumentMetadataReadHandle)
756757
* DocumentPage.next().getMetadata(handle)} (assuming
757758
* {@link #setMetadataCategories setMetadataCategories} has been called
@@ -763,7 +764,7 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
763764

764765
/**
765766
* Specifies the format for the search response from
766-
* {@link #search(QueryDefinition, long, SearchReadHandle) search} and
767+
* {@link #search(SearchQueryDefinition, long, SearchReadHandle) search} and
767768
* metadata available from {@link DocumentRecord#getMetadata(DocumentMetadataReadHandle)
768769
* DocumentPage.next().getMetadata(handle)} (assuming
769770
* {@link #setMetadataCategories setMetadataCategories} has been called
@@ -775,17 +776,17 @@ <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadat
775776

776777
/**
777778
* Returns the view types included in a SearchReadHandle populated by calls to
778-
* {@link #search(QueryDefinition, long, SearchReadHandle) search}
779+
* {@link #search(SearchQueryDefinition, long, SearchReadHandle) search}
779780
* @return the view types included in a SearchReadHandle populated by calls to
780-
* {@link #search(QueryDefinition, long, SearchReadHandle) search}
781+
* {@link #search(SearchQueryDefinition, long, SearchReadHandle) search}
781782
*/
782783
QueryView getSearchView();
783784

784785
/**
785786
* Specifies the view types included in a SearchReadHandle populated by calls to
786-
* {@link #search(QueryDefinition, long, SearchReadHandle) search}
787+
* {@link #search(SearchQueryDefinition, long, SearchReadHandle) search}
787788
* @param view the view types included in a SearchReadHandle populated by calls to
788-
* {@link #search(QueryDefinition, long, SearchReadHandle) search}
789+
* {@link #search(SearchQueryDefinition, long, SearchReadHandle) search}
789790
*/
790791
void setSearchView(QueryView view);
791792

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2019 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.expression;
17+
18+
import com.marklogic.client.io.marker.JSONReadHandle;
19+
import com.marklogic.client.io.marker.JSONWriteHandle;
20+
import com.marklogic.client.query.CtsQueryDefinition;
21+
import com.marklogic.client.type.CtsQueryExpr;
22+
23+
public abstract class CtsQueryBuilder {
24+
25+
public final CtsExpr cts;
26+
public final FnExpr fn;
27+
public final GeoExpr geo;
28+
public final JsonExpr json;
29+
public final MapExpr map;
30+
public final MathExpr math;
31+
public final RdfExpr rdf;
32+
public final SemExpr sem;
33+
public final SpellExpr spell;
34+
public final SqlExpr sql;
35+
public final XdmpExpr xdmp;
36+
public final XsExpr xs;
37+
38+
protected CtsQueryBuilder(
39+
CtsExpr cts, FnExpr fn, GeoExpr geo, JsonExpr json, MapExpr map, MathExpr math, RdfExpr rdf, SemExpr sem,
40+
SpellExpr spell, SqlExpr sql, XdmpExpr xdmp, XsExpr xs
41+
) {
42+
this.cts = cts;
43+
this.fn = fn;
44+
this.geo = geo;
45+
this.json = json;
46+
this.map = map;
47+
this.math = math;
48+
this.rdf = rdf;
49+
this.sem = sem;
50+
this.spell = spell;
51+
this.sql = sql;
52+
this.xdmp = xdmp;
53+
this.xs = xs;
54+
}
55+
56+
57+
public abstract CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query);
58+
59+
public abstract CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query, JSONWriteHandle queryOptions);
60+
61+
public abstract <T extends JSONReadHandle> T export(CtsQueryExpr query, T handle);
62+
}

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,12 @@
1717

1818
import java.util.HashSet;
1919

20-
import com.marklogic.client.document.ServerTransform;
2120
import com.marklogic.client.query.QueryDefinition;
2221
import java.util.Set;
2322

24-
public abstract class AbstractQueryDefinition implements QueryDefinition {
25-
protected String optionsUri = null;
23+
public abstract class AbstractQueryDefinition extends AbstractSearchQueryDefinition implements QueryDefinition {
2624
private Set<String> collections = new HashSet<>();
2725
private String directory = null;
28-
private ServerTransform transform = null;
29-
30-
@Override
31-
public String getOptionsName() {
32-
return optionsUri;
33-
}
34-
@Override
35-
public void setOptionsName(String uri) {
36-
optionsUri = uri;
37-
}
3826

3927
@Override
4028
public String[] getCollections() {
@@ -58,12 +46,4 @@ public void setDirectory(String directory) {
5846
this.directory = directory;
5947
}
6048

61-
@Override
62-
public ServerTransform getResponseTransform() {
63-
return transform;
64-
}
65-
@Override
66-
public void setResponseTransform(ServerTransform transform) {
67-
this.transform = transform;
68-
}
6949
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2019 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import com.marklogic.client.document.ServerTransform;
19+
import com.marklogic.client.query.SearchQueryDefinition;
20+
21+
public abstract class AbstractSearchQueryDefinition implements SearchQueryDefinition {
22+
23+
protected String optionsUri = null;
24+
private ServerTransform transform = null;
25+
26+
@Override
27+
public String getOptionsName() {
28+
return optionsUri;
29+
}
30+
31+
@Override
32+
public void setOptionsName(String uri) {
33+
this.optionsUri = uri;
34+
}
35+
@Override
36+
public ServerTransform getResponseTransform() {
37+
return transform;
38+
}
39+
40+
@Override
41+
public void setResponseTransform(ServerTransform transform) {
42+
this.transform = transform;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2020 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import com.marklogic.client.expression.*;
19+
import com.marklogic.client.io.marker.JSONReadHandle;
20+
import com.marklogic.client.io.marker.JSONWriteHandle;
21+
import com.marklogic.client.query.CtsQueryDefinition;
22+
import com.marklogic.client.type.CtsQueryExpr;
23+
24+
public class CtsQueryBuilderImpl extends CtsQueryBuilder {
25+
CtsQueryBuilderImpl() {
26+
super(CtsExprImpl.cts,
27+
FnExprImpl.fn,
28+
GeoExprImpl.geo,
29+
JsonExprImpl.json,
30+
MapExprImpl.map,
31+
MathExprImpl.math,
32+
RdfExprImpl.rdf,
33+
SemExprImpl.sem,
34+
SpellExprImpl.spell,
35+
SqlExprImpl.sql,
36+
XdmpExprImpl.xdmp,
37+
XsExprImpl.xs);
38+
}
39+
40+
@Override
41+
public CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query) {
42+
return new CtsQueryDefinitionImpl(query);
43+
}
44+
45+
@Override
46+
public CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query, JSONWriteHandle queryOptions) {
47+
return new CtsQueryDefinitionImpl(query, queryOptions);
48+
}
49+
50+
@Override
51+
public <T extends JSONReadHandle> T export(CtsQueryExpr query, T handle) {
52+
Utilities.setHandleToString(handle, (new CtsQueryDefinitionImpl(query)).serialize());
53+
return handle;
54+
}
55+
}

0 commit comments

Comments
 (0)