Skip to content

Commit 8048ee2

Browse files
authored
Merge pull request #1723 from marklogic/feature/17044-structured-queries
MLE-17044 Added trueQuery, falseQuery, and operatorState
2 parents 20867ce + 49ae6fe commit 8048ee2

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,39 @@ public StructuredQueryDefinition andNot(StructuredQueryDefinition positive, Stru
336336
return new AndNotQuery(positive, negative);
337337
}
338338

339+
/**
340+
* Defines a "true" query that selects all documents.
341+
*
342+
* @since 7.1.0
343+
* @return the StructuredQueryDefinition for the "true" query.
344+
*/
345+
public StructuredQueryDefinition trueQuery() {
346+
return new TrueQuery();
347+
}
348+
349+
/**
350+
* Defines a "true" query that selects all documents.
351+
*
352+
* @since 7.1.0
353+
* @return the StructuredQueryDefinition for the "true" query.
354+
*/
355+
public StructuredQueryDefinition falseQuery() {
356+
return new FalseQuery();
357+
}
358+
359+
/**
360+
* Define a new operator state; see https://docs.marklogic.com/guide/search-dev/structured-query#id_45570 for
361+
* more information.
362+
*
363+
* @param operatorName The name of a custom runtime configuration operator defined by the <operator/> query option.
364+
* @param stateName The name of a state recognized by this operator.
365+
* @since 7.1.0
366+
* @return the StructuredQueryDefinition for the operator state.
367+
*/
368+
public StructuredQueryDefinition operatorState(String operatorName, String stateName) {
369+
return new OperatorState(operatorName, stateName);
370+
}
371+
339372
/**
340373
* Defines a NEAR query over the list of query definitions
341374
* with default parameters.
@@ -1289,6 +1322,43 @@ public void innerSerialize(XMLStreamWriter serializer) throws XMLStreamException
12891322
}
12901323
}
12911324

1325+
protected class TrueQuery extends AbstractStructuredQuery {
1326+
@Override
1327+
public void innerSerialize(XMLStreamWriter serializer) throws XMLStreamException {
1328+
serializer.writeEmptyElement(SEARCH_API_NS, "true-query");
1329+
}
1330+
}
1331+
1332+
protected class FalseQuery extends AbstractStructuredQuery {
1333+
@Override
1334+
public void innerSerialize(XMLStreamWriter serializer) throws XMLStreamException {
1335+
serializer.writeEmptyElement(SEARCH_API_NS, "false-query");
1336+
}
1337+
}
1338+
1339+
protected class OperatorState extends AbstractStructuredQuery {
1340+
1341+
private final String operatorName;
1342+
private final String stateName;
1343+
public OperatorState(String operatorName, String stateName) {
1344+
super();
1345+
this.operatorName = operatorName;
1346+
this.stateName = stateName;
1347+
}
1348+
1349+
@Override
1350+
public void innerSerialize(XMLStreamWriter serializer) throws XMLStreamException {
1351+
writeSearchElement(serializer, "operator-state");
1352+
writeSearchElement(serializer, "operator-name");
1353+
serializer.writeCharacters(operatorName);
1354+
serializer.writeEndElement();
1355+
writeSearchElement(serializer, "state-name");
1356+
serializer.writeCharacters(stateName);
1357+
serializer.writeEndElement();
1358+
serializer.writeEndElement();
1359+
}
1360+
}
1361+
12921362
protected class AndNotQuery
12931363
extends AbstractStructuredQuery {
12941364
private StructuredQueryDefinition positive;

marklogic-client-api/src/test/java/com/marklogic/client/test/StructuredSearchTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,75 @@
1414
import com.marklogic.client.io.StringHandle;
1515
import com.marklogic.client.query.*;
1616
import com.marklogic.client.util.EditableNamespaceContext;
17+
import org.jdom2.Namespace;
18+
import org.jdom2.input.SAXBuilder;
1719
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.BeforeEach;
1821
import org.junit.jupiter.api.Test;
1922
import org.w3c.dom.Document;
2023
import org.w3c.dom.Element;
2124
import org.xml.sax.SAXException;
2225

2326
import java.io.IOException;
27+
import java.io.StringReader;
2428
import java.util.concurrent.atomic.AtomicInteger;
2529

2630
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
2731
import static org.junit.jupiter.api.Assertions.*;
2832

2933
public class StructuredSearchTest {
3034

35+
private QueryManager queryManager;
36+
private StructuredQueryBuilder queryBuilder;
37+
3138
@BeforeAll
3239
public static void beforeClass() {
3340
Common.connect();
3441
}
3542

43+
@BeforeEach
44+
void setup() {
45+
queryManager = Common.client.newQueryManager();
46+
queryBuilder = queryManager.newStructuredQueryBuilder();
47+
}
48+
49+
@Test
50+
void trueQuery() {
51+
long total = queryManager.search(queryBuilder.and(
52+
queryBuilder.trueQuery(),
53+
queryBuilder.collection("zipcode")
54+
), new SearchHandle()).getTotalResults();
55+
assertEquals(2, total, "Expecting 2 documents in the zipcode collection, and the trueQuery should not affect that.");
56+
57+
total = queryManager.search(queryBuilder.trueQuery(), new SearchHandle()).getTotalResults();
58+
assertTrue(total > 2, "Expecting all documents to be returned, which should be more than the 2 in the " +
59+
"zipcode collection. Actual count: " + total);
60+
}
61+
62+
@Test
63+
void falseQuery() {
64+
long total = queryManager.search(queryBuilder.and(
65+
queryBuilder.falseQuery(),
66+
queryBuilder.collection("zipcode")
67+
), new SearchHandle()).getTotalResults();
68+
assertEquals(0, total, "Expecting 0 documents due to the false query.");
69+
70+
total = queryManager.search(queryBuilder.falseQuery(), new SearchHandle()).getTotalResults();
71+
assertEquals(0, total, "A false-query should return zero documents.");
72+
}
73+
74+
@Test
75+
void operatorState() throws Exception {
76+
StructuredQueryDefinition query = queryBuilder.operatorState("sort", "date");
77+
String xml = query.serialize();
78+
79+
org.jdom2.Document doc = new SAXBuilder().build(new StringReader(xml));
80+
Namespace ns = Namespace.getNamespace("http://marklogic.com/appservices/search");
81+
org.jdom2.Element operatorState = doc.getRootElement().getChild("operator-state", ns);
82+
assertEquals("sort", operatorState.getChildText("operator-name", ns));
83+
assertEquals("date", operatorState.getChildText("state-name", ns));
84+
}
85+
3686
@Test
3787
public void testStructuredSearch() {
3888
QueryManager queryMgr = Common.client.newQueryManager();

0 commit comments

Comments
 (0)