Search in sources :

Example 21 with FieldQuery

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

the class YardTest method testFindReferencesText.

/**
     * Same as {@link #testFindText()} but using 
     * {@link Yard#findReferences(FieldQuery)} to execute the queries
     */
@Test
public void testFindReferencesText() {
    //init the test data
    FieldQueryTestData data = getFieldQueryTestData();
    //query for all languages and value1
    FieldQuery query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText()));
    validateQueryResults(query, getYard().findReferences(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId()));
    //same for value2
    query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText()));
    validateQueryResults(query, getYard().findReferences(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId()));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Example 22 with FieldQuery

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

the class EntityhubSearcher method lookup.

@Override
public Collection<? extends Representation> lookup(String field, Set<String> includeFields, List<String> search, String... languages) throws IllegalStateException {
    Entityhub entityhub = getSearchService();
    if (entityhub == null) {
        throw new IllegalStateException("The Entityhub is currently not active");
    }
    FieldQuery query = EntitySearcherUtils.createFieldQuery(entityhub.getQueryFactory(), field, includeFields, search, languages);
    QueryResultList<Representation> results;
    try {
        results = entityhub.find(query);
    } catch (EntityhubException e) {
        throw new IllegalStateException("Exception while searchign for " + search + '@' + Arrays.toString(languages) + "in the Entityhub", e);
    }
    return results.results();
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Entityhub(org.apache.stanbol.entityhub.servicesapi.Entityhub)

Example 23 with FieldQuery

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

the class EntityhubImpl method getMappingsByTarget.

@Override
public Collection<Entity> getMappingsByTarget(String targetId) throws YardException {
    if (targetId == null) {
        log.warn("NULL parsed as Reference -> call to getMappingsBySymbol ignored (return null)");
        return null;
    }
    FieldQuery fieldQuery = getQueryFactory().createFieldQuery();
    fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(targetId));
    QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery);
    Collection<Entity> mappings = new HashSet<Entity>();
    for (Representation rep : resultList) {
        mappings.add(loadEntity(rep));
    }
    return mappings;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) HashSet(java.util.HashSet)

Example 24 with FieldQuery

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

the class EntityhubImpl method getMappingBySource.

@Override
public Entity getMappingBySource(String reference) throws YardException {
    if (reference == null) {
        log.warn("NULL parsed as Reference -> call to getMappingByEntity ignored (return null)");
        return null;
    }
    FieldQuery fieldQuery = getQueryFactory().createFieldQuery();
    fieldQuery.setConstraint(RdfResourceEnum.mappingSource.getUri(), new ReferenceConstraint(reference));
    QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery);
    if (!resultList.isEmpty()) {
        Iterator<Representation> resultIterator = resultList.iterator();
        Entity mapping = loadEntity(resultIterator.next());
        //print warnings in case of multiple mappings
        if (resultIterator.hasNext()) {
            log.warn("Multiple Mappings found for Entity {}!", reference);
            log.warn("  > {} -> returned instance", mapping.getId());
            while (resultIterator.hasNext()) {
                log.warn("  > {} -> ignored", resultIterator.next());
            }
        }
        if (!EntityMapping.isValid(mapping)) {
            log.warn("Entity {} is not a valid EntityMapping. -> return null", mapping);
            mapping = null;
        }
        return mapping;
    } else {
        log.debug("No Mapping found for Entity {}", reference);
        return null;
    }
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)

Example 25 with FieldQuery

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

the class SolrYardTest method testFieldQueryWithSimilarityConstraint.

@Test
public void testFieldQueryWithSimilarityConstraint() throws YardException {
    // NOTE: this does not test if the updated view of the representation is
    // stored, but only that the update method works correctly
    Yard yard = getYard();
    String id1 = "urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1";
    String id2 = "urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2";
    String id3 = "urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id3";
    String similarityfield = NamespaceEnum.rdfs + "comment";
    String filterfield = "urn:the.field:used.for.testFieldQueryWithSimilarityConstraint.filter";
    Representation test1 = create(id1, true);
    Representation test2 = create(id2, true);
    Representation test3 = create(id3, true);
    // change the representations to be sure to force an update even if the
    // implementation checks for changes before updating a representation
    test1.add(similarityfield, "aaaa aaaa aaaa bbbb bbbb cccc cccc dddd dddd");
    test1.add(filterfield, "Some text content");
    test2.add(similarityfield, "aaaa bbbb bbbb bbbb bbbb eeee");
    test2.add(filterfield, "Some other content");
    test3.add(similarityfield, "eeee eeee ffff gggg");
    test3.add(filterfield, "Different content");
    Iterable<Representation> updatedIterable = yard.update(Arrays.asList(test1, test2, test3));
    assertNotNull(updatedIterable);
    // Perform a first similarity query that looks a lot like the first document
    FieldQuery query = yard.getQueryFactory().createFieldQuery();
    query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
    QueryResultList<Representation> results = yard.find(query);
    assertEquals(2, results.size());
    Iterator<Representation> it = results.iterator();
    Representation first = it.next();
    assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1", first.getId());
    // assertEquals(0.99, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));
    Representation second = it.next();
    assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2", second.getId());
    // assertEquals(0.80, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));
    // combine similarity with traditional filtering
    query = yard.getQueryFactory().createFieldQuery();
    query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
    query.setConstraint(filterfield, new TextConstraint(Arrays.asList("other")));
    results = yard.find(query);
    assertEquals(1, results.size());
    it = results.iterator();
    first = it.next();
    assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2", first.getId());
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) YardTest(org.apache.stanbol.entityhub.test.yard.YardTest) Test(org.junit.Test)

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