Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit search query option. #2592

Merged
merged 3 commits into from
Aug 7, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/components/Search/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const FUZZY_RANGE_TAG = 'FUZZY_RANGE_TAG';
*/

const BASE_SEARCH = new URLSearchParams();
BASE_SEARCH.append('q', `${REPLACE_TAG}*~${FUZZY_RANGE_TAG}`); // add query replacement tag and enable fuzzy search with ~1
BASE_SEARCH.append('q', `*${REPLACE_TAG}~${FUZZY_RANGE_TAG} OR ${REPLACE_TAG}*~${FUZZY_RANGE_TAG} OR ${REPLACE_TAG}~${FUZZY_RANGE_TAG}`); // add query replacement tag and enable fuzzy search with ~ and wildcards
BASE_SEARCH.append('fq', 'documentKind:ModuleDefinition'); // search for ModuleDefinition documents
BASE_SEARCH.append('rows', '10'); // request 10 results
BASE_SEARCH.append('hl', 'true'); // enable highlight
Expand Down Expand Up @@ -216,7 +216,7 @@ const SearchInput = ({ onStateChange }: SearchInputListener) => {
}, [isOpen, menuRef]);

const handleFetch = (value = '') => {
return fetch(SEARCH_QUERY.replace(REPLACE_TAG, value).replace(FUZZY_RANGE_TAG, value.length > 3 ? '2' : '1'))
return fetch(SEARCH_QUERY.replaceAll(REPLACE_TAG, value).replaceAll(FUZZY_RANGE_TAG, value.length > 3 ? '2' : '1'))
Copy link
Contributor

@vkrizan vkrizan Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the search query build be moved to a separate function?

Can the URL prams be build safely using a URL builder instead of search and replace? For example with URLSearchParams.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query is built with URLSearchParams and then the URL is built with URL classes on the top of the file. The search endpoint currently does not allow encoded query string. It had to be disabled a few months back. Therefore there is no point in rebuilding the query on each request and the creating was lifted out of the callback to be static and save some resources.

Currently, the replace method is as good as using the search params constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've missed the use of URLSearchParams.

Do we escape special characters within the search query?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, the search API does not support encoded/escaped characters. You can see that we actually have to decode the query params before using them in the query. My assumption is that the API requires special characters like * or ~ to build a valid SOLR query. But you would have to ask the search team for details.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which API does not support encoded URI (query string)? Is that SOLR? Query string is part of the standard of RFC3986.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the references:

@Hyperkid123 do you have more info on the reason or can you ask the search team please? Was it reported to them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to send encoded ampersand %26 to stage API, it does not fail. However, not encoding these characters leads to HTTP 400.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have any formal records. Discussion with the search team happened on a chat. I understand the encoded strings are part of the spec. The hydra API was returning 400 responses when we tried to send encoded queries.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed with Martin offline. IMO we will need to follow up on the URL encoding in a separate pull request - that way we can revert either (or both) PRs if there is an issue. It seems the search team fixed an issue with the Hydra REST endpoint that was originally causing the 400's (see slack thread). We should now be able to send the encoded special characters in the SOLR queries.

.then((r) => r.json())
.then(({ response, highlighting }: { highlighting: HighlightingResponseType; response: SearchResponseType }) => {
if (isMounted.current) {
Expand Down
Loading