2020import static org .junit .Assert .assertNotNull ;
2121import static org .junit .Assert .assertTrue ;
2222
23+ import java .io .ByteArrayOutputStream ;
2324import java .io .IOException ;
2425
2526import javax .xml .namespace .QName ;
4041import com .marklogic .client .admin .config .QueryOptions .QueryRange ;
4142import com .marklogic .client .admin .config .QueryOptions .QueryTransformResults ;
4243import com .marklogic .client .admin .config .QueryOptionsBuilder ;
44+ import com .marklogic .client .io .DocumentMetadataHandle ;
4345import com .marklogic .client .io .Format ;
4446import com .marklogic .client .io .QueryOptionsHandle ;
4547import com .marklogic .client .io .SearchHandle ;
5052import com .marklogic .client .query .MatchLocation ;
5153import com .marklogic .client .query .QueryManager ;
5254import com .marklogic .client .query .QueryManager .QueryView ;
55+ import com .marklogic .client .query .RawCombinedQueryDefinition ;
56+ import com .marklogic .client .query .SearchMetrics ;
5357import com .marklogic .client .query .StringQueryDefinition ;
58+ import com .marklogic .client .util .RequestLogger ;
5459
5560@ SuppressWarnings ("deprecation" )
5661public 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