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()));
}
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();
}
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;
}
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;
}
}
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());
}
Aggregations