Search in sources :

Example 6 with FieldDescription

use of com.yahoo.search.query.profile.types.FieldDescription in project vespa by vespa-engine.

the class QueryProfileXMLReader method readFieldDefinitions.

private void readFieldDefinitions(Element element, QueryProfileType type, QueryProfileTypeRegistry registry) {
    for (Element field : XML.getChildren(element, "field")) {
        String name = field.getAttribute("name");
        if (name == null || name.equals(""))
            throw new IllegalArgumentException("A field has no 'name' attribute");
        try {
            String fieldTypeName = field.getAttribute("type");
            if (fieldTypeName == null)
                throw new IllegalArgumentException("Field '" + field + "' has no 'type' attribute");
            FieldType fieldType = FieldType.fromString(fieldTypeName, registry);
            type.addField(new FieldDescription(name, fieldType, field.getAttribute("alias"), getBooleanAttribute("mandatory", false, field), getBooleanAttribute("overridable", true, field)), registry);
        } catch (RuntimeException e) {
            throw new IllegalArgumentException("Invalid field '" + name + "'", e);
        }
    }
}
Also used : Element(org.w3c.dom.Element) FieldDescription(com.yahoo.search.query.profile.types.FieldDescription) FieldType(com.yahoo.search.query.profile.types.FieldType)

Example 7 with FieldDescription

use of com.yahoo.search.query.profile.types.FieldDescription in project vespa by vespa-engine.

the class QueryProfileTypeTestCase method addUserFields.

private void addUserFields(QueryProfileType user, QueryProfileTypeRegistry registry) {
    user.addField(new FieldDescription("myUserString", FieldType.fromString("string", registry)), registry);
    user.addField(new FieldDescription("myUserInteger", FieldType.fromString("integer", registry), "uint"), registry);
}
Also used : FieldDescription(com.yahoo.search.query.profile.types.FieldDescription)

Example 8 with FieldDescription

use of com.yahoo.search.query.profile.types.FieldDescription in project vespa by vespa-engine.

the class TypedProfilesConfigurationTestCase method testIt.

/**
 * Asserts that everything is read correctly from this configuration
 */
public void testIt() {
    QueryProfileConfigurer configurer = new QueryProfileConfigurer("file:src/test/java/com/yahoo/search/query/profile/config/test/typed-profiles.cfg");
    QueryProfileRegistry registry = configurer.getCurrentRegistry();
    QueryProfileTypeRegistry types = registry.getTypeRegistry();
    // Assert that each type was read correctly
    QueryProfileType testType = types.getComponent("testtype");
    assertEquals("testtype", testType.getId().getName());
    assertFalse(testType.isStrict());
    assertFalse(testType.getMatchAsPath());
    assertEquals(7, testType.fields().size());
    assertEquals("myString", testType.getField("myString").getName());
    assertTrue(testType.getField("myString").isMandatory());
    assertTrue(testType.getField("myString").isOverridable());
    assertFalse(testType.getField("myInteger").isMandatory());
    assertFalse(testType.getField("myInteger").isOverridable());
    FieldDescription field = testType.getField("myUserQueryProfile");
    assertEquals("reference to a query profile of type 'user'", field.getType().toInstanceDescription());
    assertTrue(field.getAliases().contains("myqp"));
    assertTrue(field.getAliases().contains("user-profile"));
    QueryProfileType testTypeStrict = types.getComponent("testtypestrict");
    assertTrue(testTypeStrict.isStrict());
    assertTrue(testTypeStrict.getMatchAsPath());
    assertEquals(7, testTypeStrict.fields().size());
    assertEquals("reference to a query profile of type 'userstrict'", testTypeStrict.getField("myUserQueryProfile").getType().toInstanceDescription());
    QueryProfileType user = types.getComponent("user");
    assertFalse(user.isStrict());
    assertFalse(user.getMatchAsPath());
    assertEquals(2, user.fields().size());
    assertEquals(String.class, user.getField("myUserString").getType().getValueClass());
    QueryProfileType userStrict = types.getComponent("userstrict");
    assertTrue(userStrict.isStrict());
    assertFalse(userStrict.getMatchAsPath());
    assertEquals(2, userStrict.fields().size());
}
Also used : QueryProfileConfigurer(com.yahoo.search.query.profile.config.QueryProfileConfigurer) QueryProfileTypeRegistry(com.yahoo.search.query.profile.types.QueryProfileTypeRegistry) QueryProfileRegistry(com.yahoo.search.query.profile.QueryProfileRegistry) QueryProfileType(com.yahoo.search.query.profile.types.QueryProfileType) FieldDescription(com.yahoo.search.query.profile.types.FieldDescription)

