Skip to content

Commit

Permalink
Merge pull request #913 from Escavador/add-rank-to-struct-field
Browse files Browse the repository at this point in the history
Add rank to StructField
  • Loading branch information
thomasht86 authored Sep 13, 2024
2 parents f652545 + e1b6c9c commit b16d657
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
50 changes: 50 additions & 0 deletions tests/unit/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
ApplicationPackage,
AuthClient,
DeploymentConfiguration,
Struct,
StructField,
)


Expand Down Expand Up @@ -1648,3 +1650,51 @@ def test_deployment_to_text(self):
)

self.assertEqual(expected_result, app_package.deployment_to_text)


class TestSchemaStructField(unittest.TestCase):
def setUp(self):
self.app_package = ApplicationPackage(name="struct")

mystruct = Struct("mystruct", [Field("key", "string"), Field("value", "int")])

my_array = Field(
"my_array",
"array<mystruct>",
["summary"],
struct_fields=[
StructField(
"key",
indexing=["attribute"],
attribute=["fast-search"],
rank="filter",
)
],
)

self.app_package.schema.document = Document([my_array], None, [mystruct])

def test_schema_to_text(self):
expected_result = (
"schema struct {\n"
" document struct {\n"
" field my_array type array<mystruct> {\n"
" indexing: summary\n"
" struct-field key {\n"
" indexing: attribute\n"
" attribute {\n"
" fast-search\n"
" }\n"
" rank: filter\n"
" }\n"
" }\n"
" struct mystruct {\n"
" field key type string {\n"
" }\n"
" field value type int {\n"
" }\n"
" }\n"
" }\n"
"}"
)
self.assertEqual(self.app_package.schema.schema_to_text, expected_result)
24 changes: 19 additions & 5 deletions vespa/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class StructFieldConfiguration(TypedDict, total=False):
match: List[Union[str, Tuple[str, str]]]
query_command: List[str]
summary: Summary
rank: str


class StructField:
Expand All @@ -221,33 +222,43 @@ def __init__(self, name: str, **kwargs: Unpack[StructFieldConfiguration]) -> Non
:key match: Set properties that decide how the matching method for this field operate.
:key query_command: Add configuration for query-command of the field.
:key summary: Add configuration for summary of the field.
:key rank: Specify property that defines ranking calculations done for a field
>>> StructField(
... name = "first_name",
... )
StructField('first_name', None, None, None, None, None)
StructField('first_name', None, None, None, None, None, None)
>>> StructField(
... name = "first_name",
... indexing = ["attribute"],
... attribute = ["fast-search"],
... )
StructField('first_name', ['attribute'], ['fast-search'], None, None, None)
StructField('first_name', ['attribute'], ['fast-search'], None, None, None, None)
>>> StructField(
... name = "last_name",
... match = ["exact", ("exact-terminator", '"@%"')],
... query_command = ['"exact %%"'],
... summary = Summary(None, None, fields=["dynamic", ("bolding", "on")])
... )
StructField('last_name', None, None, ['exact', ('exact-terminator', '"@%"')], ['"exact %%"'], Summary(None, None, ['dynamic', ('bolding', 'on')]))
StructField('last_name', None, None, ['exact', ('exact-terminator', '"@%"')], ['"exact %%"'], Summary(None, None, ['dynamic', ('bolding', 'on')]), None)
>>> StructField(
... name = "first_name",
... indexing = ["attribute"],
... attribute = ["fast-search"],
... rank = "filter",
... )
StructField('first_name', ['attribute'], ['fast-search'], None, None, None, 'filter')
"""
self.name = name
self.indexing = kwargs.get("indexing", None)
self.attribute = kwargs.get("attribute", None)
self.match = kwargs.get("match", None)
self.query_command = kwargs.get("query_command", None)
self.summary = kwargs.get("summary", None)
self.rank = kwargs.get("rank", None)

@property
def indexing_to_text(self) -> Optional[str]:
Expand All @@ -264,24 +275,27 @@ def __eq__(self, other: object) -> bool:
self.match,
self.query_command,
self.summary,
self.rank,
) == (
other.name,
other.indexing,
other.attribute,
other.match,
other.query_command,
other.summary,
other.rank,
)

def __repr__(self) -> str:
return "{0}({1}, {2}, {3}, {4}, {5}, {6})".format(
return "{0}({1}, {2}, {3}, {4}, {5}, {6}, {7})".format(
self.__class__.__name__,
repr(self.name),
repr(self.indexing),
repr(self.attribute),
repr(self.match),
repr(self.query_command),
repr(self.summary),
repr(self.rank),
)


Expand Down Expand Up @@ -427,7 +441,7 @@ 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)], 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)], None)
>>> Field(
... name = "artist",
Expand Down
3 changes: 3 additions & 0 deletions vespa/templates/macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ field {{ field.name }} type {{ field.type }} {
{% endfor %}
}
{% endif %}
{% if struct_field.rank %}
rank: {{ struct_field.rank }}
{% endif %}
}
{% endfor %}
{% endif %}
Expand Down

0 comments on commit b16d657

Please sign in to comment.