Skip to content

Commit

Permalink
Merge pull request #3610 from 10up/feature/issue-2877
Browse files Browse the repository at this point in the history
Create a default_search analyzer
  • Loading branch information
felipeelia authored Sep 21, 2023
2 parents 49033b7 + a93eaa4 commit efe8fde
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 58 deletions.
12 changes: 6 additions & 6 deletions includes/classes/Feature/Search/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ public function add_search_synonyms( $mapping, $index ) {
}

// Ensure we have analyzers and that it is an array.
if ( ! isset( $mapping['settings']['analysis']['analyzer']['default']['filter'] )
|| ! is_array( $mapping['settings']['analysis']['analyzer']['default']['filter'] )
if ( ! isset( $mapping['settings']['analysis']['analyzer']['default_search']['filter'] )
|| ! is_array( $mapping['settings']['analysis']['analyzer']['default_search']['filter'] )
) {
return $mapping;
}
Expand All @@ -389,10 +389,10 @@ public function add_search_synonyms( $mapping, $index ) {
$mapping['settings']['analysis']['filter'][ $filter_name ] = $this->get_synonym_filter();

// Tell the analyzer to use our newly created filter.
$mapping['settings']['analysis']['analyzer']['default']['filter'] = array_values(
$mapping['settings']['analysis']['analyzer']['default_search']['filter'] = array_values(
array_merge(
[ $filter_name ],
$mapping['settings']['analysis']['analyzer']['default']['filter']
$mapping['settings']['analysis']['analyzer']['default_search']['filter']
)
);

Expand Down Expand Up @@ -464,7 +464,7 @@ public function update_synonyms() {
function( $success, $index ) {
$filter = $this->get_synonym_filter();
$mapping = Elasticsearch::factory()->get_mapping( $index );
$filters = $mapping[ $index ]['settings']['index']['analysis']['analyzer']['default']['filter'];
$filters = $mapping[ $index ]['settings']['index']['analysis']['analyzer']['default_search']['filter'];

/*
* Due to limitations in Elasticsearch, we can't remove the filter and analyzer
Expand All @@ -479,7 +479,7 @@ function( $success, $index ) {
$setting['index']['analysis']['filter']['ep_synonyms_filter'] = $filter;

// Add the analyzer.
$setting['index']['analysis']['analyzer']['default']['filter'] = array_values(
$setting['index']['analysis']['analyzer']['default_search']['filter'] = array_values(
array_unique(
array_merge(
[ $this->get_synonym_filter_name() ],
Expand Down
27 changes: 11 additions & 16 deletions includes/mappings/post/5-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,22 @@
'analyzer' => array(
'default' => array(
'tokenizer' => 'standard',
/**
* Filter Elasticsearch default analyzer's filters
*
* @since 3.6.2
* @hook ep_default_analyzer_filters
* @param {array<string>} $filters Default filters
* @return {array<string>} New filters
*/
/* This filter is documented in includes/mappings/post/7-0.php */
'filter' => apply_filters( 'ep_default_analyzer_filters', array( 'standard', 'ewp_word_delimiter', 'lowercase', 'ep_stop', 'ewp_snowball' ) ),
/**
* Filter Elasticsearch default analyzer's char_filter
*
* @since 4.2.2
* @hook ep_default_analyzer_char_filters
* @param {array<string>} $char_filters Default filter
* @return {array<string>} New filters
*/
/* This filter is documented in includes/mappings/post/7-0.php */
'char_filter' => apply_filters( 'ep_default_analyzer_char_filters', array( 'html_strip' ) ),
/* This filter is documented in includes/mappings/post/7-0.php */
'language' => apply_filters( 'ep_analyzer_language', 'english', 'analyzer_default' ),
),
'default_search' => array(
'tokenizer' => 'standard',
/* This filter is documented in includes/mappings/post/7-0.php */
'filter' => apply_filters( 'ep_default_search_analyzer_filters', array( 'standard', 'lowercase', 'ep_stop', 'ewp_snowball' ) ),
/* This filter is documented in includes/mappings/post/7-0.php */
'char_filter' => apply_filters( 'ep_default_search_analyzer_char_filters', array( 'html_strip' ) ),
/* This filter is documented in includes/mappings/post/7-0.php */
'language' => apply_filters( 'ep_analyzer_language', 'english', 'analyzer_default' ),
),
'shingle_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
Expand Down
37 changes: 28 additions & 9 deletions includes/mappings/post/7-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* @param {array<string>} $filters Default filters
* @return {array<string>} New filters
*/
'filter' => apply_filters( 'ep_default_analyzer_filters', array( 'ewp_word_delimiter', 'lowercase', 'ep_stop', 'ewp_snowball' ) ),
'filter' => apply_filters( 'ep_default_analyzer_filters', array( 'lowercase', 'ep_stop', 'ewp_snowball' ) ),
/**
* Filter Elasticsearch default analyzer's char_filter
*
Expand All @@ -92,6 +92,29 @@
*/
'language' => apply_filters( 'ep_analyzer_language', 'english', 'analyzer_default' ),
),
'default_search' => array(
'tokenizer' => 'standard',
/**
* Filter Elasticsearch default analyzer's filters
*
* @since 5.0.0
* @hook ep_default_search_analyzer_filters
* @param {array<string>} $filters Default filters
* @return {array<string>} New filters
*/
'filter' => apply_filters( 'ep_default_search_analyzer_filters', array( 'lowercase', 'ep_stop', 'ewp_snowball' ) ),
/**
* Filter Elasticsearch default analyzer's char_filter
*
* @since 5.0.0
* @hook ep_default_search_analyzer_char_filters
* @param {array<string>} $char_filters Default filter
* @return {array<string>} New filters
*/
'char_filter' => apply_filters( 'ep_default_search_analyzer_char_filters', array( 'html_strip' ) ),
/* This filter is documented above */
'language' => apply_filters( 'ep_analyzer_language', 'english', 'analyzer_default' ),
),
'shingle_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
Expand All @@ -104,27 +127,23 @@
),
),
'filter' => array(
'shingle_filter' => array(
'shingle_filter' => array(
'type' => 'shingle',
'min_shingle_size' => 2,
'max_shingle_size' => 5,
),
'ewp_word_delimiter' => array(
'type' => 'word_delimiter_graph',
'preserve_original' => true,
),
'ewp_snowball' => array(
'ewp_snowball' => array(
'type' => 'snowball',
/* This filter is documented in includes/mappings/post/7-0.php */
'language' => apply_filters( 'ep_analyzer_language', 'english', 'filter_ewp_snowball' ),
),
'edge_ngram' => array(
'edge_ngram' => array(
'side' => 'front',
'max_gram' => 10,
'min_gram' => 3,
'type' => 'edge_ngram',
),
'ep_stop' => [
'ep_stop' => [
'type' => 'stop',
'ignore_case' => true,
/* This filter is documented in includes/mappings/post/7-0.php */
Expand Down
4 changes: 2 additions & 2 deletions tests/cypress/integration/features/instant-results.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
/**
* Perform a search.
*/
cy.intercept('*search=wordpre*').as('apiRequest');
cy.intercept('*search=wordpless*').as('apiRequest');
cy.visit('/');
cy.get('.wp-block-search').last().as('searchBlock');
cy.get('@searchBlock').find('input[type="search"]').type('wordpre');
cy.get('@searchBlock').find('input[type="search"]').type('wordpless');
cy.get('@searchBlock').find('button').click();
cy.get('.ep-search-modal').should('be.visible');
cy.wait('@apiRequest');
Expand Down
47 changes: 28 additions & 19 deletions tests/cypress/integration/features/woocommerce.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,35 @@ describe('WooCommerce Feature', { tags: '@slow' }, () => {
cy.activatePlugin('woocommerce', 'wpCli');
cy.maybeEnableFeature('woocommerce');

cy.updateWeighting({
product: {
'meta._variations_skus.value': {
weight: 1,
enabled: true,
},
},
cy.updateFeatures('search', {
active: 1,
highlight_enabled: true,
highlight_excerpt: true,
highlight_tag: 'mark',
highlight_color: '#157d84',
decaying_enabled: 'disabled_includes_products',
}).then(() => {
cy.wpCli('elasticpress sync --setup --yes').then(() => {
/**
* Give Elasticsearch some time. Apparently, if the visit happens right after the index, it won't find anything.
*
*/
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(2000);
cy.visit('/?s=awesome-aluminum-shoes-variation-sku');
cy.contains(
'.site-content article:nth-of-type(1) h2',
'Awesome Aluminum Shoes',
).should('exist');
cy.updateWeighting({
product: {
'meta._variations_skus.value': {
weight: 1,
enabled: true,
},
},
}).then(() => {
cy.wpCli('elasticpress sync --setup --yes').then(() => {
/**
* Give Elasticsearch some time. Apparently, if the visit happens right after the index, it won't find anything.
*
*/
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(2000);
cy.visit('/?s=awesome-aluminum-shoes-variation-sku');
cy.contains(
'.site-content article:nth-of-type(1) h2',
'Awesome Aluminum Shoes',
).should('exist');
});
});
});
});
Expand Down
13 changes: 8 additions & 5 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,16 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => {
cy.wait(2000);
});

Cypress.Commands.add('updateFeatures', (newFeaturesValues = {}) => {
const features = Object.assign({}, cy.elasticPress.defaultFeatures, ...newFeaturesValues);

const escapedFeatures = JSON.stringify(features);
Cypress.Commands.add('updateFeatures', (featureName, newValues) => {
const escapedNewValues = JSON.stringify(newValues);

cy.wpCliEval(
`$features = json_decode( '${escapedFeatures}', true ); update_option( 'ep_feature_settings', $features );`,
`
$feature_settings = get_option( 'ep_feature_settings', [] );
$feature_settings['${featureName}'] = json_decode( '${escapedNewValues}', true );
update_option( 'ep_feature_settings', $feature_settings );
`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4834,7 +4834,7 @@
sneakers, tennis shoes, trainers, runners

# Defined alternatives (explicit mappings).
shoes => sneaker, sandal, boots, high heels]]></content:encoded>
shoes => shoes, sneaker, sandal, boots, high heels]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>2002</wp:post_id>
<wp:post_date><![CDATA[2021-02-17 15:48:58]]></wp:post_date>
Expand Down
35 changes: 35 additions & 0 deletions tests/php/features/TestSynonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,39 @@ public function testValidateSynonyms() {
$this->assertEquals( 'foo, bar', $instance->validate_synonym( ' foo, bar ' ) );
$this->assertEquals( 'foo => bar', $instance->validate_synonym( ' foo => bar ' ) );
}

/**
* Test synonyms with spaces
*
* @since 5.0.0
* @group synonyms
* @group skip-on-single-site
*/
public function test_synonyms_with_spaces() {
$instance = $this->getFeature();

wp_insert_post(
[
'ID' => $instance->get_synonym_post_id(),
'post_content' => 'internet of things, IoT',
'post_type' => $instance::POST_TYPE_NAME,
],
true
);
$instance->update_synonyms();

$post_id = $this->ep_factory->post->create( [ 'post_content' => 'IoT' ] );

ElasticPress\Elasticsearch::factory()->refresh_indices();

$query = new \WP_Query(
[
's' => 'internet of things',
'fields' => 'ids',
]
);

$this->assertTrue( $query->elasticsearch_success );
$this->assertSame( $post_id, $query->posts['0'] );
}
}

0 comments on commit efe8fde

Please sign in to comment.