internetarchive / internetarchive/openlibrary

Subject search mishandles special characters like # and +

Open
#12,799 6 comments 0 reactions 1 assignee Claimed by @RsbhThakur View on GitHub
Lead: @cdrini Needs: Review Assignee Priority: 3 Theme: Search Theme: Subjects Type: Bug
Dominant language
Python
Stars
6.7k
Forks
2k
Avg merge
2d 19h
Merged PRs (30d)
138

Description

### Problem

Subject search appears to mishandle subjects containing special characters.

I found two related examples on production:

1. Searching Subjects for `C#` shows a `C#` result, but clicking it opens the wrong subject page because the link is generated as `/subjects/c#`.
2. Searching Subjects for `C++` returns mostly unrelated results, including `Washington (D.C.)`, suggesting the `+` characters are not being handled as literal search text.

I’d like to work on this issue if maintainers agree this is a valid bug.

### Reproducing the bug

Case 1: `C#` subject link

1. Go to https://openlibrary.org/search/subjects?q=C%23
2. Click the `C#` subject result

* Expected behavior: The `C#` subject page opens.
* Actual behavior: The browser opens `https://openlibrary.org/subjects/c#`, which is interpreted as `/subjects/c` plus a URL fragment, and it shows the wrong subject page.

Case 2: `C++` subject search

1. Go to https://openlibrary.org/search/subjects?q=C%2B%2B

* Expected behavior: Results should prioritize subjects matching `C++`, such as `C++ (Computer program language)`.
* Actual behavior: The search returns 29,705 hits, with unrelated top results like `Washington (D.C.)` and `To 221 B.C.`.

### Context

- Browser (Chrome, Safari, Firefox, etc):
- OS (Windows, Mac, etc):
- Logged in (Y/N):
- Environment (prod, dev, local): prod

Notes:
This looks related to special-character handling in subject search. The `C#` case may be caused by subject result URLs being built from display names without URL-encoding `#`. The `C++` case may be caused by `+` not being preserved as a literal subject query.

### Breakdown

Implementation Details (for maintainers)

#### Requirements Checklist
* [ ]

#### Related files

*

#### Stakeholders

*


#### Instructions for Contributors

- Please [run these commands](https://docs.openlibrary.org/developers/tools/git.html#working-on-your-branch) to ensure your repository is up to date **before** [creating a new branch](https://docs.openlibrary.org/developers/tools/git.html#making-changes-and-creating-a-pull-request) to work on this issue and **each time after** pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.