Opened 14 months ago
Last modified 10 months ago
#56350 new enhancement
Allow exact search in REST API
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | good-first-bug has-patch needs-testing has-unit-tests has-testing-info |
Focuses: | rest-api, performance | Cc: |
Description
Wordpress only allows full text search when using the REST API but doesn't allow exact search. Yet, the exact search is possible using a search query like:
http://website.com/?s=keyword&exact=1
When you have tens of thousands of posts, being forced to do a full text search can take several seconds and impacts the performance of the website.
It would be an improvement to allow exact search in the WP_REST_Posts_Controller in order to allow REST API requests like:
http://website.com/wp-json/wp/v2/posts?s=keyword&exact=1
Attachments (5)
Change History (17)
#1
@
14 months ago
- Keywords good-first-bug added
- Milestone changed from Awaiting Review to Future Release
- Version changed from trunk to 4.7
#2
@
14 months ago
Hi @TimothyBlynJacobs,
I created a patch and used the term exact_search like you suggested.
#3
@
14 months ago
- Keywords has-patch needs-unit-tests added
This looks good @jimmyh61! Are you interested in working on unit tests for this issue?
#4
@
14 months ago
@TimothyBlynJacobs,
I'm not familiar with unit tests. Can somebody else work on that?
#7
@
14 months ago
- Keywords has-unit-tests added
Great collaboration, @jimmyh61 and @johnregan3!
@johnregan3, would you be able to add "optional" $message
params to the assertions in 56350.tests.improved.diff?
Why? Because the test contains multiple assertions, it's ideal for error messages to indicate why a particular assertion failed.
I'm not certain the third assertion is required, since that seems more like a test that $request
didn't reset, which isn't the class under test in this context.
#8
@
14 months ago
- Keywords has-testing-info added; needs-testing-info removed
Test Report
This combined test report includes testing instructions and enhancement patch test results.
Patch tested: https://core.trac.wordpress.org/attachment/ticket/56350/56350.diff
LGTM 👍🏻 When it lands, it will need needs-user-docs
.
Steps to Test
(Exact post content is at tester's discretion; samples provided are from the unit test. Just ensure that each post's title is unique and that there is a shared keyword between the content.)
- Create and publish a new post with title "Rye", and content "This is a post about Rye Bread".
- Create and publish a second post with title "Types of Bread", and content "Types of bread are White and Rye Bread". Note that the keyword "rye" is shared between the two posts.
- Regression Test: In a browser load the following URL:
<your site>/wp-json/wp/v2/posts?search=rye
(The REST API search
parameter corresponds to the WP_Query
s
parameter.)
- ✅ The JSON results should contain at least two (2) results, more if you have other posts containing "rye" (a JSON viewer extension may be helpful to view the results here).
- Enhancement Test: In a browser load the following URL:
<your site>/wp-json/wp/v2/posts?search=rye&exact_search=true
(The patch's exact_search
REST API parameter corresponds to the WP_Query
exact
parameter.)
- ✅ The exact search JSON results should contain just one (1) result: the "Rye" post.
Expected Results
- ✅ Querying the API without the new API parameter should return all posts containing the search keyword.
- ✅ Querying the API with
exact_search
should only return the post name matching the search keyword.
Environment
- OS: macOS 12.5
- Browser: Safari 15.6
- Browser: Google Chrome 104.0.5112.79
- Server: nginx/1.23.1
- PHP: 7.4.30
- WordPress: 6.1-alpha-53344-src
Actual Results
After applying patch...
- ✅ Querying the API without the new API parameter returned all posts containing the search keyword.
- ✅ Querying the API with
exact_search
only returned the post name matching the search keyword.
#9
@
13 months ago
Great collaboration!
I reproduced the steps to test and got the same results. Everything seems to work as expected.
#10
@
10 months ago
I have replicated the steps and got the correct results every time. Looks good on my end too. There were tests errors in the commit though.
I have pushed fixed changes in the PR now to test it with different versions of PHP.
This ticket was mentioned in PR #3677 on WordPress/wordpress-develop by @mehulkaklotar.
10 months ago
#11
Trac ticket: https://core.trac.wordpress.org/ticket/56350
@mukesh27 commented on PR #3677:
10 months ago
#12
@ironprogrammer @TimothyBJacobs What is your thoughts on https://github.com/WordPress/wordpress-develop/pull/3677#discussion_r1032178888?
Welcome to Trac @jimmyh61!
This seems sensible to me. Instead of just
exact
, I think we'd want to be a bit more specific, eitherexact_search
orsearch_exact
.Do you want to work on a patch for thisr issue?