Search in sources :

Example 16 with FieldQuery

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

the class EntitySearcherUtils method createFieldQuery.

/**
     * Validated the parsed parameter as parsed to 
     * {@link EntitySearcher#lookup(String, Set, List, String...)}
     * and creates a fieldQuery for the parsed parameter
     * @param field
     * @param includeFields
     * @param search
     * @param languages
     * @return
     */
public static final FieldQuery createFieldQuery(FieldQueryFactory factory, IRI field, Set<IRI> includeFields, List<String> search, String... languages) {
    if (field == null || field.getUnicodeString().isEmpty()) {
        throw new IllegalArgumentException("The parsed search field MUST NOT be NULL nor empty");
    }
    if (search == null || search.isEmpty()) {
        throw new IllegalArgumentException("The parsed list of search strings MUST NOT be NULL nor empty");
    }
    //build the query and than return the result
    FieldQuery query = factory.createFieldQuery();
    if (includeFields == null) {
        query.addSelectedField(field.getUnicodeString());
    } else {
        if (!includeFields.contains(field.getUnicodeString())) {
            query.addSelectedField(field.getUnicodeString());
        }
        for (IRI select : includeFields) {
            query.addSelectedField(select.getUnicodeString());
        }
    }
    //also add the entity rankings
    query.addSelectedField(RdfResourceEnum.entityRank.getUri());
    //TODO make configurable
    query.setLimit(20);
    //List<String> search2 = new ArrayList<String>(search.size() + 1);
    //Collections.reverse(search);
    //search2.add(StringUtils.join(search, " "));
    //search2.addAll(search);
    TextConstraint tc = new TextConstraint(search, languages);
    //STANBOL-1104
    tc.setProximityRanking(true);
    query.setConstraint(field.getUnicodeString(), tc);
    return query;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) IRI(org.apache.clerezza.commons.rdf.IRI) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 17 with FieldQuery

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

the class AbstractBackend method listSubjects.

@Override
public Collection<Object> listSubjects(Object property, Object object) {
    FieldQuery query = createQuery();
    if (this.isURI(object)) {
        query.setConstraint(property.toString(), new ReferenceConstraint(object.toString()));
    } else if (object instanceof Text) {
        Text text = (Text) object;
        TextConstraint constraint;
        if (text.getLanguage() == null) {
            constraint = new TextConstraint(text.getText(), PatternType.none, true);
        } else {
            constraint = new TextConstraint(text.getText(), PatternType.none, true, text.getLanguage());
        }
        query.setConstraint(property.toString(), constraint);
    } else {
        Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(object.getClass());
        if (dataTypes == null || dataTypes.isEmpty()) {
            query.setConstraint(property.toString(), new ValueConstraint(object));
        } else {
            Collection<String> types = new ArrayList<String>(dataTypes.size());
            for (DataTypeEnum type : dataTypes) {
                types.add(type.getUri());
            }
            query.setConstraint(property.toString(), new ValueConstraint(object, types));
        }
    }
    query.setLimit(Integer.valueOf(DEFAULT_MAX_SELECT));
    QueryResultList<String> results;
    try {
        results = query(query);
    } catch (EntityhubException e) {
        throw new IllegalStateException("Unable to query for resources with value '" + object + "' on property '" + property + "'!", e);
    }
    Collection<Object> references;
    if (results.isEmpty()) {
        references = Collections.emptySet();
    } else if (results.size() == 1) {
        //assuming that a single result is a likely case
        references = Collections.singleton((Object) getValueFactory().createReference(results.iterator().next()));
    } else {
        int offset = 0;
        references = new HashSet<Object>(results.size());
        for (String result : results) {
            references.add(getValueFactory().createReference(result));
        }
        while (results.size() >= DEFAULT_MAX_SELECT && references.size() <= DEFAULT_MAX_RESULTS - DEFAULT_MAX_SELECT) {
            offset = offset + results.size();
            query.setOffset(offset);
            try {
                results = query(query);
            } catch (EntityhubException e) {
                throw new IllegalStateException("Unable to query for resources with value '" + object + "' on property '" + property + "'!", e);
            }
            for (String result : results) {
                references.add(getValueFactory().createReference(result));
            }
        }
    }
    return references;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 18 with FieldQuery

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

the class YardTest method testFindTextWildcards.

@Test
public void testFindTextWildcards() {
    //init the test data
    FieldQueryTestData data = getFieldQueryTestData();
    //prefix search with *
    FieldQuery query = getYard().getQueryFactory().createFieldQuery();
    String wildcard = data.textValue1.getText();
    wildcard = wildcard.substring(0, wildcard.length() - 1) + "*";
    query.setConstraint(data.textField, new TextConstraint(wildcard, PatternType.wildcard, false, "en"));
    query.addSelectedField(data.refField);
    query.addSelectedField(data.textField);
    validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r2en.getId()), Arrays.asList(data.refField, data.textField));
    //wildcard with ?
    query = getYard().getQueryFactory().createFieldQuery();
    //selects r1en and r2en
    wildcard = data.textValue1.getText();
    wildcard = wildcard.substring(0, wildcard.length() - 1) + "?";
    query.setConstraint(data.textField, new TextConstraint(wildcard, PatternType.wildcard, false, "de"));
    query.addSelectedField(data.refField);
    query.addSelectedField(data.textField);
    validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1de.getId(), data.r2de.getId()), Arrays.asList(data.refField, data.textField));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Example 19 with FieldQuery

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

the class YardTest method testFindReference.

/**
     * Test a {@link ReferenceConstraint}
     */
@Test
public void testFindReference() {
    //init the test data
    FieldQueryTestData data = getFieldQueryTestData();
    //query for all languages and value1
    FieldQuery query = getYard().getQueryFactory().createFieldQuery();
    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.r1.getId(), data.r1en.getId(), data.r1de.getId(), data.r5.getId()), Arrays.asList(data.intField, data.refField));
    //same for value2
    query = getYard().getQueryFactory().createFieldQuery();
    query.setConstraint(data.refField, new ReferenceConstraint(data.refValue2.getReference()));
    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.r10.getId()), Arrays.asList(data.intField, data.refField));
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) Test(org.junit.Test)

Example 20 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery 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)

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