Search in sources :

Example 51 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testContextualWildcard.

@Test
public void testContextualWildcard() throws Exception {
    deleteAllIn(provider);
    List<Metacard> list = Arrays.asList((Metacard) new MockMetacard(Library.getFlagstaffRecord()), (Metacard) new MockMetacard(Library.getTampaRecord()));
    create(list);
    /** CONTEXTUAL QUERY - SIMPLE TERMS **/
    CommonQueryBuilder queryBuilder = new CommonQueryBuilder();
    boolean isCaseSensitive = false;
    boolean isFuzzy = false;
    QueryImpl query = null;
    SourceResponse sourceResponse = null;
    query = queryBuilder.like(Metacard.METADATA, "Flag*ff Chamber", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(1, sourceResponse.getResults().size());
    // FIX FOR THIS IS IN https://issues.apache.org/jira/browse/SOLR-1604
    // Either roll this in yourself or wait for it to come in with Solr
    query = queryBuilder.like(Metacard.METADATA, "Flag*ff Pulliam", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    // assertEquals(0, sourceResponse.getResults().size());
    query = queryBuilder.like(Metacard.METADATA, "*rport", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(2, sourceResponse.getResults().size());
    query = queryBuilder.like(Metacard.METADATA, "*rpor*", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(2, sourceResponse.getResults().size());
    query = queryBuilder.like(Metacard.METADATA, "*", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(2, sourceResponse.getResults().size());
    query = queryBuilder.like(Metacard.METADATA, "airpo*t", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(2, sourceResponse.getResults().size());
    query = queryBuilder.like(Metacard.METADATA, "Airpo*t", isCaseSensitive, isFuzzy);
    query.setStartIndex(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(2, sourceResponse.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 52 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testUpdateByMetacardId.

@Test
public void testUpdateByMetacardId() throws Exception {
    deleteAllIn(provider);
    MockMetacard metacard1 = new MockMetacard(Library.getFlagstaffRecord());
    MockMetacard metacard2 = new MockMetacard(Library.getShowLowRecord());
    String uri1 = "http://youwillfindme.com/here";
    String uri2 = "http://youwillfindme.com/there";
    metacard1.setResourceURI(new URI(uri1));
    metacard1.setContentTypeName("oldNitf");
    metacard2.setResourceURI(new URI(uri2));
    metacard2.setContentTypeName("oldNitf2");
    metacard2.setResourceSize("25L");
    List<Metacard> list = Arrays.asList((Metacard) metacard1, metacard2);
    CreateResponse createResponse = create(list);
    List<String> responseStrings = MockMetacard.toStringList(createResponse.getCreatedMetacards());
    assertEquals(2, responseStrings.size());
    /** UPDATE **/
    MockMetacard updatedMetacard1 = new MockMetacard(Library.getTampaRecord());
    MockMetacard updatedMetacard2 = new MockMetacard(Library.getFlagstaffRecord());
    updatedMetacard1.setId(metacard1.getId());
    updatedMetacard1.setContentTypeName("nitf");
    updatedMetacard2.setId(metacard2.getId());
    updatedMetacard2.setResourceURI(new URI(uri2));
    updatedMetacard2.setContentTypeName("nitf2");
    updatedMetacard2.setResourceSize("50L");
    list = Arrays.asList((Metacard) updatedMetacard1, updatedMetacard2);
    String[] ids = { metacard1.getId(), metacard2.getId() };
    UpdateResponse updateResponse = update(ids, list);
    assertEquals("Testing Update operation: ", 2, updateResponse.getUpdatedMetacards().size());
    List<Update> updatedMetacards = updateResponse.getUpdatedMetacards();
    for (Update up : updatedMetacards) {
        Metacard newCard = up.getNewMetacard();
        Metacard oldCard = up.getOldMetacard();
        assertNotNull(oldCard.getResourceURI());
        assertEquals(provider.getId(), oldCard.getSourceId());
        assertEquals(provider.getId(), newCard.getSourceId());
        if (oldCard.getContentTypeName().equals("oldNitf")) {
            assertEquals("nitf", newCard.getContentTypeName());
            // TPA is unique to the document
            assertTrue(newCard.getMetadata().indexOf("TPA") != ALL_RESULTS);
            assertThat(newCard.getResourceURI(), is(nullValue()));
            assertThat(oldCard.getResourceURI().toString(), equalTo(uri1));
            assertEquals(oldCard.getId(), newCard.getId());
            // Title
            assertEquals(MockMetacard.DEFAULT_TITLE, oldCard.getTitle());
            assertEquals(MockMetacard.DEFAULT_TITLE, newCard.getTitle());
            // Location (decimal points make them not exact Strings POINT(1
            // 0) as opposed to POINT( 1.0 0.0) )
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), oldCard.getLocation().substring(0, 8));
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), newCard.getLocation().substring(0, 8));
            // Metadata
            assertNotNull(oldCard.getMetadata());
            assertNotNull(newCard.getMetadata());
            assertTrue(!oldCard.getMetadata().isEmpty());
            assertTrue(!newCard.getMetadata().isEmpty());
            // Created Date
            assertFalse(oldCard.getCreatedDate().after(new Date()));
            assertFalse(newCard.getCreatedDate().after(new Date()));
            assertTrue(newCard.getCreatedDate().after(oldCard.getCreatedDate()));
            // Modified Date
            assertTrue(newCard.getModifiedDate().after(oldCard.getModifiedDate()));
            // Effective Date
            assertTrue(newCard.getEffectiveDate().after(oldCard.getEffectiveDate()));
            // Expiration Date
            assertTrue(newCard.getExpirationDate().after(oldCard.getExpirationDate()));
            // Thumbnail
            assertTrue(Arrays.equals(newCard.getThumbnail(), oldCard.getThumbnail()));
        } else if (oldCard.getContentTypeName().equals("oldNitf2")) {
            assertEquals("nitf2", newCard.getContentTypeName());
            // Cardinals is unique to the document
            assertTrue(newCard.getMetadata().indexOf("Cardinals") != ALL_RESULTS);
            assertTrue("50L".equals(newCard.getResourceSize()));
            assertEquals(uri2, newCard.getResourceURI().toString());
            assertEquals(oldCard.getId(), newCard.getId());
            // Title
            assertEquals(MockMetacard.DEFAULT_TITLE, oldCard.getTitle());
            assertEquals(MockMetacard.DEFAULT_TITLE, newCard.getTitle());
            // Location (decimal points make them not exact in Strings
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), oldCard.getLocation().substring(0, 8));
            assertEquals(MockMetacard.DEFAULT_LOCATION.substring(0, 8), newCard.getLocation().substring(0, 8));
            // Metadata
            assertNotNull(oldCard.getMetadata());
            assertNotNull(newCard.getMetadata());
            assertTrue(!oldCard.getMetadata().isEmpty());
            assertTrue(!newCard.getMetadata().isEmpty());
            // Created Date
            assertFalse(oldCard.getCreatedDate().after(new Date()));
            assertFalse(newCard.getCreatedDate().after(new Date()));
            assertTrue(newCard.getCreatedDate().after(oldCard.getCreatedDate()));
            // Modified Date
            assertTrue(newCard.getModifiedDate().after(oldCard.getModifiedDate()));
            // Effective Date
            assertTrue(newCard.getEffectiveDate().after(oldCard.getEffectiveDate()));
            // Expiration Date
            assertTrue(newCard.getExpirationDate().after(oldCard.getExpirationDate()));
            // Thumbnail
            assertTrue(Arrays.equals(newCard.getThumbnail(), oldCard.getThumbnail()));
        } else {
            Assert.fail("Expecting one or the other of the updated records.");
        }
    }
    /** READ **/
    CommonQueryBuilder builder = new CommonQueryBuilder();
    QueryImpl query = builder.queryByProperty(Metacard.RESOURCE_URI, uri2);
    QueryRequestImpl queryRequest = new QueryRequestImpl(query);
    SourceResponse sourceResponse = provider.query(queryRequest);
    assertEquals(1, sourceResponse.getResults().size());
    for (Result r : sourceResponse.getResults()) {
        assertTrue(r.getMetacard().getMetadata().indexOf("Cardinals") != ALL_RESULTS);
        assertEquals(uri2, r.getMetacard().getResourceURI().toString());
    }
    /** UPDATE with null thumbnail **/
    updatedMetacard1.setThumbnail(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", 1, updateResponse.getUpdatedMetacards().size());
    Metacard newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    Metacard oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getThumbnail());
    assertEquals(null, newCard.getThumbnail());
    /** UPDATE with null WKT **/
    // updatedMetacard1.setLocation(null);
    // updateResponse = provider.update(new
    // UpdateRequestImpl(updatedMetacard1.getId(), updatedMetacard1));
    //
    //
    //
    // assertEquals("Testing Update operation: ", 1,
    // updateResponse.getUpdatedMetacards().size());
    //
    // newCard =
    // updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    // oldCard =
    // updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    //
    // assertNotNull(oldCard.getResourceURI());
    // assertNotNull(newCard.getResourceURI());
    // assertEquals(oldCard.getResourceURI().toString(),
    // newCard.getResourceURI().toString());
    // assertEquals(provider.getId(), oldCard.getSourceId());
    // assertEquals(provider.getId(), newCard.getSourceId());
    // LOGGER.info("New Metacard location: {}", newCard.getLocation());
    // LOGGER.info("Old Metacard location: {}", oldCard.getLocation());
    // assertTrue(oldCard.getLocation().contains("POINT"));
    // assertEquals(null, newCard.getLocation());
    /** UPDATE with null expiration date **/
    updatedMetacard1.setExpirationDate(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getExpirationDate());
    assertEquals(null, newCard.getExpirationDate());
    /** UPDATE with null content type **/
    updatedMetacard1.setContentTypeName(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getContentTypeName());
    assertThat(newCard.getContentTypeName(), nullValue());
    /** UPDATE with empty content type **/
    updatedMetacard1.setContentTypeName("");
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertThat(oldCard.getContentTypeName(), nullValue());
    assertThat(newCard.getContentTypeName(), is(""));
    /** UPDATE with null content type version **/
    updatedMetacard1.setContentTypeVersion(null);
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertNotNull(oldCard.getContentTypeVersion());
    assertThat(newCard.getContentTypeVersion(), nullValue());
    /** UPDATE with empty content type version **/
    updatedMetacard1.setContentTypeVersion("");
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertThat(oldCard.getContentTypeVersion(), nullValue());
    assertThat(newCard.getContentTypeVersion(), is(""));
    /** UPDATE with new resource uri **/
    updatedMetacard1.setResourceURI(new URI(uri1 + "Now"));
    updateResponse = update(updatedMetacard1.getId(), updatedMetacard1);
    assertEquals("Testing Update operation: ", ONE_HIT, updateResponse.getUpdatedMetacards().size());
    newCard = updateResponse.getUpdatedMetacards().get(0).getNewMetacard();
    oldCard = updateResponse.getUpdatedMetacards().get(0).getOldMetacard();
    assertThat(oldCard.getResourceURI(), is(nullValue()));
    assertEquals(uri1 + "Now", newCard.getResourceURI().toString());
    /** TEST NULL UPDATE **/
    updateResponse = provider.update(new UpdateRequest() {

        @Override
        public boolean hasProperties() {
            return false;
        }

        @Override
        public Serializable getPropertyValue(String name) {
            return null;
        }

        @Override
        public Set<String> getPropertyNames() {
            return null;
        }

        @Override
        public Map<String, Serializable> getProperties() {
            return null;
        }

        @Override
        public boolean containsPropertyName(String name) {
            return false;
        }

        @Override
        public List<Entry<Serializable, Metacard>> getUpdates() {
            return null;
        }

        @Override
        public String getAttributeName() {
            return UpdateRequest.UPDATE_BY_ID;
        }
    });
    assertTrue(updateResponse.getUpdatedMetacards().isEmpty());
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) UpdateRequest(ddf.catalog.operation.UpdateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Update(ddf.catalog.operation.Update) URI(java.net.URI) Date(java.util.Date) Result(ddf.catalog.data.Result) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 53 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method greaterThanOrEqualToQueryAssertion.

private void greaterThanOrEqualToQueryAssertion(String fieldName, Serializable fieldValue, int count) throws UnsupportedQueryException {
    Filter filter = null;
    if (fieldValue instanceof Double) {
        filter = filterBuilder.attribute(fieldName).greaterThanOrEqualTo().number((Double) fieldValue);
    } else if (fieldValue instanceof Integer) {
        filter = filterBuilder.attribute(fieldName).greaterThanOrEqualTo().number((Integer) fieldValue);
    } else if (fieldValue instanceof Short) {
        filter = filterBuilder.attribute(fieldName).greaterThanOrEqualTo().number((Short) fieldValue);
    } else if (fieldValue instanceof Long) {
        filter = filterBuilder.attribute(fieldName).greaterThanOrEqualTo().number((Long) fieldValue);
    } else if (fieldValue instanceof Float) {
        filter = filterBuilder.attribute(fieldName).greaterThanOrEqualTo().number((Float) fieldValue);
    }
    SourceResponse response = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertThat(response.getResults().size(), is(equalTo(count)));
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl)

Example 54 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testQueryMissingSortFieldTemporal.

/**
     * Testing if the temporal search does not fail when no schema field can be found and/or there
     * is no data in the index
     *
     * @throws IngestException
     * @throws UnsupportedQueryException
     */
@Test
public void testQueryMissingSortFieldTemporal() throws IngestException, UnsupportedQueryException {
    /*
         * I have tested this with an empty schema and without an empty schema - both pass, but
         * there is no regression test for the empty schema scenario TODO there should probably be
         * an automated test that creates a fresh cache, that would be a better test
         */
    deleteAllIn(provider);
    Filter txtFilter = filterBuilder.attribute("id").like().text("*");
    QueryImpl query = new QueryImpl(txtFilter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Result.TEMPORAL, SortOrder.ASCENDING));
    SourceResponse response = provider.query(new QueryRequestImpl(query));
    assertEquals(0, response.getResults().size());
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 55 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testSorting.

@Test
public void testSorting() throws Exception {
    deleteAllIn(provider);
    List<Metacard> list = new ArrayList<Metacard>();
    DateTime now = new DateTime();
    for (int i = 0; i < 5; i++) {
        MockMetacard m = new MockMetacard(Library.getFlagstaffRecord());
        m.setEffectiveDate(now.minus(5L * i).toDate());
        m.setTitle("Record " + i);
        list.add(m);
    }
    create(list);
    Filter filter = null;
    QueryImpl query = null;
    SourceResponse sourceResponse = null;
    // Sort all TEMPORAL DESC
    filter = filterBuilder.attribute(Metacard.EFFECTIVE).before().date(now.plusMillis(1).toDate());
    query = new QueryImpl(filter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Metacard.EFFECTIVE, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    for (int i = 0; i < list.size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals("Record " + i, r.getMetacard().getTitle());
    }
    // Sort all TEMPORAL ASC
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Metacard.EFFECTIVE, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    int index = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(index);
        assertEquals("Record " + i, r.getMetacard().getTitle());
        index++;
    }
    // Sort all Relevancy score DESC
    filter = filterBuilder.attribute(Metacard.METADATA).like().text(FLAGSTAFF_QUERY_PHRASE);
    query = new QueryImpl(filter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Result.RELEVANCE, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    double currentScore = Integer.MAX_VALUE;
    for (Result r : sourceResponse.getResults()) {
        assertThat(currentScore, greaterThanOrEqualTo(r.getRelevanceScore()));
        currentScore = r.getRelevanceScore();
    }
    // Sort all Relevancy score DESC
    filter = filterBuilder.attribute(Metacard.METADATA).like().text(FLAGSTAFF_QUERY_PHRASE);
    query = new QueryImpl(filter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Result.RELEVANCE, SortOrder.ASCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    currentScore = 0;
    for (Result r : sourceResponse.getResults()) {
        assertThat(currentScore, lessThanOrEqualTo(r.getRelevanceScore()));
        currentScore = r.getRelevanceScore();
    }
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)232 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)186 Test (org.junit.Test)149 Filter (org.opengis.filter.Filter)117 SourceResponse (ddf.catalog.operation.SourceResponse)95 QueryRequest (ddf.catalog.operation.QueryRequest)66 Metacard (ddf.catalog.data.Metacard)61 ArrayList (java.util.ArrayList)50 Result (ddf.catalog.data.Result)49 Matchers.containsString (org.hamcrest.Matchers.containsString)30 Query (ddf.catalog.operation.Query)29 QueryResponse (ddf.catalog.operation.QueryResponse)28 SortByImpl (ddf.catalog.filter.impl.SortByImpl)27 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)25 SortBy (org.opengis.filter.sort.SortBy)25 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)24 Serializable (java.io.Serializable)23 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)22 HashMap (java.util.HashMap)20 Matchers.anyString (org.mockito.Matchers.anyString)20