Skip to content

Commit

Permalink
fix: support None in facet doc count
Browse files Browse the repository at this point in the history
Also sort selected facets first
  • Loading branch information
alexgarel committed Jun 4, 2024
1 parent 7665315 commit ae62e30
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FacetItem(BaseModel):

key: str
name: str
count: int
count: Optional[int]
"""The number of elements for this value"""

selected: bool
Expand Down
40 changes: 27 additions & 13 deletions app/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,37 @@ def build_facets(
# TODO: translate in target language ?
name="Other",
count=agg_data["sum_other_doc_count"],
selected=False,
)
)
items_count = sum(item.count or 0 for item in facet_items)
# 2024-06-04: Alex: removed as we don't support it yet
# at request construction time
#
# items_count = sum(item.count or 0 for item in facet_items)
# and empty values if there are some (taking into account error margin)
if (not search_result.is_count_exact) and search_result.count > (
items_count + count_error_margin
):
facet_items.append(
FacetItem(
key="--none--",
# TODO: translate in target language ?
name="None",
# Note: this depends on search_result.is_count_exact,
# but we leave it to user to verify
count=search_result.count - items_count,
)
# if (not search_result.is_count_exact) and search_result.count > (
# items_count + count_error_margin
# ):
# facet_items.append(
# FacetItem(
# key="--none--",
# # TODO: translate in target language ?
# name="None",
# # Note: this depends on search_result.is_count_exact,
# # but we leave it to user to verify
# count=search_result.count - items_count,
# # FIXME: compute selected !
# selected=False,
# )
# )
# re-order to have selected first
facet_items = [
item
for i, item in sorted(
enumerate(facet_items),
key=lambda i_item: (not i_item[1].selected, i_item[0]),
)
]
# append our facet information
facets[field_name] = FacetInfo(
# FIXME translate field name in target language
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/search-facets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {LitElement, html, css} from 'lit';
import {LitElement, html, nothing, css} from 'lit';
import {customElement, property, queryAssignedNodes} from 'lit/decorators.js';
import {repeat} from 'lit/directives/repeat.js';

Expand Down Expand Up @@ -158,7 +158,11 @@ export class SearchaliciousTermsFacet extends SearchaliciousFacet {
* Create the search term based upon the selected terms
*/
override searchFilter(): string | undefined {
const values = Object.keys(this.selectedTerms);
let values = Object.keys(this.selectedTerms);
// add quotes if we have ":" in values
values = values.map((value) =>
value.includes(':') ? `"${value}"` : value
);
if (values.length === 0) {
return undefined;
}
Expand All @@ -181,7 +185,10 @@ export class SearchaliciousTermsFacet extends SearchaliciousFacet {
?checked=${this.selectedTerms[term.key]}
@change=${this.setTermSelected}
/><label for="${term.key}"
>${term.name} <span part="docCount">(${term.count})</span></label
>${term.name}
${term.count
? html`<span part="docCount">(${term.count})</span>`
: nothing}</label
>
</div>
`;
Expand Down

0 comments on commit ae62e30

Please sign in to comment.