Skip to content

Commit 3c4912f

Browse files
committed
fix #59, add support for typed value queries (required for json)
1 parent 7819f2e commit 3c4912f

File tree

4 files changed

+172
-10
lines changed

4 files changed

+172
-10
lines changed

src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ public StructuredQueryDefinition value(String pojoField, String... values) {
9595
return value(jsonProperty(pojoField), values);
9696
}
9797
}
98+
public StructuredQueryDefinition value(String pojoField, Boolean value) {
99+
if ( wrapQueries ) {
100+
return super.containerQuery(jsonProperty(classWrapper),
101+
value(jsonProperty(pojoField), value));
102+
} else {
103+
return value(jsonProperty(pojoField), value);
104+
}
105+
}
106+
public StructuredQueryDefinition value(String pojoField, Number... values) {
107+
if ( wrapQueries ) {
108+
return super.containerQuery(jsonProperty(classWrapper),
109+
value(jsonProperty(pojoField), values));
110+
} else {
111+
return value(jsonProperty(pojoField), values);
112+
}
113+
}
98114
public StructuredQueryDefinition value(String pojoField, String[] options,
99115
double weight, String... values)
100116
{
@@ -105,6 +121,26 @@ public StructuredQueryDefinition value(String pojoField, String[] options,
105121
return value(jsonProperty(pojoField), null, options, weight, values);
106122
}
107123
}
124+
public StructuredQueryDefinition value(String pojoField, String[] options,
125+
double weight, Boolean value)
126+
{
127+
if ( wrapQueries ) {
128+
return super.containerQuery(jsonProperty(classWrapper),
129+
value(jsonProperty(pojoField), null, options, weight, value));
130+
} else {
131+
return value(jsonProperty(pojoField), null, options, weight, value);
132+
}
133+
}
134+
public StructuredQueryDefinition value(String pojoField, String[] options,
135+
double weight, Number... values)
136+
{
137+
if ( wrapQueries ) {
138+
return super.containerQuery(jsonProperty(classWrapper),
139+
value(jsonProperty(pojoField), null, options, weight, values));
140+
} else {
141+
return value(jsonProperty(pojoField), null, options, weight, values);
142+
}
143+
}
108144
public StructuredQueryDefinition word(String pojoField, String... words) {
109145
if ( wrapQueries ) {
110146
return super.containerQuery(jsonProperty(classWrapper),

src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ public StructuredQueryDefinition range(String pojoField,
3838
public StructuredQueryDefinition range(String pojoField, String[] options,
3939
StructuredQueryBuilder.Operator operator, Object... values);
4040
public StructuredQueryDefinition value(String pojoField, String... values);
41+
public StructuredQueryDefinition value(String pojoField, Boolean value);
42+
public StructuredQueryDefinition value(String pojoField, Number... values);
4143
public StructuredQueryDefinition value(String pojoField, String[] options,
4244
double weight, String... values);
45+
public StructuredQueryDefinition value(String pojoField, String[] options,
46+
double weight, Boolean value);
47+
public StructuredQueryDefinition value(String pojoField, String[] options,
48+
double weight, Number... values);
4349
public StructuredQueryDefinition word(String pojoField, String... words);
4450
public StructuredQueryDefinition word(String pojoField, String[] options,
4551
double weight, String... words);

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

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,28 @@ public TermQuery term(double weight, String... terms) {
398398
public StructuredQueryDefinition value(TextIndex index, String... values) {
399399
return new ValueQuery(index, null, null, null, values);
400400
}
401+
/**
402+
* Matches an element, attribute, json key, or field
403+
* that has a value with the same boolean value as at least one
404+
* of the criteria values.
405+
* @param index the value container
406+
* @param value either true or false
407+
* @return the StructuredQueryDefinition for the value query
408+
*/
409+
public StructuredQueryDefinition value(TextIndex index, Boolean value) {
410+
return new ValueQuery(index, null, null, null, new Object[] {value});
411+
}
412+
/**
413+
* Matches an element, attribute, json key, or field
414+
* that has a value with the same numeric value as at least one
415+
* of the criteria values.
416+
* @param index the value container
417+
* @param values the possible values to match
418+
* @return the StructuredQueryDefinition for the value query
419+
*/
420+
public StructuredQueryDefinition value(TextIndex index, Number... values) {
421+
return new ValueQuery(index, null, null, null, values);
422+
}
401423
/**
402424
* Matches an element, attribute, json key, or field
403425
* that has a value with the same string value as at least one
@@ -412,6 +434,34 @@ public StructuredQueryDefinition value(TextIndex index, String... values) {
412434
public StructuredQueryDefinition value(TextIndex index, FragmentScope scope, String[] options, double weight, String... values) {
413435
return new ValueQuery(index, scope, options, weight, values);
414436
}
437+
/**
438+
* Matches an element, attribute, json key, or field
439+
* that has a value with the same boolean value as at least one
440+
* of the criteria values.
441+
* @param index the value container
442+
* @param scope whether the query matches the document content or properties
443+
* @param options options for fine tuning the query
444+
* @param weight the multiplier for the match in the document ranking
445+
* @param value either true or false
446+
* @return the StructuredQueryDefinition for the value query
447+
*/
448+
public StructuredQueryDefinition value(TextIndex index, FragmentScope scope, String[] options, double weight, Boolean value) {
449+
return new ValueQuery(index, scope, options, weight, new Object[] {value});
450+
}
451+
/**
452+
* Matches an element, attribute, json key, or field
453+
* that has a value with the same numeric value as at least one
454+
* of the criteria values.
455+
* @param index the value container
456+
* @param scope whether the query matches the document content or properties
457+
* @param options options for fine tuning the query
458+
* @param weight the multiplier for the match in the document ranking
459+
* @param values the possible values to match
460+
* @return the StructuredQueryDefinition for the value query
461+
*/
462+
public StructuredQueryDefinition value(TextIndex index, FragmentScope scope, String[] options, double weight, Number... values) {
463+
return new ValueQuery(index, scope, options, weight, values);
464+
}
415465

416466
/**
417467
* Matches an element, attribute, json key, or field
@@ -1636,16 +1686,48 @@ void innerSerialize(XMLStreamWriter serializer) throws Exception {
16361686
}
16371687

16381688
class ValueQuery
1639-
extends TextQuery {
1640-
ValueQuery(TextIndex index, FragmentScope scope,
1641-
String[] options, Double weight, String[] values) {
1642-
super(index, scope, options, weight, values);
1643-
}
1644-
@Override
1645-
void innerSerialize(XMLStreamWriter serializer) throws Exception {
1646-
serializer.writeStartElement("value-query");
1647-
super.innerSerialize(serializer);
1648-
serializer.writeEndElement();
1689+
extends AbstractStructuredQuery {
1690+
TextIndex index;
1691+
FragmentScope scope;
1692+
String[] options;
1693+
Double weight;
1694+
Object[] values;
1695+
ValueQuery(TextIndex index, FragmentScope scope,
1696+
String[] options, Double weight, Object[] values) {
1697+
this.index = index;
1698+
this.scope = scope;
1699+
this.options = options;
1700+
this.weight = weight;
1701+
this.values = values;
1702+
}
1703+
void innerSerialize(XMLStreamWriter serializer) throws Exception {
1704+
serializer.writeStartElement("value-query");
1705+
((IndexImpl) index).innerSerialize(serializer);
1706+
if (scope != null) {
1707+
if (scope == FragmentScope.DOCUMENT) {
1708+
writeText(serializer, "fragment-scope", "documents");
1709+
}
1710+
else {
1711+
writeText(serializer, "fragment-scope",
1712+
scope.toString().toLowerCase());
1713+
}
1714+
}
1715+
if ( values != null ) {
1716+
for ( Object value: values ) {
1717+
if ( value == null ) {
1718+
serializer.writeEmptyElement("null");
1719+
} else if ( value instanceof String ) {
1720+
writeText(serializer, "text", value);
1721+
} else if ( value instanceof Number ) {
1722+
writeText(serializer, "number", value);
1723+
} else if ( value instanceof Boolean ) {
1724+
writeText(serializer, "boolean", value);
1725+
}
1726+
}
1727+
}
1728+
writeTextList(serializer, "term-option", options);
1729+
writeText(serializer, "weight", weight);
1730+
serializer.writeEndElement();
16491731
}
16501732
}
16511733

src/test/java/com/marklogic/client/test/PojoFacadeTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,44 @@ public void testC_QueryPojos() throws Exception {
142142
assertEquals("Failed to find number of records expected", 11, numRead);
143143
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
144144

145+
// test numeric (integer) values
146+
query = qb.value("population", 374801);
147+
page = cities.search(query, 1);
148+
iterator = page.iterator();
149+
numRead = 0;
150+
while ( iterator.hasNext() ) {
151+
City city = iterator.next();
152+
assertEquals("Wrong City", "Tirana", city.getName());
153+
numRead++;
154+
}
155+
assertEquals("Failed to find number of records expected", 1, numRead);
156+
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
157+
158+
// test numeric (fractional) values
159+
query = qb.and(qb.value("latitude", -34.72418), qb.value("longitude", -58.25265));
160+
page = cities.search(query, 1);
161+
iterator = page.iterator();
162+
numRead = 0;
163+
while ( iterator.hasNext() ) {
164+
City city = iterator.next();
165+
assertEquals("Wrong City", "Quilmes", city.getName());
166+
numRead++;
167+
}
168+
assertEquals("Failed to find number of records expected", 1, numRead);
169+
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
170+
171+
// test null values
172+
query = qb.value("country", new String[] {null});
173+
page = cities.search(query, 1);
174+
iterator = page.iterator();
175+
numRead = 0;
176+
while ( iterator.hasNext() ) {
177+
City city = iterator.next();
178+
numRead++;
179+
}
180+
assertEquals("Failed to find number of records expected", 50, numRead);
181+
assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
182+
145183
query = qb.range("population", Operator.LT, 350000);
146184
page = cities.search(query, 1);
147185
iterator = page.iterator();

0 commit comments

Comments
 (0)