use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class YardTest method testFindMultipleConstraints.
/**
* Tests simple {@link RangeConstraint}
*/
@Test
public void testFindMultipleConstraints() {
//init the test data
FieldQueryTestData data = getFieldQueryTestData();
//Integer Range and reference query
FieldQuery query = getYard().getQueryFactory().createFieldQuery();
//selects r2, r2en, r2de, r5
query.setConstraint(data.intField, new RangeConstraint(data.intValue2, data.intValue5, true));
//selects r1, r1en, r1de, r5
query.setConstraint(data.refField, new ReferenceConstraint(data.refValue1.getReference()));
query.addSelectedField(data.intField);
query.addSelectedField(data.refField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r5.getId()), Arrays.asList(data.intField, data.refField));
//text and reference
query = getYard().getQueryFactory().createFieldQuery();
//selects r1en and r2en
String wildcard = data.textValue1.getText();
wildcard = wildcard.substring(0, wildcard.length() - 1) + "*";
query.setConstraint(data.textField, new TextConstraint(wildcard, PatternType.wildcard, false, "en"));
//selects r1, r1en, r1de, r5
query.setConstraint(data.refField, new ReferenceConstraint(data.refValue1.getReference()));
query.addSelectedField(data.refField);
query.addSelectedField(data.textField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId()), Arrays.asList(data.refField, data.textField));
//text and value
query = getYard().getQueryFactory().createFieldQuery();
//selects r1de and r2de
query.setConstraint(data.textField, new TextConstraint(wildcard, PatternType.wildcard, false, "de"));
//selects r2, r2en, r2de
query.setConstraint(data.intField, new ValueConstraint(data.intValue2));
query.addSelectedField(data.refField);
query.addSelectedField(data.textField);
validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2de.getId()), Arrays.asList(data.refField, data.textField));
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint 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.TextConstraint 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());
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class MockEntityCorefDbpediaSite method findEntities.
@SuppressWarnings("deprecation")
@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws SiteException {
TextConstraint labelConstraint = (TextConstraint) query.getConstraint(RDFS_LABEL.getUnicodeString());
for (Entity entity : entities.values()) {
Iterator<Object> entityAttributes = entity.getRepresentation().get(RDFS_LABEL.getUnicodeString());
while (entityAttributes.hasNext()) {
Text entityAttribute = (Text) entityAttributes.next();
if (entityAttribute.getText().equals(labelConstraint.getText())) {
Collection<Entity> retEntities = new ArrayList<>(1);
retEntities.add(entity);
return new QueryResultListImpl<Entity>(null, retEntities, Entity.class);
}
}
}
return null;
}
use of org.apache.stanbol.entityhub.servicesapi.query.TextConstraint in project stanbol by apache.
the class EntityhubDereferenceContext method initFieldMappings.
protected void initFieldMappings(List<String> fields) throws DereferenceConfigurationException {
TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
FieldMapper fieldMapper;
if (fields != null && !fields.isEmpty()) {
log.debug("parse FieldMappings from EnhancementProperties");
List<FieldMapping> mappings = new ArrayList<FieldMapping>(fields.size());
for (String configuredMapping : fields) {
FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, dereferencer.getNsPrefixService());
if (mapping != null) {
log.debug(" - add FieldMapping {}", mapping);
mappings.add(mapping);
} else if (configuredMapping != null && !configuredMapping.isEmpty()) {
log.warn(" - unable to parse FieldMapping '{}'", configuredMapping);
}
}
if (!mappings.isEmpty()) {
log.debug(" > apply {} valid mappings", mappings.size());
fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
for (FieldMapping mapping : mappings) {
fieldMapper.addMapping(mapping);
}
} else {
//no valid mapping parsed
log.debug(" > no valid mapping parsed ... will dereference all fields");
fieldMapper = null;
}
} else if (dereferencer.getFieldMapper() != null) {
fieldMapper = dereferencer.getFieldMapper().clone();
} else {
fieldMapper = null;
}
//TODO: uncomment this to merge context with engine mappings. Not sure
// if this is desirable
// if(fieldMapper != null){
// if(dereferencer.getFieldMapper() != null){
// //add mappings of the engine configuration to the context mappings
// for(FieldMapping mapping : dereferencer.getFieldMapper().getMappings()){
// fieldMapper.addMapping(mapping);
// }
// }
// }
//if a fieldMapper is present and languages are set we will add a language
//filter to the fieldMapper. If the fieldmapper is null languages are
//filtered separately.
Collection<String> langs = getLanguages();
if (langs != null && !langs.isEmpty()) {
if (fieldMapper == null) {
//create a fieldMapper for filtering languages
fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
}
fieldMapper.addMapping(new FieldMapping(new TextConstraint((String) null, langs.toArray(new String[langs.size()]))));
}
//set the field
this.fieldMapper = fieldMapper;
}
Aggregations