10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.24k stars 312 forks source link

Getting inconsistent results when searching meta #3961

Closed MediaMaquina closed 1 week ago

MediaMaquina commented 1 month ago

Describe your question

Version 5.1.3

if (($query->is_search && !is_admin()) && $query->is_main_query()) {
            $query->set('ep_integrate', true);
            $query->set('search_fields', array(
            'post_title',
            'post_content',
            'meta' => array( 'game_id')
        ));
        }

Hello! I found that for some posts it finds them searching for that meta but others not, I used PHPMyadmin and confirmed the meta exists and is equal to what I'm searching. I re-indexed but it didn't change anything. How is this possible?

Code of Conduct

felipeelia commented 1 month ago

@MediaMaquina did you add that meta key for all post types in the "Search Fields & Weighting Dashboard"?

MediaMaquina commented 1 month ago

Worth mentioning it's an ACF meta. The only way I can get it to appear down there is doing this:

add_filter( 'ep_prepare_meta_allowed_protected_keys', function( $protected_keys ) {
    $protected_keys[] = 'rmp_vote_count';
    $protected_keys[] = 'rmp_avg_rating';
        $protected_keys[] = 'game_id';
    return $protected_keys;
} );

But then when I click the Searchable option and click save I get: "Something went wrong. Please Try again" and it doesn't save it. So i removed it from there.

I tried a filter mentioned in the docs but it had no effect (I may have done this wrong?) The meta doesn't show up.

function epio_blog_add_custom_field_ep_weighting( $fields, $post_type ) {
    if ( 'page' === $post_type ) {
        if ( empty( $fields['meta'] ) ) {
            $fields['meta'] = array(
                'label'    => 'Custom Fields',
                'children' => array(),
            );
        }

        // Change my_custom_field here to what you need.
        $key = 'meta.game_id.value';

        $fields['meta']['children'][ $key ] = array(
            'key'   => $key,
            'label' => __( 'My Custom Field', 'textdomain' ),
        );
    }

    return $fields;
}
add_filter(
    'ep_weighting_fields_for_post_type',
    'epio_blog_add_custom_field_ep_weighting',
    10,
    2
);

Also when I use the "Search Fields & Weighting Dashboard" "ADD FIELD" input and insert game_id it adds it, but when I refresh that page it disappears, no errors or anything.

I also have these, may be totally unrelated:

add_filter(
    'ep_prepare_meta_data',
    function( $all_meta ) {
        // Change this array to match all meta keys you want to index.
        $allowed_meta = array( 'game_name', 'game_id');
        $meta         = [];

        foreach ( $allowed_meta as $meta_key ) {
            if ( ! isset( $all_meta[ $meta_key ] ) ) {
                continue;
            }
            $meta[ $meta_key ] = $all_meta[ $meta_key ];
        }

        return $meta;
    }
)

 function set_to_exact( $formatted_args, $args ) {
    if ( ! empty( $formatted_args['query']['bool']['should'] ) ) {
        $formatted_args['query']['bool']['must'] = $formatted_args['query']['bool']['should'];
        $formatted_args['query']['bool']['must'][0]['multi_match']['operator'] = 'AND';
        unset( $formatted_args['query']['bool']['should'] );
        unset( $formatted_args["query"]["bool"]["must"][0]["multi_match"]["type"] );
    }
    return $formatted_args;
}
add_filter( 'ep_formatted_args', 'set_to_exact', 10, 2 );
felipeelia commented 3 weeks ago

@MediaMaquina when you get that Something went wrong. Please Try again, do you see any errors in your browser's Developer Tools' Console? Usually that is due to some JS error or conflict.

In ElasticPress 5+ you should manage all your meta fields in the "Search Fields & Weighting Dashboard", so I recommend you focus on fixing that page and removing any reference to ep_prepare_meta_allowed_protected_keys, ep_weighting_fields_for_post_type, and ep_prepare_meta_data.

github-actions[bot] commented 2 weeks ago

It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.

github-actions[bot] commented 1 week ago

This issue has been automatically closed because there has been no response to our request for more information in the past 3 days. With only the information that is currently available, we are unable to take further action on this ticket. Please reach out if you have found or find the answer we need so that we can investigate further. When the information is ready, you can re-open this ticket to share it with us.