htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.39k stars 244 forks source link

Association search not functioning as expected for large # of entries with body text #957

Closed tonyketcham closed 1 year ago

tonyketcham commented 3 years ago

Version

Expected Behavior

Searching for a custom post type returns entries with keyword matches in the title.

Working example with Video Block

Although our video custom post type often has body text in the entires, (much less content than the custom post type we're experiencing the issue with) the search works just fine Screen Recording 2020-12-18 at 3 46 46 PM

Actual Behavior

Using the association field to embed a standard custom post type (articles) inside other articles (of which there are 30-60k+ per site) causes the search feature to return unrelated/glitchy results 9 times out of 10. It will often loop forever between increasing the returned results to decreasing them, with none of the returned results being relevant to the search query. My guess is that the search field is perhaps trying to compare keywords to the body content of every article in the 30k+ entries, rather than just the titles? I could be wrong in that assumption.

The search lags very intensely for articles, where the bulk of the time it returns segmented iterations of results, none of which have any relation to the query.

Undesired current article block search

Screen Recording 2020-12-18 at 3 36 08 PM

However, while still quite slow for the articles...

This specific query was found to return some expected results image As does the video block image

Container definition

Video Block

Block::make(__('Video Embed'))
  ->add_fields(array(
    Field::make('html', 'video_information_text')
      ->set_html('<h2>Video Embed</h2><p><span style="color: red">Please note:</span> The video has to be published to the sites that this article is published to for the video to show up in the article</p>'),
    Field::make('association', 'gnl_video_embed', __('Video'))
      ->set_types(array(
        array(
          'type'      => 'post',
          'post_type' => 'gnl_videos',
        )
      ))
      ->set_min(1)
      ->set_max(1)

  ))
  ->set_category('gnl_blocks', __('Blocks by <Company Name>'), '')
  ->set_icon('video-alt2')
  ->set_preview_mode(true)
  ->set_render_callback(function ($video_block) { ... });
...

Article Block

Block::make(__('Article Embed'))
    ->add_fields(array(
        Field::make('html', 'article_information_text')
            ->set_html('<h2>Article Embed</h2><p><span style="color: red">Please note:</span> The embedded article has to be published to the sites that this post is published to for the embedded article to show up in this post</p>'),
        Field::make('association', 'gnl_article_embed', __('Article'))
            ->set_types(array(
                array(
                    'type'          => 'post',
                    'post_type'     => 'post',
                ),
            ))
            ->set_min(1)
            ->set_max(1)

    ))
    ->set_category('gnl_blocks', __('Blocks by <Company Name>'), '')
    ->set_icon('media-document')
    ->set_preview_mode(true)
    ->set_render_callback(function ($article_block) { ... });
...

Steps to Reproduce the Problem

  1. Have a custom post type like a blog post/article
  2. Have 30k+ entries
  3. Use the above config for defining the block's association field

Comments

I'm looking for a way to have the search feature strictly compare to the post taxonomy (our articles) title content, and return relevant results. Could I perhaps specify something in the association query to compare the search query to ONLY the post title?

elvishp2006 commented 3 years ago

+1 I'm facing the same problem, I saw that each time that I press a key, It 's calling a ajax, I thing that a debounce can solve the problem

tonyketcham commented 3 years ago

I think having a config option on the association type, which constrains the search to only considering the specific field you declare (such as the post title) combined with debouncing could reduce or solve the problem

Cariloha commented 1 year ago

Hi, did you find anything for this issue? I'm facing the same thing, it only loads the first 20 items and then if I keep scrolling to see the rest of the items list, it crashes.

HTMLBurger-NG commented 1 year ago

Hi @tonyketcham ,

Currently, we're using the s parameter in the WP_Query object, which would trigger the WP core search functionality.

If you'd like to override this you could try using our filter and override the arguments for the WP Query and then you could add a filter for the WP Query. The filter is using the field name and field context and looks like this: carbon_fields_association_field_options_{field_name}_{context_type}_{post_type}. In most cases the {context_type} would be simply post, and the {post_type} could be any post type that you would use the block for.

With this filter, you could also override the search field and you could search inside post_content instead, or even in the post meta fields.

Here is an example of usage with the block field that you've used:

add_filter( 'carbon_fields_association_field_options_gnl_article_embed_post_post', function( $args ) {
    if ( ! empty( $args['s'] ) ) {
        $search_term = $args['s'];
        unset( $args['s'] );

        $args['search_by_title'] = $search_term;

        add_filter( 'posts_where', 'gnl_posts_where', 10, 2 );
    }

    return $args;
} );

function gnl_posts_where( $where, $wp_query ) {
    global $wpdb;

    $search_by_title = $wp_query->get( 'search_by_title' );
    if ( ! empty( $search_by_title ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $search_by_title ) ) . '%\'';
    }

    return $where;
}

More information is available in our documentation in Advanced > PHP Hooks > Association Field section. https://docs.carbonfields.net/learn/advanced-topics/php-hooks.html#association-field

HTMLBurger-NG commented 1 year ago

Hi @Cariloha,

Currently, we're unable to reproduce your issue.

Please open a new issue and provide us with more information regarding your WP version, PHP version, Carbon Fields version, the used container, and fields.

Also, you could first try upgrading to our latest release and then re-check if the issue still exists.