Search in sources :

Example 16 with TextConstraint

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));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Example 17 with TextConstraint

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()));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Example 18 with TextConstraint

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

Example 19 with TextConstraint

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;
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) ArrayList(java.util.ArrayList) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 20 with TextConstraint

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;
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Aggregations

TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)31 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)17 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)8 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)7 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)7 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)5 HashSet (java.util.HashSet)3 DataTypeEnum (org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum)3 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)3 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)3 ReconcileValue (org.apache.stanbol.entityhub.jersey.grefine.ReconcileValue)2 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)2 FieldMapping (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)2 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2