Skip to content

Commit d852f3b

Browse files
committed
fix #423 - add support for extract-resolution-time; test SearchHandle and SearchMetrics; deprecate SearchMetrics.getMetadataResolutionTime; fix indenting in StrinSearchTest to be spaces with no tabs mixed in
1 parent ae10e65 commit d852f3b

File tree

3 files changed

+120
-26
lines changed

3 files changed

+120
-26
lines changed

src/main/java/com/marklogic/client/io/SearchHandle.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,15 @@ static private class SearchMetricsImpl implements SearchMetrics {
461461
long frTime = -1;
462462
long srTime = -1;
463463
long mrTime = -1;
464+
long erTime = -1;
464465
long totalTime = -1;
465466

466-
public SearchMetricsImpl(long qrTime, long frTime, long srTime, long mrTime, long totalTime) {
467+
public SearchMetricsImpl(long qrTime, long frTime, long srTime, long mrTime, long erTime, long totalTime) {
467468
this.qrTime = qrTime;
468469
this.frTime = frTime;
469470
this.srTime = srTime;
470471
this.mrTime = mrTime;
472+
this.erTime = erTime;
471473
this.totalTime = totalTime;
472474
}
473475

@@ -491,6 +493,11 @@ public long getMetadataResolutionTime() {
491493
return mrTime;
492494
}
493495

496+
@Override
497+
public long getExtractResolutionTime() {
498+
return erTime;
499+
}
500+
494501
@Override
495502
public long getTotalTime() {
496503
return totalTime;
@@ -1505,13 +1512,15 @@ private void handleMetrics(XMLEventReader reader, StartElement element)
15051512
QName facetName = new QName(SEARCH_NS, "facet-resolution-time");
15061513
QName snippetName = new QName(SEARCH_NS, "snippet-resolution-time");
15071514
QName metadataName = new QName(SEARCH_NS, "metadata-resolution-time");
1515+
QName extractName = new QName(SEARCH_NS, "extract-resolution-time");
15081516
QName totalName = new QName(SEARCH_NS, "total-time");
15091517

15101518
long qrTime = -1;
15111519
long frTime = -1;
15121520
long srTime = -1;
1513-
long tTime = -1;
15141521
long mrTime = -1;
1522+
long erTime = -1;
1523+
long tTime = -1;
15151524

15161525
QName metricsName = element.getName();
15171526
events: while (reader.hasNext()) {
@@ -1530,6 +1539,8 @@ private void handleMetrics(XMLEventReader reader, StartElement element)
15301539
srTime = parseTime(dtFactory, now, reader.getElementText());
15311540
} else if (metadataName.equals(startName)) {
15321541
mrTime = parseTime(dtFactory, now, reader.getElementText());
1542+
} else if (extractName.equals(startName)) {
1543+
erTime = parseTime(dtFactory, now, reader.getElementText());
15331544
} else if (totalName.equals(startName)) {
15341545
tTime = parseTime(dtFactory, now, reader.getElementText());
15351546
} else {
@@ -1544,7 +1555,7 @@ private void handleMetrics(XMLEventReader reader, StartElement element)
15441555
}
15451556
}
15461557

1547-
tempMetrics = new SearchMetricsImpl(qrTime, frTime, srTime, mrTime, tTime);
1558+
tempMetrics = new SearchMetricsImpl(qrTime, frTime, srTime, mrTime, erTime, tTime);
15481559
}
15491560
private void handleConstraint(XMLEventReader reader, StartElement element)
15501561
throws XMLStreamException {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ public interface SearchMetrics {
3737
*/
3838
public long getSnippetResolutionTime();
3939

40+
@Deprecated
4041
/**
4142
* Returns the metadata resolution time in milliseconds.
4243
* @return The metadata resolution time.
44+
* @deprecated this is only populated when using the deprecated option extract-metadata
4345
*/
4446
public long getMetadataResolutionTime();
4547

48+
/**
49+
* Returns the extract resolution time in milliseconds.
50+
* @return The extract resolution time.
51+
*/
52+
public long getExtractResolutionTime();
53+
4654
/**
4755
* Returns the total time taken by the query in milliseconds.
4856
* @return The total time.

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

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertNotNull;
2121
import static org.junit.Assert.assertTrue;
2222

23+
import java.io.ByteArrayOutputStream;
2324
import java.io.IOException;
2425

2526
import javax.xml.namespace.QName;
@@ -40,6 +41,7 @@
4041
import com.marklogic.client.admin.config.QueryOptions.QueryRange;
4142
import com.marklogic.client.admin.config.QueryOptions.QueryTransformResults;
4243
import com.marklogic.client.admin.config.QueryOptionsBuilder;
44+
import com.marklogic.client.io.DocumentMetadataHandle;
4345
import com.marklogic.client.io.Format;
4446
import com.marklogic.client.io.QueryOptionsHandle;
4547
import com.marklogic.client.io.SearchHandle;
@@ -50,14 +52,17 @@
5052
import com.marklogic.client.query.MatchLocation;
5153
import com.marklogic.client.query.QueryManager;
5254
import com.marklogic.client.query.QueryManager.QueryView;
55+
import com.marklogic.client.query.RawCombinedQueryDefinition;
56+
import com.marklogic.client.query.SearchMetrics;
5357
import com.marklogic.client.query.StringQueryDefinition;
58+
import com.marklogic.client.util.RequestLogger;
5459

5560
@SuppressWarnings("deprecation")
5661
public class StringSearchTest {
57-
@SuppressWarnings("unused")
58-
private static final Logger logger = (Logger) LoggerFactory
59-
.getLogger(QueryOptionsHandleTest.class);
60-
62+
@SuppressWarnings("unused")
63+
private static final Logger logger = (Logger) LoggerFactory
64+
.getLogger(QueryOptionsHandleTest.class);
65+
6166
@BeforeClass
6267
public static void beforeClass() {
6368
Common.connectAdmin();
@@ -107,16 +112,16 @@ public void testStringSearch2() throws IOException {
107112
handle = queryMgr.search(qdef, handle);
108113
assertNotNull(handle);
109114

110-
MatchDocumentSummary[] summaries = handle.getMatchResults();
111-
assertNotNull(summaries);
112-
assertEquals("expected 2 results", 2, summaries.length);
115+
MatchDocumentSummary[] summaries = handle.getMatchResults();
116+
assertNotNull(summaries);
117+
assertEquals("expected 2 results", 2, summaries.length);
113118

114-
for (MatchDocumentSummary summary : summaries) {
115-
MatchLocation[] locations = summary.getMatchLocations();
116-
assertEquals("expected 1 match location", 1, locations.length);
117-
for (MatchLocation location : locations) {
118-
assertNotNull(location.getAllSnippetText());
119-
}
119+
for (MatchDocumentSummary summary : summaries) {
120+
MatchLocation[] locations = summary.getMatchLocations();
121+
assertEquals("expected 1 match location", 1, locations.length);
122+
for (MatchLocation location : locations) {
123+
assertNotNull(location.getAllSnippetText());
124+
}
120125
}
121126
}
122127

@@ -154,6 +159,76 @@ public void testStringSearch4()
154159
summaries = results.getMatchResults();
155160
assertNotNull(summaries);
156161
assertEquals("expected 2 results", 2, summaries.length);
162+
assertEquals("empty-snippet", results.getSnippetTransformType());
163+
}
164+
165+
@Test
166+
public void testSearchHandle() throws Exception {
167+
String xml =
168+
"<product xmlns='http://example.com/products'>" +
169+
"<description xmlns='' xml:lang='en'>some description</description>" +
170+
"</product>";
171+
172+
ByteArrayOutputStream out = new ByteArrayOutputStream();
173+
RequestLogger logger = Common.client.newLogger(out);
174+
DocumentMetadataHandle meta = new DocumentMetadataHandle()
175+
.withCollections("xml", "products");
176+
Common.client.newXMLDocumentManager().writeAs("test.xml", meta, xml);
177+
QueryManager queryMgr = Common.client.newQueryManager();
178+
queryMgr.startLogging(logger);
179+
RawCombinedQueryDefinition query = queryMgr.newRawCombinedQueryDefinition(
180+
new StringHandle(
181+
"<search xmlns='http://marklogic.com/appservices/search'>" +
182+
"<options>" +
183+
"<extract-document-data selected='all'>" +
184+
"<extract-path>//description[@xml:lang='en']</extract-path>" +
185+
"</extract-document-data>" +
186+
"<constraint name='myFacet'>" +
187+
"<range type='xs:string' facet='true'>" +
188+
"<element name='grandchild'/>" +
189+
"</range>" +
190+
"</constraint>" +
191+
"<extract-metadata>" +
192+
"<qname elem-name='description'/>" +
193+
"</extract-metadata>" +
194+
"<return-constraints>true</return-constraints>" +
195+
"<return-facets>true</return-facets>" +
196+
"<return-metrics>true</return-metrics>" +
197+
"<return-plan>true</return-plan>" +
198+
"<return-qtext>true</return-qtext>" +
199+
"<return-query>true</return-query>" +
200+
"<return-results>true</return-results>" +
201+
"<debug>true</debug>" +
202+
"</options>" +
203+
"<query>" +
204+
"<and-query>" +
205+
"<collection-query><uri>xml</uri></collection-query>" +
206+
"<collection-query><uri>products</uri></collection-query>" +
207+
"</and-query>" +
208+
"</query>" +
209+
"</search>"
210+
)
211+
);
212+
queryMgr.setView(QueryView.ALL);
213+
SearchHandle results = queryMgr.search(query, new SearchHandle());
214+
assertTrue(results.getConstraintIterator(new StringHandle()).next().get().startsWith("<search:constraint"));
215+
assertTrue(results.getConstraintNames()[0].equals("myFacet"));
216+
assertTrue(results.getConstraint("myFacet", new StringHandle()).get().startsWith("<search:constraint"));
217+
assertEquals("myFacet", results.getFacetNames()[0]);
218+
SearchMetrics metrics = results.getMetrics();
219+
assertTrue(metrics.getFacetResolutionTime() >= 0);
220+
assertTrue(metrics.getQueryResolutionTime() >= 0);
221+
assertTrue(metrics.getSnippetResolutionTime() >= 0);
222+
assertTrue(metrics.getMetadataResolutionTime() >= 0);
223+
assertTrue(metrics.getExtractResolutionTime() >= 0);
224+
assertTrue(metrics.getTotalTime() >= 0);
225+
assertTrue(results.getPlan(new StringHandle()).get().startsWith("<search:plan"));
226+
assertEquals("plan", results.getPlan().getFirstChild().getLocalName());
227+
assertEquals("SEARCH-FLWOR", results.getReports()[0].getId());
228+
assertTrue(results.getQuery(new StringHandle()).get().startsWith("<search:query"));
229+
assertEquals("snippet", results.getSnippetTransformType());
230+
assertTrue(results.getWarnings().length == 0);
231+
assertTrue(out.toString().startsWith("searched"));
157232
}
158233

159234
@Test
@@ -200,15 +275,15 @@ private String writeOptions()
200275

201276
// Get back facets...
202277
QueryOptionsBuilder builder = new QueryOptionsBuilder();
203-
QueryRange grandchildRange = builder.range(
204-
builder.elementRangeIndex(
205-
new QName("grandchild"),
206-
builder.stringRangeType("http://marklogic.com/collation/")
207-
));
208-
grandchildRange.setDoFacets(true);
209-
QueryOptionsHandle options = new QueryOptionsHandle().withConstraints(
210-
builder.constraint("grandchild",grandchildRange)
211-
);
278+
QueryRange grandchildRange = builder.range(
279+
builder.elementRangeIndex(
280+
new QName("grandchild"),
281+
builder.stringRangeType("http://marklogic.com/collation/")
282+
));
283+
grandchildRange.setDoFacets(true);
284+
QueryOptionsHandle options = new QueryOptionsHandle().withConstraints(
285+
builder.constraint("grandchild",grandchildRange)
286+
);
212287

213288
QueryRange range = options.getConstraint("grandchild").getSource();
214289
assertEquals(range.getElement(), new QName("grandchild"));
@@ -217,7 +292,7 @@ private String writeOptions()
217292
options.withTransformResults(tresults);
218293

219294
QueryOptionsManager queryOptionsMgr =
220-
Common.client.newServerConfigManager().newQueryOptionsManager();
295+
Common.client.newServerConfigManager().newQueryOptionsManager();
221296

222297
queryOptionsMgr.writeOptions(optionsName, options);
223298

0 commit comments

Comments
 (0)