Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/news/101.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@solr-suggest accepts an optional path_prefix (path_parents filter), so livesearch suggestions honor a subtree scope like the search results do. @reebalazs
7 changes: 7 additions & 0 deletions backend/src/kitconcept/solr/services/suggest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def query_suggest(self, query):
if lang:
d["fq"] = d["fq"] + ["Language:(" + escape(lang) + ")"]

# Optional path scoping: restricts suggestions to a subtree,
# e.g. a subsite or workspace, matching the local search.
if path_prefix := self.request.form.get("path_prefix", "").strip():
portal_path = "/".join(api.portal.get().getPhysicalPath())
prefix = portal_path + path_prefix.rstrip("/")
d["fq"] = d["fq"] + [f'path_parents:"{prefix}"']

d["fq"] = " AND ".join(d["fq"])
querystring = urllib.parse.urlencode(d)
url = "{}/{}".format(connection.solrBase, f"suggest?{querystring}")
Expand Down
10 changes: 10 additions & 0 deletions backend/tests/services/suggest/test_suggest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ def test_suggest_result_props(
get_suggest_result_props(expected_dict).items()
<= get_suggest_result_props(get_suggest_item(self.data, index)).items()
)


class TestSuggestPathPrefix(TestSuggestDefault):
"""path_prefix restricts suggestions to a subtree (e.g. a workspace)."""

url = "/@solr-suggest?query=chomsky&path_prefix=/mydocument"

def test_only_prefixed_results(self, get_suggest_result_path):
paths = [get_suggest_result_path(item) for item in self.data["suggestions"]]
assert paths == ["/plone/mydocument"]
1 change: 1 addition & 0 deletions frontend/packages/volto-solr/news/101.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Local search fixes: @solr-suggest accepts an optional path_prefix so suggestions honor a subtree scope, and an explicit path_prefix URL param wins over the getPathPrefix heuristic, which silently dropped top-level (single-segment) prefixes. @reebalazs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
export const GET_SOLR_SEARCH_SUGGESTIONS = 'GET_SOLR_SEARCH_SUGGESTIONS';

export function solrSearchSuggestions(term) {
/**
* Fetch live search suggestions.
* @param {string} term The (url-encoded) search term.
* @param {string=} pathPrefix Optional path to restrict the suggestions
* to a subtree (e.g. a subsite or workspace).
*/
export function solrSearchSuggestions(term, pathPrefix) {
const params = [`query=${term}`];
if (pathPrefix) {
params.push(`path_prefix=${encodeURIComponent(pathPrefix)}`);
}
return {
type: GET_SOLR_SEARCH_SUGGESTIONS,
request: {
op: 'get',
path: `/@solr-suggest?query=${term}`,
path: `/@solr-suggest?${params.join('&')}`,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,18 @@ class SolrSearch extends Component {
...params,
sort_on: params.sort_on !== 'relevance' ? params.sort_on : '',
b_start: (this.state.currentPage - 1) * config.settings.defaultPageSize,
path_prefix: getPathPrefix(window.location),
path_prefix: this.searchPathPrefix(params),
doEmptySearch: this.props.doEmptySearch,
});
};

// An explicit path_prefix URL param wins over the URL heuristic:
// getPathPrefix treats every single-segment path as a language root
// (/de, /en), so a top-level subsite or workspace (/my-workspace)
// would silently lose its prefix.
searchPathPrefix = (params) =>
params.path_prefix || getPathPrefix(window.location);

updateSearch = () => {
this.props.history.replace({
search: qs.stringify(queryStateToParams(this.state)),
Expand Down
Loading