Skip to content

fix(did-you-mean): include post_title in phrase suggester#4331

Open
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/3554-did-you-mean-title-shingle
Open

fix(did-you-mean): include post_title in phrase suggester#4331
faisalahammad wants to merge 1 commit into
10up:developfrom
faisalahammad:fix/3554-did-you-mean-title-shingle

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

Did You Mean only used post_content.shingle, so suggestions missed keywords that live in post_title — like WooCommerce product names. This fix adds an ep_did_you_mean field that combines post_title and post_content via copy_to, and switches the phrase suggester to use that field.

Fixes #3554

Changes

includes/classes/Feature/DidYouMean/DidYouMean.php

Before:

$mapping['mappings']['properties']['post_content']['fields'] = [
    'shingle' => [
        'type'     => 'text',
        'analyzer' => 'trigram',
    ],
];
$search_analyzer = [
    'phrase' => [
        'field'            => 'post_content.shingle',
        'max_errors'       => 2,
        'direct_generator' => [
            [
                'field' => 'post_content.shingle',
            ],
        ],
    ],
];

After:

foreach ( [ 'post_content', 'post_title' ] as $source_field ) {
    $existing_copy_to = $mapping_properties[ $source_field ]['copy_to'] ?? [];
    $existing_copy_to = is_array( $existing_copy_to ) ? $existing_copy_to : [ $existing_copy_to ];

    if ( ! in_array( 'ep_did_you_mean', $existing_copy_to, true ) ) {
        $existing_copy_to[] = 'ep_did_you_mean';
    }

    $mapping_properties[ $source_field ]['copy_to'] = $existing_copy_to;
}

$mapping_properties['ep_did_you_mean'] = [
    'type'     => 'text',
    'analyzer' => 'trigram',
];
$search_analyzer = [
    'phrase' => [
        'field'            => 'ep_did_you_mean',
        'max_errors'       => 2,
        'direct_generator' => [
            [
                'field' => 'ep_did_you_mean',
            ],
        ],
    ],
];

Why: The suggester now has access to the same keywords the search query uses, so misspelled title terms like shir can suggest shirt.

tests/php/features/TestDidYouMean.php

Before: mapping tests only checked post_content.shingle, and there was no test for title-only suggestions.

After: mapping tests now assert the ep_did_you_mean field and copy_to mappings exist, and two new tests verify suggestions from post_title.

Why: Prevents regression and verifies the reported scenario.

Testing

Test 1: Title-only suggestion

  1. Create a post with title Sleek Cotton Shirt and empty content.
  2. Sync the index and search for shir.
    Result: suggested term is shirt.

Test 2: Existing content suggestions still work

  1. Create a post with content Test post.
  2. Search for teet.
    Result: suggested term is test.

Automated:

EP_HOST=http://127.0.0.1:8890/ vendor/bin/phpunit --filter TestDidYouMean

All 19 tests pass.

- Adds an ep_did_you_mean field combining post_title and post_content via copy_to.
- Switches the phrase suggester from post_content.shingle to ep_did_you_mean so titles provide suggestion candidates.
- Updates mapping tests and adds title-based suggestion tests.

Fixes 10up#3554
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.

BUG: "Did You Mean" feature results are different when using self-hosted Elasticsearch compared to when using EP.io

1 participant