Example 9 with FieldDescription

use of com.yahoo.search.query.profile.types.FieldDescription in project vespa by vespa-engine.

the class QueryProfileConfigurer method instantiateFieldDescription.

private static void instantiateFieldDescription(QueryProfilesConfig.Queryprofiletype.Field fieldConfig, QueryProfileType type, QueryProfileTypeRegistry registry) {
    try {
        FieldType fieldType = FieldType.fromString(fieldConfig.type(), registry);
        FieldDescription field = new FieldDescription(fieldConfig.name(), fieldType, fieldConfig.alias(), fieldConfig.mandatory(), fieldConfig.overridable());
        type.addField(field, registry);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Invalid field '" + fieldConfig.name() + "' in " + type, e);
    }
}
Also used : FieldDescription(com.yahoo.search.query.profile.types.FieldDescription) FieldType(com.yahoo.search.query.profile.types.FieldType)

Example 10 with FieldDescription

use of com.yahoo.search.query.profile.types.FieldDescription in project vespa by vespa-engine.

the class QueryProfile method checkAndConvertAssignment.

/**
 * Converts to the type of the receiving field, if possible and necessary.
 *
 * @return the value to be assigned: the original or a converted value
 * @throws IllegalArgumentException if the assignment is illegal
 */
protected Object checkAndConvertAssignment(String localName, Object value, QueryProfileRegistry registry) {
    // no type checking
    if (type == null)
        return value;
    FieldDescription fieldDescription = type.getField(localName);
    if (fieldDescription == null) {
        if (type.isStrict())
            throw new IllegalArgumentException("'" + localName + "' is not declared in " + type + ", and the type is strict");
        return value;
    }
    if (registry == null && (fieldDescription.getType() instanceof QueryProfileFieldType))
        throw new IllegalArgumentException("A registry was not passed: Query profile references is not supported");
    Object convertedValue = fieldDescription.getType().convertFrom(value, registry);
    if (convertedValue == null)
        throw new IllegalArgumentException("'" + value + "' is not a " + fieldDescription.getType().toInstanceDescription());
    return convertedValue;
}
Also used : FieldDescription(com.yahoo.search.query.profile.types.FieldDescription) QueryProfileFieldType(com.yahoo.search.query.profile.types.QueryProfileFieldType)

Aggregations

FieldDescription (com.yahoo.search.query.profile.types.FieldDescription)22 QueryProfileType (com.yahoo.search.query.profile.types.QueryProfileType)11 QueryProfileRegistry (com.yahoo.search.query.profile.QueryProfileRegistry)7 QueryProfile (com.yahoo.search.query.profile.QueryProfile)5 QueryProfileTypeRegistry (com.yahoo.search.query.profile.types.QueryProfileTypeRegistry)5 ComponentId (com.yahoo.component.ComponentId)4 CompiledQueryProfileRegistry (com.yahoo.search.query.profile.compiled.CompiledQueryProfileRegistry)3 CompoundName (com.yahoo.processing.request.CompoundName)2 CompiledQueryProfile (com.yahoo.search.query.profile.compiled.CompiledQueryProfile)2 FieldType (com.yahoo.search.query.profile.types.FieldType)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 HttpRequest (com.yahoo.container.jdisc.HttpRequest)1 Query (com.yahoo.search.Query)1 QueryProfileConfigurer (com.yahoo.search.query.profile.config.QueryProfileConfigurer)1 QueryProfileXMLReader (com.yahoo.search.query.profile.config.QueryProfileXMLReader)1 QueryProfilesConfig (com.yahoo.search.query.profile.config.QueryProfilesConfig)1 QueryProfileFieldType (com.yahoo.search.query.profile.types.QueryProfileFieldType)1 Reference (com.yahoo.searchlib.rankingexpression.Reference)1 TensorType (com.yahoo.tensor.TensorType)1