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

Allow exact search in REST API #3677

Closed
wants to merge 13 commits into from

Conversation

mehulkaklotar
Copy link
Member

Trac ticket: https://core.trac.wordpress.org/ticket/56350


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

$this->assertCount( 1, $data, 'Querying the API with exact_search should return posts matching the search keyword' );

// Note that "exact_search" is still true.
$request->set_param( 'search', 'Rye Bread' );
Copy link
Member

Choose a reason for hiding this comment

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

What result do you get when you search Types of Bread?

Copy link
Contributor

Choose a reason for hiding this comment

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

Much like /?s=Types+of+Bread&exact=1, this will not return a match. This applies to any exact search in which the search term includes a space.

Copy link
Member Author

Choose a reason for hiding this comment

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

When we exact search the one word, it will return the posts matching exactly, but if there are multiple words to exact search, it doesn't work actually. so would it make sense to introduce sentence as well in the REST api?

Copy link
Contributor

Choose a reason for hiding this comment

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

@TimothyBJacobs What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@mukeshpanchal27
Copy link
Member

@ironprogrammer @TimothyBJacobs What is your thoughts on #3677 (comment)?

@@ -251,6 +251,7 @@ public function get_items( $request ) {
'search' => 's',
'slug' => 'post_name__in',
'status' => 'post_status',
'exact_search' => 'exact',
Copy link
Contributor

Choose a reason for hiding this comment

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

These appear to be organised alphabetically by key, so the new exact_search key should be placed above exclude.

Copy link
Member Author

Choose a reason for hiding this comment

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

@costdev I have updated the placement for exact_search now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @mehulkaklotar!

Copy link

github-actions bot commented Sep 17, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mehulkaklotar, flixos90, mukesh27, costdev, timothyblynjacobs.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@TimothyBJacobs TimothyBJacobs left a comment

Choose a reason for hiding this comment

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

this looks good to me! we just need to regenerate wp-api-generated.js

@TimothyBJacobs
Copy link
Member

Hm, though looking at this, if we do want to add sentence support, we probably want to change this to be something like search_semantics as an enum of either exact or sentence? So you could do search_semantics=exact,sentence?

@felixarntz
Copy link
Member

@TimothyBJacobs At this point, we'd probably only support the exact value though, right (as for this PR)? I'm okay choosing a future compatible path here, that said we could also go with the current pragmatic approach and if we want to support more later change it (e.g. deprecate the current one). What do you think?

@TimothyBJacobs
Copy link
Member

Yeah, I don't think this is bad as is. A search_semantics style parameter is more forward compatible. I don't think we've officially deprecated any actual query parameters yet. But I don't think a search_exact and search_sentence is the worst thing.

@felixarntz
Copy link
Member

@TimothyBJacobs Updated to use search_semantics enum parameter in 15ee682.

@@ -337,6 +337,13 @@ public function get_items( $request ) {
}
}

if (
isset( $registered['search_semantics'], $request['search_semantics'] )
Copy link
Member

Choose a reason for hiding this comment

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

We don't technically need the isset check since the rest request object will just return null in this case.

Copy link
Member

Choose a reason for hiding this comment

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

Fair. I feel like it's okay keeping though for consistency. Maybe also a tiny bit safer.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me.

@TimothyBJacobs TimothyBJacobs self-requested a review September 17, 2024 21:11
@felixarntz
Copy link
Member

Committed in https://core.trac.wordpress.org/changeset/59034

@felixarntz felixarntz closed this Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants