This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
add endpoint search
#224
Merged
Merged
add endpoint search
#224
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88b4904
add enpoint `search`
remi-dupre 6d9041f
idunn: use IdunnAutocomplete's output
remi-dupre fd28219
search: remove unused function `search_intention`
remi-dupre 6c598d4
instant_answer: fix inconsistant return values
remi-dupre c9eb79c
search: remove unused function `search_single_place`
remi-dupre 2078c67
search: allow same set of parameters as autocomplete
remi-dupre e410d04
search: allow POST queries
remi-dupre 5cdc530
search: rely on get_autocomplete
remi-dupre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import logging | ||
from fastapi import Body, Depends | ||
|
||
from idunn import settings | ||
from idunn.geocoder.bragi_client import bragi_client | ||
from idunn.geocoder.nlu_client import nlu_client, NluClientException | ||
from idunn.utils import result_filter | ||
from idunn.instant_answer import normalize | ||
from ..geocoder.models import ExtraParams, QueryParams, IdunnAutocomplete | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
nlu_allowed_languages = settings["NLU_ALLOWED_LANGUAGES"].split(",") | ||
|
||
|
||
def no_search_result(query=None, lang=None): | ||
if query is not None: | ||
logger.info( | ||
"search: no result", | ||
extra={ | ||
"request": { | ||
"query": query, | ||
"lang": lang, | ||
} | ||
}, | ||
) | ||
return IdunnAutocomplete() | ||
|
||
|
||
async def search( | ||
query: QueryParams = Depends(QueryParams), extra: ExtraParams = Body(ExtraParams()) | ||
) -> IdunnAutocomplete: | ||
""" | ||
Perform a query which is intended to display a relevant result directly (as | ||
opposed to `autocomplete` which gives a list of plausible results). | ||
|
||
Similarly to `instant_answer`, the result will need some quality checks. | ||
""" | ||
query.q = normalize(query.q) | ||
|
||
if query.lang in nlu_allowed_languages: | ||
try: | ||
intentions = await nlu_client.get_intentions(text=query.q, lang=query.lang) | ||
if intentions: | ||
return IdunnAutocomplete(intentions=intentions) | ||
except NluClientException: | ||
# No intention could be interpreted from query | ||
pass | ||
|
||
# Direct geocoding query | ||
bragi_response = await bragi_client.autocomplete(query, extra) | ||
features = sorted( | ||
( | ||
(result_filter.rank_bragi_response(query.q, geocoding), feature) | ||
for feature in bragi_response["features"] | ||
for geocoding in [feature["properties"]["geocoding"]] | ||
if result_filter.check_bragi_response(query.q, geocoding) | ||
), | ||
key=lambda item: -item[0], # sort by descending rank | ||
) | ||
|
||
if not features: | ||
return no_search_result(query=query.q, lang=query.lang) | ||
|
||
return IdunnAutocomplete(features=[features[0][1]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from .urlsolver import follow_redirection | ||
from .instant_answer import get_instant_answer, InstantAnswerResponse | ||
from ..places.place import Address, Place | ||
from .search import search | ||
from ..utils.prometheus import ( | ||
expose_metrics, | ||
expose_metrics_multiprocess, | ||
|
@@ -74,6 +75,11 @@ def get_api_urls(settings): | |
response_model=IdunnAutocomplete, | ||
response_model_exclude_unset=True, | ||
), | ||
APIRoute( | ||
"/search", | ||
search, | ||
response_model=IdunnAutocomplete, | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with "/autocomplete", There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and we should allow to provide extra parameters through POST queries 👍 |
||
# Solve URLs | ||
APIRoute( | ||
"/redirect", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from app import app | ||
|
||
from .fixtures.autocomplete import ( | ||
mock_autocomplete_get, | ||
mock_NLU_with_city, | ||
mock_NLU_with_brand_and_city, | ||
mock_bragi_carrefour_in_bbox, | ||
) | ||
|
||
|
||
def test_search_paris(mock_autocomplete_get, mock_NLU_with_city): | ||
client = TestClient(app) | ||
response = client.get("/v1/search", params={"q": "paris", "lang": "fr"}) | ||
assert response.status_code == 200 | ||
response_json = response.json() | ||
place = response_json["features"][0]["properties"]["geocoding"] | ||
assert place["name"] == "Paris" | ||
|
||
|
||
def test_search_intention_full_text(mock_NLU_with_brand_and_city, mock_autocomplete_get): | ||
client = TestClient(app) | ||
response = client.get("/v1/search", params={"q": "auchan à paris", "lang": "fr"}) | ||
assert response.status_code == 200 | ||
response_json = response.json() | ||
intention = response_json["intentions"][0]["filter"] | ||
assert intention["q"] == "auchan" | ||
assert intention["bbox"] is not None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also want to keep this option overridden by AUTOCOMPLETE_NLU_SHADOW_ENABLED, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this option has never been actually used, and is probably not very useful now that the
nlu=true
is used by the application in most cases.So I think this flag
AUTOCOMPLETE_NLU_SHADOW_ENABLED
can be ignored for/search
.