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