Skip to content

Commit

Permalink
Merge branch 'modularization-main' into feature/sc-27901/card-box-com…
Browse files Browse the repository at this point in the history
…ponent
  • Loading branch information
stevekaplan123 committed Sep 9, 2024
2 parents 6b998a2 + 5563654 commit 486330f
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 143 deletions.
43 changes: 22 additions & 21 deletions reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,24 +785,28 @@ def get_filters(prefix, filter_type):
return [urllib.parse.unquote(f) for f in get_dict.get(get_param(prefix+filter_type+"Filters", i)).split("|")] if get_dict.get(get_param(prefix+filter_type+"Filters", i), "") else []

sheet_filters_types = ("collections", "topics_en", "topics_he")
sheet_filters = []
sheet_agg_types = []
for filter_type in sheet_filters_types:
filters = get_filters("s", filter_type)
sheet_filters += filters
sheet_agg_types += [filter_type] * len(filters)
text_filters = get_filters("t", "path")
filters = []
agg_types = []
sort = None
field = None
if get_dict.get('tab') == 'text':
filters = get_filters("t", "path")
sort = get_dict.get(get_param("tsort", i), None)
agg_types = [None for _ in filters] # currently unused. just needs to be equal len as filters
field = ("naive_lemmatizer" if get_dict.get(get_param("tvar", i)) == "1" else "exact") if get_dict.get(get_param("tvar", i)) else ""
else:
for filter_type in sheet_filters_types:
filters += get_filters("s", filter_type)
agg_types += [filter_type] * len(filters)
sort = get_dict.get(get_param("ssort", i), None)

return {
"query": urllib.parse.unquote(get_dict.get(get_param("q", i), "")),
"tab": urllib.parse.unquote(get_dict.get(get_param("tab", i), "text")),
"textField": ("naive_lemmatizer" if get_dict.get(get_param("tvar", i)) == "1" else "exact") if get_dict.get(get_param("tvar", i)) else "",
"textSort": get_dict.get(get_param("tsort", i), None),
"textFilters": text_filters,
"textFilterAggTypes": [None for _ in text_filters], # currently unused. just needs to be equal len as text_filters
"sheetSort": get_dict.get(get_param("ssort", i), None),
"sheetFilters": sheet_filters,
"sheetFilterAggTypes": sheet_agg_types,
"field": field,
"sort": sort,
"filters": filters,
"filterAggTypes": agg_types,
}


