Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #116 from QwantResearch/fetch-by-id-optim
Browse files Browse the repository at this point in the history
Optimize queries to fetch by id in Elasticsearch
  • Loading branch information
amatissart authored Feb 6, 2020
2 parents db398fa + a0ef2f1 commit 67cb0b9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 3 additions & 1 deletion idunn/api/pages_jaunes.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def get_place(self, id):
internal_id = id.replace(f"{self.PLACE_ID_NAMESPACE}:", "", 1)

es_places = self.es.search(
index=self.es_index, body={"filter": {"term": {"_id": internal_id}}}
index=self.es_index,
body={"query": {"bool": {"filter": {"term": {"_id": internal_id}}}}},
ignore_unavailable=True,
)

es_place = es_places.get("hits", {}).get("hits", [])
Expand Down
14 changes: 5 additions & 9 deletions idunn/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,7 @@ def fetch_es_poi(id, es) -> dict:
This function gets from Elasticsearch the
entry corresponding to the given id.
"""
es_pois = es.search(
index=PLACE_POI_INDEX, body={"filter": {"term": {"_id": id}}}, ignore_unavailable=True
)

es_poi = es_pois.get("hits", {}).get("hits", [])
if len(es_poi) == 0:
raise HTTPException(status_code=404, detail=f"poi '{id}' not found")
return es_poi[0]["_source"]
return fetch_es_place(id, es, type="poi")["_source"]


def fetch_bbox_places(es, indices, raw_filters, bbox, max_size) -> list:
Expand Down Expand Up @@ -217,7 +210,10 @@ def fetch_es_place(id, es, type) -> dict:

try:
es_places = es.search(
index=index_name, body={"filter": {"term": {"_id": id}}}, ignore_unavailable=True,
index=index_name,
body={"query": {"bool": {"filter": {"term": {"_id": id}}}}},
ignore_unavailable=True,
_source_exclude=["boundary"],
)
except ElasticsearchException as error:
logger.warning(f"error with database: {error}")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ def test_block_null():
assert resp["blocks"][0]["url"] == "tel:+33142720937"


def test_unknow_poi():
def test_unknown_poi():
client = TestClient(app)
response = client.get(url="http://localhost/v1/pois/an_unknown_poi_id",)

assert response.status_code == 404
assert response.json() == {"detail": "poi 'an_unknown_poi_id' not found"}
assert "'an_unknown_poi_id' not found" in response.json()["detail"]


def test_services_and_information():
Expand Down

0 comments on commit 67cb0b9

Please sign in to comment.