Skip to content

Commit

Permalink
allow fuzzy search with 1 edit distance
Browse files Browse the repository at this point in the history
  • Loading branch information
MacHu-GWU committed Jan 20, 2024
1 parent 6e7e1d8 commit 84b080b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion aws_resource_search/base_searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def search(
objects, otherwise return the elasticsearch liked result.
:param verbose: whether to print the log
:param bsm: you can explicitly use a ``BotoSesManager`` object to override
the default one you defined when creating the :class:`Searcher`` object.
the default one you defined when creating the :class:`aws_resource_search.base_searcher.BaseSearcher`` object.
"""
final_boto_kwargs = self._get_final_boto_kwargs(boto_kwargs=boto_kwargs)
ds = self._get_ds(
Expand Down
2 changes: 1 addition & 1 deletion aws_resource_search/code/gen_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def enrich_searcher_metadata(
) -> T.List[SearcherMetadata]:
"""
Recursively scan all modules in ``aws_resource_search.res`` package,
try to locate all subclass of the :class:`Searcher` to extract
try to locate all subclass of the :class:`aws_resource_search.base_searcher.BaseSearcher` to extract
all searcher metadata.
Also if the searcher is defined in the ``searcher_enum.json`` but not
Expand Down
14 changes: 12 additions & 2 deletions aws_resource_search/documents/resource_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ class ResourceDocument(BaseArsDocument):
same as the resource name. For example, AWS Lambda function name,
S3 bucket name. But for some resources, it's not. For example,
AWS EC2 instance id, AWS VPC id. This field is also used as a searchable
``IdField`` in :class:`Searcher`.
``IdField`` in :class:`aws_resource_search.base_searcher.BaseSearcher`.
:param name: the human friendly name of the aws resource. For example,
AWS Lambda function name, S3 bucket name. This field is also used as a
searchable ``NgramWordsField`` in :class:`Searcher`.
searchable ``NgramWordsField`` in :class:`aws_resource_search.base_searcher.BaseSearcher`.
:param name: similar to name, but this field is used as a searchable
``TextField`` in :class:`aws_resource_search.base_searcher.BaseSearcher`.
In your subclass, you must implement the following methods.
Please read the docstrings to understand their functionality.
Expand Down Expand Up @@ -285,8 +287,16 @@ class ResourceDocument(BaseArsDocument):
raw_data: "T_RESULT_DATA" = dataclasses.field(metadata={"field": sayt.StoredField(name="raw_data")})
id: str = dataclasses.field(metadata={"field": sayt.IdField(name="id", field_boost=5.0, stored=True)})
name: str = dataclasses.field(metadata={"field": sayt.NgramWordsField(name="name", minsize=2, maxsize=4, stored=True, sortable=True, ascending=True)})
name_text: str = dataclasses.field(metadata={
"field": sayt.TextField(name="name_text", stored=False, sortable=True, ascending=True)}, init=False)
# fmt: on

def __post_init__(self):
name_text = self.name
for char in "-_":
name_text = name_text.replace(char, " ")
self.name_text = name_text

@classmethod
def from_resource(
cls,
Expand Down
1 change: 1 addition & 0 deletions release-history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Backlog (TODO)
- add config system so that user can configure ARS by editing the ``~/.aws_resource_search/config.json`` file.
- add ``!{`` command to view and edit the json file.
- allow user to jump to list aws resources view in AWS console by tapping 'Enter' key in aws resource type search view.
- allow fuzzy search with 1 character edit distance.

**Minor Improvements**

Expand Down
4 changes: 2 additions & 2 deletions tests/test_documents_resource_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class DummyS3Bucket(ResourceDocument):
)

search_fields = DummyS3Bucket.get_dataset_fields()
assert search_fields[3].name == "arn"
assert isinstance(search_fields[3], sayt.StoredField)
assert search_fields[4].name == "arn"
assert isinstance(search_fields[4], sayt.StoredField)

@dataclasses.dataclass
class WrongResource1(ResourceDocument):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_handlers_search_aws_profile_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
def test_search_aws_profile_handler():
with test_home_aws_folder.temp():
items = search_aws_profile_and_return_items(
line_input="s3: my-bucket",
line_input="s3-bucket: my-bucket",
profile_query="*",
refresh_data=True,
)
assert len(items) == 3

items = search_aws_profile_and_return_items(
line_input="s3: my-bucket",
line_input="s3-bucket: my-bucket",
profile_query="*",
)
assert len(items) == 3

items = search_aws_profile_and_return_items(
line_input="s3: my-bucket",
line_input="s3-bucket: my-bucket",
profile_query="invalidprofile",
)
assert len(items) == 1
Expand Down

0 comments on commit 84b080b

Please sign in to comment.