Search in sources :

Example 86 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class CqlRequest method createQueryRequest.

public QueryRequest createQueryRequest(String localSource, FilterBuilder filterBuilder) {
    Query query = new QueryImpl(createFilter(filterBuilder), start, count, parseSort(sort), true, timeout);
    String source = parseSrc(localSource);
    QueryRequest queryRequest;
    if (CACHE_SOURCE.equals(source)) {
        queryRequest = new QueryRequestImpl(query, true);
        queryRequest.getProperties().put("mode", "cache");
    } else {
        queryRequest = new QueryRequestImpl(query, Collections.singleton(source));
        queryRequest.getProperties().put("mode", "update");
    }
    return queryRequest;
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl)

Example 87 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class SolrProviderTestCase method queryAndVerifyCount.

protected void queryAndVerifyCount(int count, Filter filter) throws UnsupportedQueryException {
    Query query = new QueryImpl(filter);
    QueryRequest request = new QueryRequestImpl(query);
    SourceResponse response = provider.query(request);
    assertEquals(count, response.getResults().size());
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl)

Example 88 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class SolrProviderTest method testExtensibleMetacards.

@Test
public void testExtensibleMetacards() throws IngestException, UnsupportedQueryException {
    deleteAllIn(provider);
    String brandNewField1 = "description";
    String brandNewFieldValue1 = "myDescription";
    /* BASIC STRINGS */
    Set<AttributeDescriptor> descriptors = new HashSet<AttributeDescriptor>();
    descriptors.add(new AttributeDescriptorImpl(Metacard.ID, true, true, true, false, BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(brandNewField1, true, true, true, false, BasicTypes.STRING_TYPE));
    MetacardTypeImpl mType = new MetacardTypeImpl("custom1", descriptors);
    MetacardImpl customMetacard = new MetacardImpl(mType);
    // customMetacard.setAttribute("id", "44567880");
    customMetacard.setAttribute(brandNewField1, brandNewFieldValue1);
    create(customMetacard);
    // search id
    Query query = new QueryImpl(filterBuilder.attribute("id").like().text("*"));
    QueryRequest request = new QueryRequestImpl(query);
    SourceResponse response = provider.query(request);
    assertEquals(1, response.getResults().size());
    assertEquals("custom1", response.getResults().get(0).getMetacard().getMetacardType().getName());
    assertThat(response.getResults().get(0).getMetacard().getMetacardType().getAttributeDescriptors(), equalTo(descriptors));
    // search title - *
    query = new QueryImpl(filterBuilder.attribute(brandNewField1).like().text("*"));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(1, response.getResults().size());
    assertEquals("custom1", response.getResults().get(0).getMetacard().getMetacardType().getName());
    assertThat(response.getResults().get(0).getMetacard().getMetacardType().getAttributeDescriptors(), equalTo(descriptors));
    // search title - exact
    query = new QueryImpl(filterBuilder.attribute(brandNewField1).equalTo().text(brandNewFieldValue1));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(1, response.getResults().size());
    // search negative
    query = new QueryImpl(filterBuilder.attribute(brandNewField1).like().text("no"));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(0, response.getResults().size());
    // NEW TYPE
    String brandNewXmlField1 = "author";
    String brandNewXmlFieldValue1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<author>john doe</author>";
    descriptors = new HashSet<AttributeDescriptor>();
    descriptors.add(new AttributeDescriptorImpl(Metacard.ID, true, true, true, false, BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(brandNewXmlField1, true, true, true, false, BasicTypes.XML_TYPE));
    mType = new MetacardTypeImpl("34ga$^TGHfg:/", descriptors);
    customMetacard = new MetacardImpl(mType);
    // customMetacard.setAttribute(Metacard.ID, "44567880");
    customMetacard.setAttribute(brandNewXmlField1, brandNewXmlFieldValue1);
    create(customMetacard);
    // Two Ids
    query = new QueryImpl(filterBuilder.attribute(Metacard.ID).like().text("*"));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(2, response.getResults().size());
    // search xml
    query = new QueryImpl(filterBuilder.attribute(brandNewXmlField1).like().caseSensitiveText("doe"));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(1, response.getResults().size());
    assertEquals("34ga$^TGHfg:/", response.getResults().get(0).getMetacard().getMetacardType().getName());
    assertThat(response.getResults().get(0).getMetacard().getMetacardType().getAttributeDescriptors(), equalTo(descriptors));
    // search xml - negative
    query = new QueryImpl(filterBuilder.attribute(brandNewXmlField1).like().text("author"));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(0, response.getResults().size());
    // EVERYTHING ELSE type
    String doubleField = "hertz";
    double doubleFieldValue = 16065.435;
    String floatField = "inches";
    float floatFieldValue = 4.435f;
    String intField = "count";
    int intFieldValue = 4;
    String longField = "milliseconds";
    long longFieldValue = 987654322111L;
    String byteField = "bytes";
    byte[] byteFieldValue = { 86 };
    String booleanField = "expected";
    boolean booleanFieldValue = true;
    String dateField = "lost";
    Date dateFieldValue = new Date();
    String geoField = "geo";
    String geoFieldValue = GULF_OF_GUINEA_POINT_WKT;
    String shortField = "daysOfTheWeek";
    short shortFieldValue = 1;
    String objectField = "payload";
    BevelBorder objectFieldValue = new BevelBorder(BevelBorder.RAISED);
    descriptors = new HashSet<AttributeDescriptor>();
    descriptors.add(new AttributeDescriptorImpl(Metacard.ID, true, true, true, false, BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(doubleField, true, true, false, false, BasicTypes.DOUBLE_TYPE));
    descriptors.add(new AttributeDescriptorImpl(floatField, true, true, false, false, BasicTypes.FLOAT_TYPE));
    descriptors.add(new AttributeDescriptorImpl(intField, true, true, false, false, BasicTypes.INTEGER_TYPE));
    descriptors.add(new AttributeDescriptorImpl(longField, true, true, false, false, BasicTypes.LONG_TYPE));
    descriptors.add(new AttributeDescriptorImpl(byteField, false, true, false, false, BasicTypes.BINARY_TYPE));
    descriptors.add(new AttributeDescriptorImpl(booleanField, true, true, false, false, BasicTypes.BOOLEAN_TYPE));
    descriptors.add(new AttributeDescriptorImpl(dateField, true, true, false, false, BasicTypes.DATE_TYPE));
    descriptors.add(new AttributeDescriptorImpl(geoField, true, true, false, false, BasicTypes.GEO_TYPE));
    descriptors.add(new AttributeDescriptorImpl(shortField, true, true, false, false, BasicTypes.SHORT_TYPE));
    descriptors.add(new AttributeDescriptorImpl(objectField, false, true, false, false, BasicTypes.OBJECT_TYPE));
    mType = new MetacardTypeImpl("numbersMT", descriptors);
    customMetacard = new MetacardImpl(mType);
    // customMetacard.setAttribute(Metacard.ID, "245gasg324");
    customMetacard.setAttribute(doubleField, doubleFieldValue);
    customMetacard.setAttribute(floatField, floatFieldValue);
    customMetacard.setAttribute(intField, intFieldValue);
    customMetacard.setAttribute(longField, longFieldValue);
    customMetacard.setAttribute(byteField, byteFieldValue);
    customMetacard.setAttribute(booleanField, booleanFieldValue);
    customMetacard.setAttribute(dateField, dateFieldValue);
    customMetacard.setAttribute(geoField, geoFieldValue);
    customMetacard.setAttribute(shortField, shortFieldValue);
    customMetacard.setAttribute(objectField, objectFieldValue);
    create(customMetacard);
    // Three Ids
    query = new QueryImpl(filterBuilder.attribute(Metacard.ID).like().text("*"));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(3, response.getResults().size());
    // search double
    query = new QueryImpl(filterBuilder.attribute(doubleField).greaterThan().number(doubleFieldValue - 1.0));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(1, response.getResults().size());
    // search int
    query = new QueryImpl(filterBuilder.attribute(intField).greaterThan().number(intFieldValue - 1));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(1, response.getResults().size());
    Metacard resultMetacard = response.getResults().get(0).getMetacard();
    assertThat(resultMetacard.getAttribute(Metacard.ID), notNullValue());
    assertThat((Double) (resultMetacard.getAttribute(doubleField).getValue()), equalTo(doubleFieldValue));
    assertThat((Integer) (resultMetacard.getAttribute(intField).getValue()), equalTo(intFieldValue));
    assertThat((Float) (resultMetacard.getAttribute(floatField).getValue()), equalTo(floatFieldValue));
    assertThat((Long) (resultMetacard.getAttribute(longField).getValue()), equalTo(longFieldValue));
    assertThat((byte[]) (resultMetacard.getAttribute(byteField).getValue()), equalTo(byteFieldValue));
    assertThat((Boolean) (resultMetacard.getAttribute(booleanField).getValue()), equalTo(booleanFieldValue));
    assertThat((Date) (resultMetacard.getAttribute(dateField).getValue()), equalTo(dateFieldValue));
    assertThat((String) (resultMetacard.getAttribute(geoField).getValue()), equalTo(geoFieldValue));
    assertThat((Short) (resultMetacard.getAttribute(shortField).getValue()), equalTo(shortFieldValue));
    assertThat((BevelBorder) (resultMetacard.getAttribute(objectField).getValue()), instanceOf(BevelBorder.class));
    /*
         * Going to use the XMLEncoder. If it writes out the objects the same way in xml, then they
         * are the same.
         */
    ByteArrayOutputStream beveledBytesStreamFromSolr = new ByteArrayOutputStream();
    XMLEncoder solrXMLEncoder = new XMLEncoder(new BufferedOutputStream(beveledBytesStreamFromSolr));
    solrXMLEncoder.writeObject((resultMetacard.getAttribute(objectField).getValue()));
    solrXMLEncoder.close();
    ByteArrayOutputStream beveledBytesStreamOriginal = new ByteArrayOutputStream();
    XMLEncoder currendEncoder = new XMLEncoder(new BufferedOutputStream(beveledBytesStreamOriginal));
    currendEncoder.writeObject(objectFieldValue);
    currendEncoder.close();
    assertThat(beveledBytesStreamFromSolr.toByteArray(), equalTo(beveledBytesStreamOriginal.toByteArray()));
    // search short
    query = new QueryImpl(filterBuilder.attribute(shortField).greaterThanOrEqualTo().number(shortFieldValue));
    request = new QueryRequestImpl(query);
    response = provider.query(request);
    assertEquals(1, response.getResults().size());
    resultMetacard = response.getResults().get(0).getMetacard();
}
Also used : Query(ddf.catalog.operation.Query) BevelBorder(javax.swing.border.BevelBorder) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) QueryImpl(ddf.catalog.operation.impl.QueryImpl) BufferedOutputStream(java.io.BufferedOutputStream) HashSet(java.util.HashSet) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) Metacard(ddf.catalog.data.Metacard) XMLEncoder(java.beans.XMLEncoder) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 89 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class CatalogFrameworkQueryTest method testAfterQuery.

@Test
public void testAfterQuery() throws Exception {
    Calendar afterCal = Calendar.getInstance();
    Calendar card1Exp = Calendar.getInstance();
    card1Exp.add(Calendar.YEAR, 1);
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    createResponse = framework.create(new CreateRequestImpl(metacards, null));
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Instant afterInstant = new DefaultInstant(new DefaultPosition(afterCal.getTime()));
    QueryImpl query = new QueryImpl(filterFactory.after(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(afterInstant)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        LOGGER.info("Response:{}", response);
        assertEquals("Expecting return 2 results.", 2, response.getHits());
    } catch (UnsupportedQueryException e) {
        LOGGER.error("Failure!!!", e);
        fail();
    } catch (FederationException e) {
        fail();
    }
    afterInstant = new DefaultInstant(new DefaultPosition(card1Exp.getTime()));
    query = new QueryImpl(filterFactory.after(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(afterInstant)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("After filter should return 1 result", 1, response.getHits());
        assertEquals("After filter should return metacard[" + mcId2 + "]", mcId2, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException e) {
        fail();
    } catch (FederationException e) {
        fail();
    }
    afterInstant = new DefaultInstant(new DefaultPosition(card2Exp.getTime()));
    query = new QueryImpl(filterFactory.after(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(afterInstant)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("After filter should return 0 results.", 0, response.getHits());
    } catch (UnsupportedQueryException e) {
        fail();
    } catch (FederationException e) {
        fail();
    }
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Instant(org.opengis.temporal.Instant) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 90 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class CatalogFrameworkQueryTest method testDuringQuery.

@Test
public void testDuringQuery() {
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    Calendar duringStart = Calendar.getInstance();
    Calendar card1Exp = Calendar.getInstance();
    card1Exp.add(Calendar.YEAR, 1);
    Calendar duringEnd1 = Calendar.getInstance();
    duringEnd1.add(Calendar.YEAR, 2);
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    Calendar duringEnd2 = Calendar.getInstance();
    duringEnd2.add(Calendar.YEAR, 4);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    try {
        createResponse = framework.create(new CreateRequestImpl(metacards, null));
    } catch (IngestException e1) {
        LOGGER.error("Failure", e1);
        fail();
    } catch (SourceUnavailableException e1) {
        LOGGER.error("Failure", e1);
        fail();
    }
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Period duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(duringStart.getTime())), new DefaultInstant(new DefaultPosition(duringEnd1.getTime())));
    QueryImpl query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Expecting return 1 result.", 1, response.getHits());
        assertEquals("During filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card1Exp.getTime())), new DefaultInstant(new DefaultPosition(duringEnd2.getTime())));
    query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("During filter should return 1 result", 1, response.getHits());
        assertEquals("During filter should return metacard[" + mcId2 + "]", mcId2, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(duringStart.getTime())), new DefaultInstant(new DefaultPosition(duringEnd2.getTime())));
    query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("During filter should return 2 result", 2, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Aggregations

QueryRequest (ddf.catalog.operation.QueryRequest)153 Test (org.junit.Test)98 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)86 QueryImpl (ddf.catalog.operation.impl.QueryImpl)66 QueryResponse (ddf.catalog.operation.QueryResponse)57 ArrayList (java.util.ArrayList)41 SourceResponse (ddf.catalog.operation.SourceResponse)39 Metacard (ddf.catalog.data.Metacard)33 Result (ddf.catalog.data.Result)31 Filter (org.opengis.filter.Filter)31 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)30 Query (ddf.catalog.operation.Query)29 Source (ddf.catalog.source.Source)24 FederationException (ddf.catalog.federation.FederationException)20 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)15 InputStream (java.io.InputStream)15 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)14 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)13 SortByImpl (ddf.catalog.filter.impl.SortByImpl)11 HashMap (java.util.HashMap)11