Expand Down Expand Up @@ -840,13 +844,10 @@ def search(request):
"initialMenu": "search",
"initialQuery": search_params["query"],
"initialSearchType": search_params["tab"],
"initialTextSearchFilters": search_params["textFilters"],
"initialTextSearchFilterAggTypes": search_params["textFilterAggTypes"],
"initialTextSearchField": search_params["textField"],
"initialTextSearchSortType": search_params["textSort"],
"initialSheetSearchFilters": search_params["sheetFilters"],
"initialSheetSearchFilterAggTypes": search_params["sheetFilterAggTypes"],
"initialSheetSearchSortType": search_params["sheetSort"]
"initialSearchFilters": search_params["filters"],
"initialSearchFilterAggTypes": search_params["filterAggTypes"],
"initialSearchField": search_params["field"],
"initialSearchSortType": search_params["sort"],
}
return render_template(request,'base.html', props, {
"title": (search_params["query"] + " | " if search_params["query"] else "") + _("Sefaria Search"),
Expand Down
9 changes: 5 additions & 4 deletions sourcesheets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ def sheets_by_ref_api(request, ref):
if include_collections:
sheets = annotate_sheets_with_collections(sheets)
return jsonResponse(sheets)

def sheets_with_ref(request, tref):
"""
Accepts tref as a string which is expected to be in the format of a ref or refs separated by commas, indicating a range.
Expand All @@ -1042,10 +1043,10 @@ def sheets_with_ref(request, tref):

props={
"initialSearchType": "sheet",
"initialTextSearchField": search_params["textField"],
"initialSheetSearchFilters": search_params["sheetFilters"],
"initialSheetSearchFilterAggTypes": search_params["sheetFilterAggTypes"],
"initialSheetSearchSortType": search_params["sheetSort"]
"initialSearchField": search_params["field"],
"initialSearchFilters": search_params["filters"],
"initialSearchFilterAggTypes": search_params["filterAggTypes"],
"initialSearchSortType": search_params["sort"]
}
he_tref = Ref(tref).he_normal()
normal_ref = tref if request.interfaceLang == "english" else he_tref
Expand Down
67 changes: 36 additions & 31 deletions static/js/ElasticSearchQuerier.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class TopicQuerier {
return searchTopic;
}
}

class ElasticSearchQuerier extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -115,8 +116,8 @@ class ElasticSearchQuerier extends Component {
this.state.hits = this.state.hits.concat(cachedQuery.hits.hits);
this.state.totals = cachedQuery.hits.total;
this.state.pagesLoaded += 1;
args.start = this.state.pagesLoaded * this.querySize[this.props.type];
if (this.props.type === "text") {
args.start = this.state.pagesLoaded * this.querySize[this.props.searchState.type];
if (this.props.searchState.type === "text") {
// Since texts only have one filter type, aggregations are only requested once on first page
args.aggregationsToUpdate = [];
}
Expand All @@ -130,20 +131,24 @@ class ElasticSearchQuerier extends Component {
this._abortRunningQuery(); // todo: make this work w/ promises
}
componentWillReceiveProps(newProps) {
let state = {
hits: [],
pagesLoaded: 0,
moreToLoad: true
};
if (this.props.query !== newProps.query) {
this.setState(state, () => this._executeAllQueries(newProps));
} else if (this._shouldUpdateQuery(this.props, newProps, this.props.type)) {
this.setState(state, () => {
this._executeQuery(newProps, this.props.type);
})
}
let state = {
hits: [],
pagesLoaded: 0,
moreToLoad: true
};
if (this.props.query !== newProps.query) {
this.setState(state, () => {
this._executeAllQueries(newProps);
if (!this.props.searchInBook) {
this.props.resetSearchFilters();
}
});
} else if (this._shouldUpdateQuery(this.props, newProps, this.props.searchState.type)) {
this.setState(state, () => {
this._executeQuery(newProps, this.props.searchState.type);
})
}
}

async _executeTopicQuery() {
const topicQuerier = new TopicQuerier();
const d = await Sefaria.getName(this.props.query)
Expand Down Expand Up @@ -193,7 +198,7 @@ class ElasticSearchQuerier extends Component {
if (!this.props.searchInBook) {
this._executeTopicQuery();
}
this._executeQuery(props, this.props.type);
this._executeQuery(props, this.props.searchState.type);
}
_getAggsToUpdate(filtersValid, aggregation_field_array, aggregation_field_lang_suffix_array, appliedFilterAggTypes, type) {
// Returns a list of aggregations type which we should request from the server.
Expand Down Expand Up @@ -222,7 +227,7 @@ class ElasticSearchQuerier extends Component {
const request_applied = args.applied_filters;
const searchState = this._getSearchState(props);
const { appliedFilters, appliedFilterAggTypes } = searchState;
const { aggregation_field_array, build_and_apply_filters } = SearchState.metadataByType[this.props.type];
const { aggregation_field_array, build_and_apply_filters } = SearchState.metadataByType[this.props.searchState.type];

args.success = data => {
this.updateRunningQuery(null);
Expand All @@ -232,12 +237,12 @@ class ElasticSearchQuerier extends Component {
hits: data.hits.hits,
totals: currTotal,
pagesLoaded: 1,
moreToLoad: currTotal.getValue() > this.querySize[this.props.type]
moreToLoad: currTotal.getValue() > this.querySize[this.props.searchState.type]
};
this.setState(state);
const filter_label = (request_applied && request_applied.length > 0) ? (' - ' + request_applied.join('|')) : '';
const query_label = props.query + filter_label;
Sefaria.track.event("Search", `${this.props.searchInBook? "SidebarSearch ": ""}Query: ${this.props.type}`, query_label, data.hits.total.getValue());
Sefaria.track.event("Search", `${this.props.searchInBook? "SidebarSearch ": ""}Query: ${this.props.searchState.type}`, query_label, data.hits.total.getValue());
}

if (data.aggregations) {
Expand All @@ -257,7 +262,7 @@ class ElasticSearchQuerier extends Component {
orphans.push(...tempOrphans);
}
}
this.props.registerAvailableFilters(this.props.type, availableFilters, registry, orphans, args.aggregationsToUpdate);
this.props.registerAvailableFilters(availableFilters, registry, orphans, args.aggregationsToUpdate);
}
};
args.error = this._handleError;
Expand All @@ -271,16 +276,16 @@ class ElasticSearchQuerier extends Component {
const searchState = this._getSearchState(props);
const { field, fieldExact, sortType, filtersValid, appliedFilters, appliedFilterAggTypes } = searchState;
const request_applied = filtersValid && appliedFilters;
const { aggregation_field_array, aggregation_field_lang_suffix_array } = SearchState.metadataByType[this.props.type];
const aggregationsToUpdate = this._getAggsToUpdate(filtersValid, aggregation_field_array, aggregation_field_lang_suffix_array, appliedFilterAggTypes, this.props.type);
const { aggregation_field_array, aggregation_field_lang_suffix_array } = SearchState.metadataByType[this.props.searchState.type];
const aggregationsToUpdate = this._getAggsToUpdate(filtersValid, aggregation_field_array, aggregation_field_lang_suffix_array, appliedFilterAggTypes, this.props.searchState.type);

return {
query: props.query,
type: this.props.type,
type: this.props.searchState.type,
applied_filters: request_applied,
appliedFilterAggTypes,
aggregationsToUpdate,
size: this.querySize[this.props.type],
size: this.querySize[this.props.searchState.type],
field,
sort_type: sortType,
exact: fieldExact === field,
Expand All @@ -289,14 +294,14 @@ class ElasticSearchQuerier extends Component {
_loadNextPage() {
console.log("load next page")
const args = this._getQueryArgs(this.props);
args.start = this.state.pagesLoaded * this.querySize[this.props.type];
args.start = this.state.pagesLoaded * this.querySize[this.props.searchState.type];
args.error = () => console.log("Failure in SearchResultList._loadNextPage");
args.success = data => {
let nextHits = this.state.hits.concat(data.hits.hits);

this.state.hits = nextHits;
this.state.pagesLoaded += 1;
if (this.state.pagesLoaded * this.querySize[this.props.type] >= this.state.totals.getValue() ) {
if (this.state.pagesLoaded * this.querySize[this.props.searchState.type] >= this.state.totals.getValue() ) {
this.state.moreToLoad = false;
}

Expand All @@ -317,7 +322,7 @@ class ElasticSearchQuerier extends Component {
this.updateRunningQuery(null);
}
normalizeHitsMetaData() {
if (this.props.type === 'sheet') {
if (this.props.searchState.type === 'sheet') {
let results = this.state.hits;
return results.map(result => {
let normalizedResult = result._source;
Expand All @@ -336,10 +341,10 @@ class ElasticSearchQuerier extends Component {
isQueryRunning={this.state.isQueryRunning}
searchTopMsg="Results for"
query={this.props.query}
sortTypeArray={SearchState.metadataByType[this.props.type].sortTypeArray}
sortTypeArray={SearchState.metadataByType[this.props.searchState.type].sortTypeArray}
hits={this.normalizeHitsMetaData()}
totalResults={this.state.totals}
type={this.props.type}
type={this.props.searchState.type}
searchState={this.props.searchState}
settings={this.props.settings}
panelsOpen={this.props.panelsOpen}
Expand All @@ -362,7 +367,6 @@ class ElasticSearchQuerier extends Component {

ElasticSearchQuerier.propTypes = {
query: PropTypes.string,
type: PropTypes.oneOf(["text", "sheet"]),
searchState: PropTypes.object,
onResultClick: PropTypes.func,
registerAvailableFilters: PropTypes.func,
Expand All @@ -375,7 +379,8 @@ ElasticSearchQuerier.propTypes = {
onQueryChange: PropTypes.func,
updateAppliedFilter: PropTypes.func,
updateAppliedOptionSort: PropTypes.func,
updateAppliedOptionField: PropTypes.func
updateAppliedOptionField: PropTypes.func,
resetSearchFilters: PropTypes.func
};

export { ElasticSearchQuerier };
Loading

0 comments on commit 486330f

Please sign in to comment.