Search in sources :

Example 36 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class YardTest method testFindRange.

/**
     * Tests simple {@link RangeConstraint}
     */
@Test
public void testFindRange() {
    //init the test data
    FieldQueryTestData data = getFieldQueryTestData();
    //query for all languages and value1
    FieldQuery query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.intField, new RangeConstraint(data.intValue2, data.intValue5, true));
    query.addSelectedField(data.intField);
    query.addSelectedField(data.refField);
    validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId(), data.r5.getId()), Arrays.asList(data.intField, data.refField));
    //same for value2
    query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.intField, new RangeConstraint(data.intValue2, data.intValue10, false));
    query.addSelectedField(data.intField);
    query.addSelectedField(data.textField);
    validateQueryResults(query, getYard().find(query), Arrays.asList(data.r5.getId()), Arrays.asList(data.intField, data.textField));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) Test(org.junit.Test)

Example 37 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class YardTest method testfindOptionalTexts.

/**
     * Tests a TextConstraint with multiple optional values
     */
@Test
public void testfindOptionalTexts() {
    //init the test data
    FieldQueryTestData data = getFieldQueryTestData();
    //value1@en || value2@en
    FieldQuery query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.textField, new TextConstraint(Arrays.asList(data.textValue1.getText(), data.textValue2.getText()), "en"));
    query.addSelectedField(data.textField);
    query.addSelectedField(data.refField);
    validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r2en.getId()), Arrays.asList(data.textField, data.refField));
    //value1@en,de || value2@en,de
    query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.textField, new TextConstraint(Arrays.asList(data.textValue1.getText(), data.textValue2.getText()), "en", "de"));
    query.addSelectedField(data.textField);
    query.addSelectedField(data.refField);
    validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r1de.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.textField, data.refField));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Example 38 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class SiteManagerImpl method find.

@Override
public QueryResultList<Representation> find(FieldQuery query) {
    log.debug("find with query{}", query);
    Set<Representation> representations = new HashSet<Representation>();
    //TODO: The QueryResultList expects that the query as executed is added
    //to the response. However when executing queries on multiple site they
    //might support a different set of features and therefore execute
    //different variants. For now I return simple the query as executed by
    //the first Site that contributes results
    FieldQuery processedQuery = null;
    FieldQuery queryWithResults = null;
    for (Site site : referencedSites) {
        if (site.supportsSearch()) {
            log.debug(" > query site {}", site.getId());
            try {
                QueryResultList<Representation> results = site.find(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (Representation rep : results) {
                    if (!representations.contains(rep)) {
                        //do not override
                        representations.add(rep);
                    } else {
                        log.info("Entity {} found on more than one Referenced Site" + " -> Representation of Site {} is ignored", rep.getId(), site.getConfiguration().getName());
                    }
                }
            } catch (SiteException e) {
                log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
            }
        } else {
            log.debug(" > Site {} does not support queries", site.getId());
        }
    }
    return new QueryResultListImpl<Representation>(//use the query with results
    queryWithResults != null ? //use the query with results
    queryWithResults : //if not a processed
    processedQuery != null ? //if not a processed
    processedQuery : //else the parsed one
    query, representations, Representation.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 39 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class ClerezzaModelWriter method write.

@Override
public void write(QueryResultList<?> result, OutputStream out, MediaType mediaType) throws WebApplicationException, IOException {
    Graph queryRdf = toRDF(result);
    //we need also to the JSON formatted FieldQuery as a literal to the
    //RDF data.
    FieldQuery query = result.getQuery();
    if (query != null) {
        try {
            JSONObject fieldQueryJson = FieldQueryToJsonUtils.toJSON(query, nsPrefixService);
            if (fieldQueryJson != null) {
                //add the triple with the fieldQuery
                queryRdf.add(new TripleImpl(QUERY_RESULT_LIST, FIELD_QUERY, literalFactory.createTypedLiteral(fieldQueryJson.toString())));
            }
        } catch (JSONException e) {
            log.warn(String.format("Unable to serialize Fieldquery '%s' to JSON! " + "Query response will not contain the serialized query.", query), e);
        }
    }
    //now serialise the data
    writeRdf(queryRdf, out, mediaType);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 40 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class SesameModelWriter method write.

@Override
public void write(QueryResultList<?> result, OutputStream out, MediaType mediaType) throws WebApplicationException, IOException {
    Model queryRdf = toRDF(result);
    //we need also to the JSON formatted FieldQuery as a literal to the
    //RDF data.
    FieldQuery query = result.getQuery();
    if (query != null) {
        try {
            JSONObject fieldQueryJson = FieldQueryToJsonUtils.toJSON(query, nsPrefixService);
            if (fieldQueryJson != null) {
                //add the triple with the fieldQuery
                queryRdf.add(QUERY_RESULT_LIST, FIELD_QUERY, sesameFactory.createLiteral(fieldQueryJson.toString()));
            }
        } catch (JSONException e) {
            log.warn(String.format("Unable to serialize Fieldquery '%s' to JSON! " + "Query response will not contain the serialized query.", query), e);
        }
    }
    //now serialise the data
    writeRdf(queryRdf, out, mediaType);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) JSONObject(org.codehaus.jettison.json.JSONObject) Model(org.openrdf.model.Model) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)41 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)18 Test (org.junit.Test)13 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)12 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)10 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)6 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)5 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)5 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)5 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)4 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)4 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)4 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)3 JSONException (org.codehaus.jettison.json.JSONException)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 IOException (java.io.IOException)2 PrivilegedActionException (java.security.PrivilegedActionException)2