|
| 1 | +package com.marklogic.spark.reader.document; |
| 2 | + |
| 3 | +import com.marklogic.spark.AbstractIntegrationTest; |
| 4 | +import com.marklogic.spark.Options; |
| 5 | +import org.apache.spark.sql.DataFrameReader; |
| 6 | +import org.apache.spark.sql.Dataset; |
| 7 | +import org.apache.spark.sql.Row; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | + |
| 12 | +/** |
| 13 | + * As touched in the documentation for this feature, filtering can in some scenarios significantly improve performance |
| 14 | + * by not retrieving a large number of false positives. Generally, as the percentage of false positives increases, |
| 15 | + * the benefit from filtering will increase by causing the connector to retrieve fewer documents. Overall though, |
| 16 | + * we would still recommend to a customer to configure their indexes so that they can use an unfiltered query that is |
| 17 | + * both fast and accurate. |
| 18 | + */ |
| 19 | +class ReadFilteredDocumentRowsTest extends AbstractIntegrationTest { |
| 20 | + |
| 21 | + private static final String FALSE_POSITIVE_QUERY = "<json-property-word-query xmlns='http://marklogic.com/cts'>" + |
| 22 | + "<property>ForeName</property>" + |
| 23 | + "<text xml:lang='en'>Wool*</text>" + |
| 24 | + "</json-property-word-query>"; |
| 25 | + |
| 26 | + private static final String CORRECT_WILDCARD_QUERY = "<json-property-word-query xmlns='http://marklogic.com/cts'>" + |
| 27 | + "<property>LastName</property>" + |
| 28 | + "<text xml:lang='en'>Wool*</text>" + |
| 29 | + "</json-property-word-query>"; |
| 30 | + |
| 31 | + @Test |
| 32 | + void falsePositive() { |
| 33 | + DataFrameReader reader = newSparkSession().read() |
| 34 | + .format(CONNECTOR_IDENTIFIER) |
| 35 | + .option(Options.CLIENT_URI, makeClientUri()) |
| 36 | + .option(Options.READ_DOCUMENTS_COLLECTIONS, "author") |
| 37 | + .option(Options.READ_DOCUMENTS_QUERY, FALSE_POSITIVE_QUERY); |
| 38 | + |
| 39 | + Dataset<Row> dataset = reader.load(); |
| 40 | + |
| 41 | + assertEquals(1, dataset.count(), "The database has trailing-wildcard-searches enabled, which allows for " + |
| 42 | + "'Wool*' to work. But since the search is unfiltered, we get a false positive as 'Wooles' appears in " + |
| 43 | + "the LastName property, not the ForeName property."); |
| 44 | + |
| 45 | + dataset = reader.option(Options.READ_DOCUMENTS_FILTERED, "true").load(); |
| 46 | + |
| 47 | + assertEquals(0, dataset.count(), "Now that the search is filtered, the false positive will be omitted."); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void correctWildcardQuery() { |
| 52 | + DataFrameReader reader = newSparkSession().read() |
| 53 | + .format(CONNECTOR_IDENTIFIER) |
| 54 | + .option(Options.CLIENT_URI, makeClientUri()) |
| 55 | + .option(Options.READ_DOCUMENTS_COLLECTIONS, "author") |
| 56 | + .option(Options.READ_DOCUMENTS_QUERY, CORRECT_WILDCARD_QUERY); |
| 57 | + |
| 58 | + Dataset<Row> dataset = reader.load(); |
| 59 | + assertEquals(1, dataset.count()); |
| 60 | + |
| 61 | + dataset = reader.option(Options.READ_DOCUMENTS_FILTERED, "true").load(); |
| 62 | + assertEquals(1, dataset.count(), "This test just verifies that a valid wildcard query works correctly on " + |
| 63 | + "our test database."); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void invalidValue() { |
| 68 | + Dataset<Row> dataset = newSparkSession().read() |
| 69 | + .format(CONNECTOR_IDENTIFIER) |
| 70 | + .option(Options.CLIENT_URI, makeClientUri()) |
| 71 | + .option(Options.READ_DOCUMENTS_COLLECTIONS, "author") |
| 72 | + .option(Options.READ_DOCUMENTS_QUERY, FALSE_POSITIVE_QUERY) |
| 73 | + .option(Options.READ_DOCUMENTS_FILTERED, "not-valid") |
| 74 | + .load(); |
| 75 | + |
| 76 | + assertEquals(1, dataset.count(), "Boolean.parseBoolean interprets a non-true/false value as false, so we " + |
| 77 | + "expect the query to be unfiltered and thus we get back a count of 1 due to the false positive."); |
| 78 | + } |
| 79 | +} |
0 commit comments