Skip to content

Commit

Permalink
Refactor/search enhancement (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfagundes authored Aug 15, 2023
1 parent 5a3d4e7 commit de6f1cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
13 changes: 2 additions & 11 deletions basedosdados_api/api/v1/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DatasetIndex(indexes.SearchIndex, indexes.Indexable):

organization_id = indexes.CharField(model_attr="organization__id", null=True)
organization_slug = indexes.CharField(model_attr="organization__slug")
organization_name = indexes.CharField(model_attr="organization__name")
organization_name = indexes.EdgeNgramField(model_attr="organization__name")
organization_description = indexes.CharField(
model_attr="organization__description", null=True
)
Expand All @@ -27,7 +27,7 @@ class DatasetIndex(indexes.SearchIndex, indexes.Indexable):

table_ids = indexes.MultiValueField(model_attr="tables__id", null=True)
table_slugs = indexes.MultiValueField(model_attr="tables__slug", null=True)
table_names = indexes.MultiValueField(model_attr="tables__name", null=True)
table_names = indexes.EdgeNgramField(model_attr="tables__name", null=True)
table_descriptions = indexes.EdgeNgramField(
model_attr="tables__description", null=True
)
Expand Down Expand Up @@ -142,15 +142,6 @@ def prepare(self, obj):
table_ids = data.get("table_slugs", [])
if table_ids:
data["tables"] = []
for i in range(len(table_ids)):
data["tables"].append(
{
"id": data.get("table_ids", [])[i],
"name": data.get("table_names", [])[i],
"slug": data.get("table_slugs", [])[i],
"is_closed": data.get("table_is_closed", [])[i],
}
)
data["total_tables"] = len(table_ids)
else:
data["total_tables"] = 0
Expand Down
11 changes: 9 additions & 2 deletions basedosdados_api/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ def get(self, request, *args, **kwargs):

storage = get_storage_class()

# If query is empty, query all datasets
if not query:
# If query is empty, query all datasets
query = {"match_all": {}}
# Factor to multiply the number of tables by
# Has no effect if no query is passed
n_table_factor = 0
else:
# If query is not empty, query datasets and tables
query = {
"bool": {
"should": [
Expand All @@ -50,9 +54,12 @@ def get(self, request, *args, **kwargs):
}
},
{"match": {"name.edgengram": query}},
{"match": {"table_names.edgengram": query}},
{"match": {"organization_name.edgengram": query}},
]
}
}
n_table_factor = 2

all_filters = []

Expand Down Expand Up @@ -147,7 +154,7 @@ def get(self, request, *args, **kwargs):
"field_value_factor": {
"field": "n_tables",
"modifier": "square",
"factor": 2,
"factor": n_table_factor,
"missing": 0,
}
},
Expand Down

0 comments on commit de6f1cf

Please sign in to comment.