Skip to content

Commit

Permalink
feat: search v2 API (#1556)
Browse files Browse the repository at this point in the history
* Initial commit for new search API

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* small cleanup

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* query formation works for search term and filters, need to implement multi search and decide on behavior for when a filter does not apply to a resource

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* verified some behavior and need to get proper functionality on multi search

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* way simpler thanks to Dmitriy

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* Pagination implemented

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* enum

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* parse args correctly in endpoint to make request to es proxy

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* formatted response

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* execute returns a list of Response items not just a Response

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* but of cleanup

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* added pagination details to response

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* tested and made some fixes to format response, still writing unit tests

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* added search score

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* stopping point, writing tests

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* we want to respond with they keys in the mapping not the values

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* tests taking shape, removed prelim models, will put reeq/resp schemas in common

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* added common req/resp/filters model

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* marshmallow request validation

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* unit tests

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* cleanup + feedback imp

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* deleted test file for api not needed

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* lint from common

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* mypy

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* lint and cleanup

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* mypy + flake

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* sorted imports

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* feedback small

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* bump common version

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* renamed endpoint to v2/search

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* AMDResource -> AmundsenResource

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>
  • Loading branch information
allisonsuarez authored Nov 16, 2021
1 parent 88d9271 commit 25265d9
Show file tree
Hide file tree
Showing 8 changed files with 1,146 additions and 1 deletion.
51 changes: 51 additions & 0 deletions common/amundsen_common/models/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0

from typing import List, Optional, Dict

import attr

from marshmallow3_annotations.ext.attrs import AttrsSchema


@attr.s(auto_attribs=True, kw_only=True)
class Filter:
name: str
values: List[str]
operation: str


class FilterSchema(AttrsSchema):
class Meta:
target = Filter
register_as_scheme = True


@attr.s(auto_attribs=True, kw_only=True)
class SearchRequest:
query_term: str
resource_types: List[str] = []
page_index: Optional[int] = 0
results_per_page: Optional[int] = 10
filters: List[Filter] = []


class SearchRequestSchema(AttrsSchema):
class Meta:
target = SearchRequest
register_as_scheme = True


@attr.s(auto_attribs=True, kw_only=True)
class SearchResponse:
msg: str
page_index: int
results_per_page: int
results: Dict # type: ignore
status_code: int


class SearchResponseSchema(AttrsSchema):
class Meta:
target = SearchResponse
register_as_scheme = True
2 changes: 1 addition & 1 deletion common/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from setuptools import find_packages, setup

__version__ = '0.22.0'
__version__ = '0.23.0'


requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'requirements-dev.txt')
Expand Down
4 changes: 4 additions & 0 deletions search/search_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from search_service.api.feature import SearchFeatureAPI, SearchFeatureFilterAPI
from search_service.api.healthcheck import HealthcheckAPI
from search_service.api.search import SearchAPI
from search_service.api.table import SearchTableAPI, SearchTableFilterAPI
from search_service.api.user import SearchUserAPI

Expand Down Expand Up @@ -89,6 +90,9 @@ def create_app(*, config_module_class: str) -> Flask:
# Health Check
api.add_resource(HealthcheckAPI, '/healthcheck')

# New search endpoint
api.add_resource(SearchAPI, '/v2/search')

# Table Search API
api.add_resource(SearchTableFilterAPI, '/search_table')
# TODO: Rename endpoint to be more generic and accept a resource type so that logic can be re-used
Expand Down
55 changes: 55 additions & 0 deletions search/search_service/api/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0

import json
from http import HTTPStatus
from typing import ( # noqa: F401
Any, Iterable, List,
)

from amundsen_common.models.search import SearchRequestSchema
from flasgger import swag_from
from flask_restful import Resource, request

from search_service.proxy.es_search_proxy import (
RESOURCE_STR_MAPPING, ElasticsearchProxy, Resource as AmundsenResource,
)


class SearchAPI(Resource):
"""
Search API handles search requests for filtered and unfiltered search
"""

def __init__(self) -> None:
self.search_proxy = ElasticsearchProxy()

@swag_from('swagger_doc/search/search.yml')
def post(self) -> Iterable[Any]:
"""
Fetch search results
:return: json payload of schema
"""

request_data = SearchRequestSchema().loads(json.dumps(request.get_json()))

resources: List[AmundsenResource] = []
for r in request.get_json().get('resource_types'):
resource = RESOURCE_STR_MAPPING.get(r)
if resource:
resources.append(resource)
else:
err_msg = f'Search for invalid resource "{r}" requested'
return {'message': err_msg}, HTTPStatus.BAD_REQUEST

try:
search_results = self.search_proxy.search(query_term=request_data.query_term,
page_index=request_data.page_index,
results_per_page=request_data.results_per_page,
resource_types=resources,
filters=request_data.filters)
return self.results_schema().dump(search_results), HTTPStatus.OK

except RuntimeError as e:
err_msg = f'Exception encountered while processing search request {e}'
return {'message': err_msg}, HTTPStatus.INTERNAL_SERVER_ERROR
33 changes: 33 additions & 0 deletions search/search_service/api/swagger_doc/search/search.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Resource search
This is used by the frontend API to search through resources.
---
tags:
- 'search_resources'
requestBody:
content:
application/json:
schema:
properties:
query_term:
type: string
page_index:
type: integer
results_per_page:
type: integer
resource_types:
type: array
filters:
type: object
responses:
200:
description: Resource result information with query string
content:
application/json:
schema:
type: json
500:
description: Exception encountered while searching
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
Loading

0 comments on commit 25265d9

Please sign in to comment.