Make WordPress Core


Ignore:
Timestamp:
09/17/2024 09:56:18 PM (4 weeks ago)
Author:
flixos90
Message:

REST API: Support exact search in the REST API posts endpoint.

This changeset adds support for a new search_semantics enum query parameter that can be passed alongside the search string parameter. At this point, it only supports "exact" as possible value, but an enum is used for forward compatibility with potential enhancements like "sentence" search support. If search_semantics=exact is passed, it will look for an exact match rather than do a full text search, which for some use-cases is more appropriate and more performant.

Props mehulkaklotar, timothyblynjacobs, jimmyh61, ironprogrammer, johnregan3, mukesh27, costdev.
Fixes #56350.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r58326 r59034  
    207207                'search',
    208208                'search_columns',
     209                'search_semantics',
    209210                'slug',
    210211                'status',
     
    764765            $this->assertNotEquals( $draft_id, $post['id'] );
    765766        }
     767    }
     768
     769    /**
     770     * @ticket 56350
     771     *
     772     * @dataProvider data_get_items_exact_search
     773     *
     774     * @param string $search_term  The search term.
     775     * @param bool   $exact_search Whether the search is an exact or general search.
     776     * @param int    $expected     The expected number of matching posts.
     777     */
     778    public function test_get_items_exact_search( $search_term, $exact_search, $expected ) {
     779        self::factory()->post->create(
     780            array(
     781                'post_title'   => 'Rye',
     782                'post_content' => 'This is a post about Rye Bread',
     783            )
     784        );
     785
     786        self::factory()->post->create(
     787            array(
     788                'post_title'   => 'Types of Bread',
     789                'post_content' => 'Types of bread are White and Rye Bread',
     790            )
     791        );
     792
     793        $request           = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     794        $request['search'] = $search_term;
     795        if ( $exact_search ) {
     796            $request['search_semantics'] = 'exact';
     797        }
     798        $response = rest_get_server()->dispatch( $request );
     799        $this->assertCount( $expected, $response->get_data() );
     800    }
     801
     802    /**
     803     * Data provider for test_get_items_exact_search().
     804     *
     805     * @return array[]
     806     */
     807    public function data_get_items_exact_search() {
     808        return array(
     809            'general search, one exact match and one partial match' => array(
     810                'search_term'  => 'Rye',
     811                'exact_search' => false,
     812                'expected'     => 2,
     813            ),
     814            'exact search, one exact match and one partial match' => array(
     815                'search_term'  => 'Rye',
     816                'exact_search' => true,
     817                'expected'     => 1,
     818            ),
     819            'exact search, no match and one partial match' => array(
     820                'search_term'  => 'Rye Bread',
     821                'exact_search' => true,
     822                'expected'     => 0,
     823            ),
     824        );
    766825    }
    767826
Note: See TracChangeset for help on using the changeset viewer.