Skip to content

Commit

Permalink
Recognize 'alias' as an option for Field (#633)
Browse files Browse the repository at this point in the history
* Recognize 'alias' as an option for Field

* Add test for generation of aliased fields
  • Loading branch information
maxice8 committed Nov 23, 2023
1 parent 2fbb736 commit 8dfdecb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 15 deletions.
42 changes: 27 additions & 15 deletions vespa/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class FieldConfiguration(TypedDict, total=False):
rank: str
query_command: List[str]
struct_fields: List[StructField]
alias: List[str]


class Field(object):
Expand Down Expand Up @@ -342,17 +343,18 @@ def __init__(
:key rank: Add configuration for ranking calculations of the field.
:key query_command: Add configuration for query-command of the field.
:key struct_fields: Add struct-fields to the field.
:key alias: Add alias to the field.
>>> Field(name = "title", type = "string", indexing = ["index", "summary"], index = "enable-bm25")
Field('title', 'string', ['index', 'summary'], 'enable-bm25', None, None, None, None, None, None, True, None, None, None, [])
Field('title', 'string', ['index', 'summary'], 'enable-bm25', None, None, None, None, None, None, True, None, None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... indexing = ["attribute"],
... attribute=["fast-search", "fast-access"]
... )
Field('abstract', 'string', ['attribute'], None, ['fast-search', 'fast-access'], None, None, None, None, None, True, None, None, None, [])
Field('abstract', 'string', ['attribute'], None, ['fast-search', 'fast-access'], None, None, None, None, None, True, None, None, None, [], None)
>>> Field(name="tensor_field",
... type="tensor<float>(x[128])",
Expand All @@ -363,56 +365,56 @@ def __init__(
... neighbors_to_explore_at_insert=200,
... ),
... )
Field('tensor_field', 'tensor<float>(x[128])', ['attribute'], None, None, HNSW('euclidean', 16, 200), None, None, None, None, True, None, None, None, [])
Field('tensor_field', 'tensor<float>(x[128])', ['attribute'], None, None, HNSW('euclidean', 16, 200), None, None, None, None, True, None, None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... match = ["exact", ("exact-terminator", '"@%"',)],
... )
Field('abstract', 'string', None, None, None, None, ['exact', ('exact-terminator', '"@%"')], None, None, None, True, None, None, None, [])
Field('abstract', 'string', None, None, None, None, ['exact', ('exact-terminator', '"@%"')], None, None, None, True, None, None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... weight = 200,
... )
Field('abstract', 'string', None, None, None, None, None, 200, None, None, True, None, None, None, [])
Field('abstract', 'string', None, None, None, None, None, 200, None, None, True, None, None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... bolding = True,
... )
Field('abstract', 'string', None, None, None, None, None, None, True, None, True, None, None, None, [])
Field('abstract', 'string', None, None, None, None, None, None, True, None, True, None, None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... summary = Summary(None, None, ["dynamic", ["bolding", "on"]]),
... )
Field('abstract', 'string', None, None, None, None, None, None, None, Summary(None, None, ['dynamic', ['bolding', 'on']]), True, None, None, None, [])
Field('abstract', 'string', None, None, None, None, None, None, None, Summary(None, None, ['dynamic', ['bolding', 'on']]), True, None, None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... stemming = "shortest",
... )
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, 'shortest', None, None, [])
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, 'shortest', None, None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... rank = "filter",
... )
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, None, 'filter', None, [])
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, None, 'filter', None, [], None)
>>> Field(
... name = "abstract",
... type = "string",
... query_command = ['"exact %%"'],
... )
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, None, None, ['"exact %%"'], [])
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, None, None, ['"exact %%"'], [], None)
>>> Field(
... name = "abstract",
Expand All @@ -425,7 +427,14 @@ def __init__(
... ),
... ],
... )
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [StructField('first_name', ['attribute'], ['fast-search'], None, None, None)])
Field('abstract', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [StructField('first_name', ['attribute'], ['fast-search'], None, None, None)], None)
>>> Field(
... name = "artist",
... type = "string",
... alias = ["artist_name"],
... )
Field('artist', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [], ['artist_name'])
"""
self.name = name
self.type = type
Expand All @@ -451,6 +460,7 @@ def __init__(
]
)
)
self.alias = kwargs.get("alias", None)

@property
def indexing_to_text(self) -> Optional[str]:
Expand Down Expand Up @@ -489,10 +499,11 @@ def __eq__(self, other: object) -> bool:
and self.rank == other.rank
and self.query_command == other.query_command
and self.struct_fields == other.struct_fields
and self.alias == other.alias
)

def __repr__(self) -> str:
return "{0}({1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15})".format(
return "{0}({1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16})".format(
self.__class__.__name__,
repr(self.name),
repr(self.type),
Expand All @@ -509,6 +520,7 @@ def __repr__(self) -> str:
repr(self.rank),
repr(self.query_command),
repr(self.struct_fields),
repr(self.alias),
)


Expand Down Expand Up @@ -580,7 +592,7 @@ def __init__(self, name: str, fields: Optional[List[Field]] = None):
... Field("last_name", "string"),
... ],
... )
Struct('person', [Field('first_name', 'string', None, None, None, None, None, None, None, None, True, None, None, None, []), Field('last_name', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [])])
Struct('person', [Field('first_name', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [], None), Field('last_name', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [], None)])
"""
self.name = name
self.fields = fields
Expand Down Expand Up @@ -691,10 +703,10 @@ def __init__(
Document(None, None, None)
>>> Document(fields=[Field(name="title", type="string")])
Document([Field('title', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [])], None, None)
Document([Field('title', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [], None)], None, None)
>>> Document(fields=[Field(name="title", type="string")], inherits="context")
Document([Field('title', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [])], context, None)
Document([Field('title', 'string', None, None, None, None, None, None, None, None, True, None, None, None, [], None)], context, None)
"""
self.inherits = inherits
self._fields = (
Expand Down
10 changes: 10 additions & 0 deletions vespa/templates/macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ field {{ field.name }} type {{ field.type }} {
query-command: {{ qc }}
{% endfor %}
{% endif %}
{% if field.alias %}
{% for alias in field.alias %}
alias: {{ alias }}
{% endfor %}
{% endif %}
{% if field.struct_fields %}
{% for struct_field in field.struct_fields %}
struct-field {{ struct_field.name }} {
Expand Down Expand Up @@ -90,6 +95,11 @@ field {{ field.name }} type {{ field.type }} {
query-command: {{ qc }}
{% endfor %}
{% endif %}
{% if field.alias %}
{% for alias in field.alias %}
alias: {{ alias }}
{% endfor %}
{% endif %}
{% if struct_field.summary %}
summary {% if struct_field.summary.name %}{{struct_field.summary.name}}{% endif %}{% if struct_field.summary.type %} type {{ struct_field.summary.type }} {% endif %}{
{% for field in struct_field.summary.attributes_as_string_list %}
Expand Down
5 changes: 5 additions & 0 deletions vespa/templates/schema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ schema {{ schema_name }}{% if schema.inherits %} inherits {{ schema.inherits }}{
{% if field.query_command %}
query-command: {{ field.query_command }}
{% endif %}
{% if field.alias %}
{% for alias in field.alias %}
alias: {{ alias }}
{% endfor %}
{% endif %}
}
{% endfor %}
}
Expand Down
50 changes: 50 additions & 0 deletions vespa/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,3 +1303,53 @@ def test_invalid_name(self):
ApplicationPackage(name="test_app")
with pytest.raises(ValueError):
ApplicationPackage(name="test-app")


class TestFieldAlias(unittest.TestCase):
def setUp(self) -> None:
self.test_schema = Schema(
name="alias_test_schema",
document=Document(
fields=[
Field(
name="single_aliased_field",
type="string",
alias=["single_aliased_field_alias"],
),
Field(
name="multiple_aliased_field",
type="string",
alias=[
"first_alias",
"second_alias",
"third_alias",
],
)
]
),
)

self.app_package = ApplicationPackage(
name="testapp",
schema=[self.test_schema],
)

def test_alias_to_schema(self) -> None:
expected_result = (
"schema alias_test_schema {\n"
" document alias_test_schema {\n"
" field single_aliased_field type string {\n"
" alias: single_aliased_field_alias\n"
" }\n"
" field multiple_aliased_field type string {\n"
" alias: first_alias\n"
" alias: second_alias\n"
" alias: third_alias\n"
" }\n"
" }\n"
"}"
)
self.assertEqual(
self.app_package.get_schema("alias_test_schema").schema_to_text,
expected_result,
)

0 comments on commit 8dfdecb

Please sign in to comment.