|
32 | 32 | import org.junit.Before; |
33 | 33 | import org.junit.BeforeClass; |
34 | 34 | import org.junit.Test; |
| 35 | +import org.w3c.dom.Document; |
35 | 36 | import org.xml.sax.SAXException; |
36 | 37 |
|
37 | 38 | import com.fasterxml.jackson.core.JsonProcessingException; |
|
42 | 43 | import com.marklogic.client.DatabaseClientFactory; |
43 | 44 | import com.marklogic.client.Transaction; |
44 | 45 | import com.marklogic.client.DatabaseClientFactory.Authentication; |
| 46 | +import com.marklogic.client.document.DocumentManager; |
45 | 47 | import com.marklogic.client.document.DocumentPage; |
46 | 48 | import com.marklogic.client.document.DocumentRecord; |
47 | 49 | import com.marklogic.client.document.DocumentWriteSet; |
|
57 | 59 | import com.marklogic.client.io.SearchHandle; |
58 | 60 | import com.marklogic.client.io.StringHandle; |
59 | 61 | import com.marklogic.client.io.DocumentMetadataHandle.Capability; |
| 62 | +import com.marklogic.client.query.ExtractedItem; |
| 63 | +import com.marklogic.client.query.ExtractedResult; |
60 | 64 | import com.marklogic.client.query.MatchDocumentSummary; |
61 | 65 | import com.marklogic.client.query.QueryDefinition; |
62 | 66 | import com.marklogic.client.query.QueryManager; |
| 67 | +import com.marklogic.client.query.RawCombinedQueryDefinition; |
63 | 68 | import com.marklogic.client.query.RawStructuredQueryDefinition; |
64 | 69 | import com.marklogic.client.query.QueryManager.QueryView; |
65 | 70 | import com.marklogic.client.query.StructuredQueryBuilder; |
@@ -393,6 +398,256 @@ public void testBulkSearchRawXMLStrucQD() throws Exception{ |
393 | 398 | } |
394 | 399 |
|
395 | 400 | } |
| 401 | + |
| 402 | + // This test is to verify extract-document-data & extract-path with Default selected option query |
| 403 | + @Test |
| 404 | + public void testExtractDocumentData() throws Exception { |
| 405 | + this.loadJSONDocuments(); |
| 406 | + this.loadXMLDocuments(); |
| 407 | + String head = "<search:search xmlns:search=\"http://marklogic.com/appservices/search\">"; |
| 408 | + String tail = "</search:search>"; |
| 409 | + String qtext4 = "<search:qtext>71 OR dog14</search:qtext>"; |
| 410 | + DocumentManager docMgr = client.newDocumentManager(); |
| 411 | + QueryManager queryMgr = client.newQueryManager(); |
| 412 | + String options = |
| 413 | + "<search:options>" + |
| 414 | + "<search:extract-document-data>" + |
| 415 | + "<search:extract-path>//foo</search:extract-path>" + |
| 416 | + "<search:extract-path>//says</search:extract-path>" + |
| 417 | + "</search:extract-document-data>" + |
| 418 | + "</search:options>"; |
| 419 | + // test XML response with extracted XML and JSON matches |
| 420 | + String combinedSearch = head + qtext4 + options + tail; |
| 421 | + RawCombinedQueryDefinition rawCombinedQueryDefinition = |
| 422 | + queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedSearch).withMimetype("application/xml")); |
| 423 | + SearchHandle results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); |
| 424 | + MatchDocumentSummary[] summaries = results.getMatchResults(); |
| 425 | + assertNotNull(summaries); |
| 426 | + assertEquals(2, summaries.length); |
| 427 | + for (MatchDocumentSummary summary : summaries) { |
| 428 | + ExtractedResult extracted = summary.getExtracted(); |
| 429 | + if ( Format.XML == summary.getFormat() ) { |
| 430 | + // we don't test for kind because it isn't sent in this case |
| 431 | + assertEquals(1, extracted.size()); |
| 432 | + Document item1 = extracted.next().getAs(Document.class); |
| 433 | + assertEquals("This is so foo with a bar 71", item1.getFirstChild().getTextContent()); |
| 434 | + continue; |
| 435 | + } else if ( Format.JSON == summary.getFormat() ) { |
| 436 | + // we don't test for kind because it isn't sent in this case |
| 437 | + assertEquals(1, extracted.size()); |
| 438 | + for ( ExtractedItem item : extracted ) { |
| 439 | + String stringJsonItem = item.getAs(String.class); |
| 440 | + JsonNode nodeJsonItem = item.getAs(JsonNode.class); |
| 441 | + if ( nodeJsonItem.has("says") ) { |
| 442 | + assertEquals("{\"says\":\"woof\"}", stringJsonItem); |
| 443 | + continue; |
| 444 | + } |
| 445 | + fail("unexpected extracted item:" + stringJsonItem); |
| 446 | + } |
| 447 | + continue; |
| 448 | + } |
| 449 | + fail("unexpected search result:" + summary.getUri()); |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + // This test is to verify extract-document-data & extract-path with selected=exclude option query |
| 454 | + @Test |
| 455 | + public void testExtractDocumentData2() throws Exception { |
| 456 | + this.loadJSONDocuments(); |
| 457 | + this.loadXMLDocuments(); |
| 458 | + String head = "<search:search xmlns:search=\"http://marklogic.com/appservices/search\">"; |
| 459 | + String tail = "</search:search>"; |
| 460 | + String qtext4 = "<search:qtext>71 OR dog14</search:qtext>"; |
| 461 | + DocumentManager docMgr = client.newDocumentManager(); |
| 462 | + QueryManager queryMgr = client.newQueryManager(); |
| 463 | + String options = |
| 464 | + "<search:options>" + |
| 465 | + "<search:extract-document-data selected=\"exclude\">" + |
| 466 | + "<search:extract-path>//foo</search:extract-path>" + |
| 467 | + "<search:extract-path>//says</search:extract-path>" + |
| 468 | + "</search:extract-document-data>" + |
| 469 | + "</search:options>"; |
| 470 | + // test XML response with extracted XML and JSON matches |
| 471 | + String combinedSearch = head + qtext4 + options + tail; |
| 472 | + RawCombinedQueryDefinition rawCombinedQueryDefinition = |
| 473 | + queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedSearch).withMimetype("application/xml")); |
| 474 | + SearchHandle results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); |
| 475 | + MatchDocumentSummary[] summaries = results.getMatchResults(); |
| 476 | + assertNotNull(summaries); |
| 477 | + assertEquals(2, summaries.length); |
| 478 | + for (MatchDocumentSummary summary : summaries) { |
| 479 | + ExtractedResult extracted = summary.getExtracted(); |
| 480 | + if ( Format.XML == summary.getFormat() ) { |
| 481 | + // we don't test for kind because it isn't sent in this case |
| 482 | + assertEquals(1, extracted.size()); |
| 483 | + Document item1 = extracted.next().getAs(Document.class); |
| 484 | + assertEquals("This is so foo with a bar 71", item1.getFirstChild().getTextContent()); |
| 485 | + continue; |
| 486 | + } else if ( Format.JSON == summary.getFormat() ) { |
| 487 | + // we don't test for kind because it isn't sent in this case |
| 488 | + assertEquals(1, extracted.size()); |
| 489 | + for ( ExtractedItem item : extracted ) { |
| 490 | + String stringJsonItem = item.getAs(String.class); |
| 491 | + JsonNode nodeJsonItem = item.getAs(JsonNode.class); |
| 492 | + if ( nodeJsonItem.has("animal") ) { |
| 493 | + assertEquals("{\"animal\":\"dog14\"}", stringJsonItem); |
| 494 | + continue; |
| 495 | + } |
| 496 | + fail("unexpected extracted item:" + stringJsonItem); |
| 497 | + } |
| 498 | + continue; |
| 499 | + } |
| 500 | + fail("unexpected search result:" + summary.getUri()); |
| 501 | + } |
| 502 | + } |
| 503 | + |
| 504 | + // This test is to verify extract-document-data & extract-path with selected=include-with-ancestors option query |
| 505 | + @Test |
| 506 | + public void testExtractDocumentData3() throws Exception { |
| 507 | + String head = "<search:search xmlns:search=\"http://marklogic.com/appservices/search\">"; |
| 508 | + String tail = "</search:search>"; |
| 509 | + String qtext4 = "<search:qtext>71 OR dog14</search:qtext>"; |
| 510 | + DocumentManager docMgr = client.newDocumentManager(); |
| 511 | + QueryManager queryMgr = client.newQueryManager(); |
| 512 | + String options = |
| 513 | + "<search:options>" + |
| 514 | + "<search:extract-document-data selected=\"include-with-ancestors\">" + |
| 515 | + "<search:extract-path>//foo</search:extract-path>" + |
| 516 | + "<search:extract-path>//says</search:extract-path>" + |
| 517 | + "</search:extract-document-data>" + |
| 518 | + "</search:options>"; |
| 519 | + // test XML response with extracted XML and JSON matches |
| 520 | + String combinedSearch = head + qtext4 + options + tail; |
| 521 | + RawCombinedQueryDefinition rawCombinedQueryDefinition = |
| 522 | + queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedSearch).withMimetype("application/xml")); |
| 523 | + SearchHandle results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); |
| 524 | + MatchDocumentSummary[] summaries = results.getMatchResults(); |
| 525 | + assertNotNull(summaries); |
| 526 | + assertEquals(2, summaries.length); |
| 527 | + for (MatchDocumentSummary summary : summaries) { |
| 528 | + ExtractedResult extracted = summary.getExtracted(); |
| 529 | + if ( Format.XML == summary.getFormat() ) { |
| 530 | + // we don't test for kind because it isn't sent in this case |
| 531 | + assertEquals(1, extracted.size()); |
| 532 | + Document item1 = extracted.next().getAs(Document.class); |
| 533 | + assertEquals("", item1.getFirstChild().getTextContent()); |
| 534 | + continue; |
| 535 | + } else if ( Format.JSON == summary.getFormat() ) { |
| 536 | + // we don't test for kind because it isn't sent in this case |
| 537 | + assertEquals(1, extracted.size()); |
| 538 | + for ( ExtractedItem item : extracted ) { |
| 539 | + String stringJsonItem = item.getAs(String.class); |
| 540 | + JsonNode nodeJsonItem = item.getAs(JsonNode.class); |
| 541 | + if ( nodeJsonItem.has("says") ) { |
| 542 | + assertEquals("{\"says\":\"woof\"}", stringJsonItem); |
| 543 | + continue; |
| 544 | + } |
| 545 | + fail("unexpected extracted item:" + stringJsonItem); |
| 546 | + } |
| 547 | + continue; |
| 548 | + } |
| 549 | + fail("unexpected search result:" + summary.getUri()); |
| 550 | + } |
| 551 | + } |
| 552 | + |
| 553 | + // This test is to verify extract-document-data & extract-path with selected=include option query |
| 554 | + @Test |
| 555 | + public void testExtractDocumentData4() throws Exception { |
| 556 | + String head = "<search:search xmlns:search=\"http://marklogic.com/appservices/search\">"; |
| 557 | + String tail = "</search:search>"; |
| 558 | + String qtext4 = "<search:qtext>71 OR dog14</search:qtext>"; |
| 559 | + DocumentManager docMgr = client.newDocumentManager(); |
| 560 | + QueryManager queryMgr = client.newQueryManager(); |
| 561 | + String options = |
| 562 | + "<search:options>" + |
| 563 | + "<search:extract-document-data selected=\"include\">" + |
| 564 | + "<search:extract-path>//foo</search:extract-path>" + |
| 565 | + "<search:extract-path>//says</search:extract-path>" + |
| 566 | + "</search:extract-document-data>" + |
| 567 | + "</search:options>"; |
| 568 | + // test XML response with extracted XML and JSON matches |
| 569 | + String combinedSearch = head + qtext4 + options + tail; |
| 570 | + RawCombinedQueryDefinition rawCombinedQueryDefinition = |
| 571 | + queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedSearch).withMimetype("application/xml")); |
| 572 | + SearchHandle results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); |
| 573 | + MatchDocumentSummary[] summaries = results.getMatchResults(); |
| 574 | + assertNotNull(summaries); |
| 575 | + assertEquals(2, summaries.length); |
| 576 | + for (MatchDocumentSummary summary : summaries) { |
| 577 | + ExtractedResult extracted = summary.getExtracted(); |
| 578 | + if ( Format.XML == summary.getFormat() ) { |
| 579 | + // we don't test for kind because it isn't sent in this case |
| 580 | + assertEquals(1, extracted.size()); |
| 581 | + Document item1 = extracted.next().getAs(Document.class); |
| 582 | + assertEquals("This is so foo with a bar 71", item1.getFirstChild().getTextContent()); |
| 583 | + continue; |
| 584 | + } else if ( Format.JSON == summary.getFormat() ) { |
| 585 | + // we don't test for kind because it isn't sent in this case |
| 586 | + assertEquals(1, extracted.size()); |
| 587 | + for ( ExtractedItem item : extracted ) { |
| 588 | + String stringJsonItem = item.getAs(String.class); |
| 589 | + JsonNode nodeJsonItem = item.getAs(JsonNode.class); |
| 590 | + if ( nodeJsonItem.has("says") ) { |
| 591 | + assertEquals("{\"says\":\"woof\"}", stringJsonItem); |
| 592 | + continue; |
| 593 | + } |
| 594 | + fail("unexpected extracted item:" + stringJsonItem); |
| 595 | + } |
| 596 | + continue; |
| 597 | + } |
| 598 | + fail("unexpected search result:" + summary.getUri()); |
| 599 | + } |
| 600 | + } |
| 601 | + |
| 602 | + // This test is to verify extract-document-data & extract-path with selected=all option query |
| 603 | + @Test |
| 604 | + public void testExtractDocumentData5() throws Exception { |
| 605 | + String head = "<search:search xmlns:search=\"http://marklogic.com/appservices/search\">"; |
| 606 | + String tail = "</search:search>"; |
| 607 | + String qtext4 = "<search:qtext>71 OR dog14</search:qtext>"; |
| 608 | + DocumentManager docMgr = client.newDocumentManager(); |
| 609 | + QueryManager queryMgr = client.newQueryManager(); |
| 610 | + String options = |
| 611 | + "<search:options>" + |
| 612 | + "<search:extract-document-data selected=\"all\">" + |
| 613 | + "<search:extract-path>//foo</search:extract-path>" + |
| 614 | + "<search:extract-path>//says</search:extract-path>" + |
| 615 | + "</search:extract-document-data>" + |
| 616 | + "</search:options>"; |
| 617 | + // test XML response with extracted XML and JSON matches |
| 618 | + String combinedSearch = head + qtext4 + options + tail; |
| 619 | + RawCombinedQueryDefinition rawCombinedQueryDefinition = |
| 620 | + queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedSearch).withMimetype("application/xml")); |
| 621 | + SearchHandle results = queryMgr.search(rawCombinedQueryDefinition, new SearchHandle()); |
| 622 | + MatchDocumentSummary[] summaries = results.getMatchResults(); |
| 623 | + assertNotNull(summaries); |
| 624 | + assertEquals(2, summaries.length); |
| 625 | + for (MatchDocumentSummary summary : summaries) { |
| 626 | + ExtractedResult extracted = summary.getExtracted(); |
| 627 | + if ( Format.XML == summary.getFormat() ) { |
| 628 | + // we don't test for kind because it isn't sent in this case |
| 629 | + assertEquals(1, extracted.size()); |
| 630 | + Document item1 = extracted.next().getAs(Document.class); |
| 631 | + assertEquals("This is so foo with a bar 71", item1.getFirstChild().getTextContent()); |
| 632 | + continue; |
| 633 | + } else if ( Format.JSON == summary.getFormat() ) { |
| 634 | + // we don't test for kind because it isn't sent in this case |
| 635 | + assertEquals(1, extracted.size()); |
| 636 | + for ( ExtractedItem item : extracted ) { |
| 637 | + String stringJsonItem = item.getAs(String.class); |
| 638 | + JsonNode nodeJsonItem = item.getAs(JsonNode.class); |
| 639 | + if ( nodeJsonItem.has("says") ) { |
| 640 | + assertEquals("{\"animal\":\"dog14\", \"says\":\"woof\"}", stringJsonItem); |
| 641 | + continue; |
| 642 | + } |
| 643 | + fail("unexpected extracted item:" + stringJsonItem); |
| 644 | + } |
| 645 | + continue; |
| 646 | + } |
| 647 | + fail("unexpected search result:" + summary.getUri()); |
| 648 | + } |
| 649 | + } |
| 650 | + |
396 | 651 | //This test is to verify RAW JSON structured query |
397 | 652 | @Test |
398 | 653 | public void testBulkSearchRawJSONStrucQD() throws Exception{ |
|
0 commit comments