Search in sources :

Example 41 with QueryImpl

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

the class CommonQueryBuilder method read.

/**
     * Builds {@link QueryImpl} with a start index of 1 and request for total count set to true that
     * contains all the filter logic to request documents from the given List of ids.
     *
     * @param ids
     * @return {@link QueryImpl}
     */
public QueryImpl read(List<String> ids) {
    LinkedList<Filter> orFilters = new LinkedList<Filter>();
    for (String id : ids) {
        orFilters.add(filterFactory.equals(filterFactory.property(Metacard.ID), filterFactory.literal(id)));
    }
    QueryImpl query = new QueryImpl(filterFactory.or(orFilters));
    query.setStartIndex(1);
    query.setRequestsTotalResultsCount(true);
    return query;
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) LinkedList(java.util.LinkedList)

Example 42 with QueryImpl

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

the class SolrProviderTest method testQueryHasLuceneSpecialCharacters.

@Test
public void testQueryHasLuceneSpecialCharacters() throws Exception {
    deleteAllIn(provider);
    List<Metacard> list = Arrays.asList((Metacard) new MockMetacard(Library.getFlagstaffRecord()), new MockMetacard(Library.getTampaRecord()));
    create(list);
    // if + is escaped, this query will be an implicit OR otherwise both both terms would be
    // required
    Filter txtFilter = filterBuilder.attribute(Metacard.ANY_TEXT).like().text("+Flag?taff +" + TAMPA_QUERY_PHRASE);
    SourceResponse response = provider.query(new QueryRequestImpl(new QueryImpl(txtFilter)));
    assertEquals(1, response.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) 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 43 with QueryImpl

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

the class SolrProviderTest method testXpathCompoundContextualQuery.

@Test
public void testXpathCompoundContextualQuery() throws Exception {
    ConfigurationStore.getInstance().setDisableTextPath(false);
    deleteAllIn(provider);
    String nonexistentXpath = "/this/xpath[does/not/@ex:exist]";
    MockMetacard tampa = new MockMetacard(Library.getTampaRecord());
    tampa.setTitle("Tampa");
    tampa.setLocation(TAMPA_AIRPORT_POINT_WKT);
    MockMetacard flagstaff = new MockMetacard(Library.getFlagstaffRecord());
    flagstaff.setLocation(FLAGSTAFF_AIRPORT_POINT_WKT);
    create(Arrays.asList((Metacard) tampa, flagstaff));
    /* XPath AND temporal AND spatial. */
    Filter filter = filterBuilder.allOf(filterBuilder.xpath("/rss/channel[item/link]").exists(), filterBuilder.attribute(Metacard.MODIFIED).before().date(new DateTime().plus(1).toDate()), filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(FLAGSTAFF_AIRPORT_POINT_WKT));
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("XPath AND temporal AND spatial", ONE_HIT, sourceResponse.getResults().size());
    /* temporal AND (Bad XPath OR XPath) */
    filter = filterBuilder.allOf(filterBuilder.attribute(Metacard.MODIFIED).before().date(new DateTime().plus(1).toDate()), filterBuilder.anyOf(filterBuilder.xpath(nonexistentXpath).exists(), filterBuilder.xpath("//channel/image/title").is().like().text(FLAGSTAFF_QUERY_PHRASE)));
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("temporal AND (Bad XPath OR XPath)", ONE_HIT, sourceResponse.getResults().size());
    /* Bad XPath OR (spatial AND XPath) */
    filter = filterBuilder.anyOf(filterBuilder.xpath(nonexistentXpath).is().like().text("any phrase"), filterBuilder.allOf(filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(TAMPA_AIRPORT_POINT_WKT), filterBuilder.xpath("/rss//item/enclosure/@url").exists()));
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Bad XPath OR (spatial AND XPath)", ONE_HIT, sourceResponse.getResults().size());
    assertEquals("Tampa", sourceResponse.getResults().get(0).getMetacard().getTitle());
    /* spatial AND (Bad XPath OR Bad XPath) */
    filter = filterBuilder.allOf(filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(FLAGSTAFF_AIRPORT_POINT_WKT), filterBuilder.anyOf(filterBuilder.xpath(nonexistentXpath).exists(), filterBuilder.xpath("//also/does/not[@exist]").is().like().text(FLAGSTAFF_QUERY_PHRASE)));
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("spatial AND (Bad XPath OR Bad XPath)", 0, sourceResponse.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 44 with QueryImpl

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

the class SolrProviderTest method testDeleteAlternativeAttribute.

/**
     * Testing if another attribute can be used to delete records other than {@link Metacard#ID}
     *
     * @throws IngestException
     * @throws UnsupportedQueryException
     */
@Test
public void testDeleteAlternativeAttribute() throws IngestException, UnsupportedQueryException {
    deleteAllIn(provider);
    MockMetacard metacard = new MockMetacard(Library.getFlagstaffRecord());
    CreateResponse createResponse = create(metacard);
    DeleteResponse deleteResponse = provider.delete(new DeleteRequest() {

        @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<? extends Serializable> getAttributeValues() {
            return Arrays.asList(MockMetacard.DEFAULT_TITLE);
        }

        @Override
        public String getAttributeName() {
            return Metacard.TITLE;
        }
    });
    Metacard deletedMetacard = deleteResponse.getDeletedMetacards().get(0);
    verifyDeletedRecord(metacard, createResponse, deleteResponse, deletedMetacard);
    // verify it is really not in SOLR
    Filter filter = filterBuilder.attribute(Metacard.TITLE).like().text(MockMetacard.DEFAULT_TITLE);
    QueryImpl query = new QueryImpl(filter);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    List<Result> results = sourceResponse.getResults();
    assertEquals(0, results.size());
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) SourceResponse(ddf.catalog.operation.SourceResponse) CreateResponse(ddf.catalog.operation.CreateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DeleteRequest(ddf.catalog.operation.DeleteRequest) Map(java.util.Map) Test(org.junit.Test)

Example 45 with QueryImpl

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

the class SolrProviderTest method testNumericSort.

@Test
public void testNumericSort() throws Exception {
    deleteAllIn(provider);
    /* SETUP */
    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 = 9876543293L;
    String shortField = "daysOfTheWeek";
    short shortFieldValue = 1;
    int factor = 5;
    List<Metacard> list = new ArrayList<Metacard>();
    DateTime now = new DateTime();
    for (int i = 0; i < 5; i++) {
        Set<AttributeDescriptor> descriptors = numericalDescriptors(doubleField, floatField, intField, longField, shortField);
        MetacardTypeImpl mType = new MetacardTypeImpl("numberMetacardType", descriptors);
        MetacardImpl customMetacard1 = new MetacardImpl(mType);
        customMetacard1.setAttribute(Metacard.ID, "");
        customMetacard1.setAttribute(doubleField, doubleFieldValue + factor * i);
        customMetacard1.setAttribute(floatField, floatFieldValue + factor * i);
        customMetacard1.setAttribute(intField, intFieldValue + factor * i);
        customMetacard1.setAttribute(longField, longFieldValue + factor * i);
        customMetacard1.setAttribute(shortField, shortFieldValue + factor * i);
        list.add(customMetacard1);
    }
    create(list);
    Filter filter = null;
    QueryImpl query = null;
    SourceResponse sourceResponse = null;
    // Sort all Double ASCENDING
    filter = filterBuilder.attribute(Metacard.ANY_TEXT).like().text("*");
    query = new QueryImpl(filter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(doubleField, SortOrder.ASCENDING.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(doubleFieldValue + factor * i, r.getMetacard().getAttribute(doubleField).getValue());
    }
    // Sort all double DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(doubleField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    int counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(doubleFieldValue + factor * counter, r.getMetacard().getAttribute(doubleField).getValue());
        counter++;
    }
    // Sort all Float ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(floatField, SortOrder.ASCENDING.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(floatFieldValue + factor * i, r.getMetacard().getAttribute(floatField).getValue());
    }
    // Sort all Float DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(floatField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(floatFieldValue + factor * counter, r.getMetacard().getAttribute(floatField).getValue());
        counter++;
    }
    // Sort all int ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(intField, SortOrder.ASCENDING.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(intFieldValue + factor * i, r.getMetacard().getAttribute(intField).getValue());
    }
    // Sort all int DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(intField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(intFieldValue + factor * counter, r.getMetacard().getAttribute(intField).getValue());
        counter++;
    }
    // Sort all long ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(longField, SortOrder.ASCENDING.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(longFieldValue + factor * i, r.getMetacard().getAttribute(longField).getValue());
    }
    // Sort all long DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(longField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals(longFieldValue + factor * counter, r.getMetacard().getAttribute(longField).getValue());
        counter++;
    }
    // Sort all short ASCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(shortField, SortOrder.ASCENDING.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((short) (shortFieldValue + factor * i), r.getMetacard().getAttribute(shortField).getValue());
    }
    // Sort all short DESCENDING
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(shortField, SortOrder.DESCENDING.name()));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(list.size(), sourceResponse.getResults().size());
    counter = 0;
    for (int i = (list.size() - 1); i >= 0; i--) {
        Result r = sourceResponse.getResults().get(i);
        assertEquals((short) (shortFieldValue + factor * counter), r.getMetacard().getAttribute(shortField).getValue());
        counter++;
    }
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) 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