-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ajax requests do not use meta fields #2114
Comments
Hi @moritzlang , It seems you have some other custom code affecting the query sent to Elasticsearch, is that right? I can see terms like The problem you're facing now seems to be related to the |
Hi @felipeelia, thank you for your help. Unfortunately I could not find any plugin nor code from the project which is manipulating the search_fields. To run the project only the following plugins are used:
This is the whole elasticpress related code of the project, does this look fine to you? <?php
/**
* Indexes only selected meta fields
*/
add_filter("ep_prepare_meta_data", function($meta, $post) {
$meta = [];
$meta["header"] = Post::ep_header($post->ID); // Returns a string
$meta["headlines"] = Post::ep_headlines($post->ID); // Returns a string
$meta["texts"] = Post::ep_texts($post->ID); // Returns a string
return $meta;
}, 10, 2);
/**
* Display weighting fields in dashboard
*/
add_filter("ep_weighting_fields_for_post_type", function($fields, $post_type) {
// Unset some fields
unset($fields["attributes"]["children"]["post_content"]);
unset($fields["attributes"]["children"]["post_excerpt"]);
// Set the meta fields
if (empty($fields["meta"])) {
$fields["meta"] = array(
"label" => esc_html(__("Custom fields", "mi_base_theme")),
"children" => array(),
);
}
$fields["meta"]["children"]["meta.header.value"] = array(
"key" => "meta.header.value",
"label" => esc_html(__("Header", "mi_base_theme")),
);
$fields["meta"]["children"]["meta.headlines.value"] = array(
"key" => "meta.headlines.value",
"label" => esc_html(__("Headlines", "mi_base_theme")),
);
$fields["meta"]["children"]["meta.texts.value"] = array(
"key" => "meta.texts.value",
"label" => esc_html(__("Texts", "mi_base_theme")),
);
return $fields;
}, 10, 2);
/**
* Use previous search algorithm for fuzziness
*/
add_filter("ep_search_algorithm_version", function() {
return "3.4";
});
/**
* Define post types available to autosuggest
*/
add_filter("ep_term_suggest_post_type", function($post_types) {
$excludes_post_types = array("download", "teaser", "person");
$post_types = array_diff($post_types, $excludes_post_types);
return $post_types;
});
/**
* Integrate with admin ajax queries
*/
add_filter("ep_ajax_wp_query_integration", function($integrate) {
return true;
}, 10);
/**
* Remove unnecessary stylesheets
*/
add_action("wp_enqueue_scripts", function() {
wp_deregister_style("elasticpress-autosuggest");
wp_deregister_style("elasticpress-related-posts-block");
}, 100); Otherwise the problem is most likely related to how the ajax call is made. |
Hi @moritzlang , The code looks good. Investigating things a bit deeper, there is a code that is probably preventing the Weighting feature to change the query. If you look here: https://github.com/10up/ElasticPress/blob/develop/includes/classes/Feature/Search/Weighting.php#L567 the Thanks! |
Hi @felipeelia, this partly fixed it for me! I get results, but the queries still differ when using the search algorithm version 3.4. I recognized that in the AJAX request the I guess the search algorithm version 3.4 is somehow not applied in the AJAX request? Thanks for your help! |
Removing the Further improvement would be to use the /**
* Ensure both search and autosuggest use fuziness with type auto
*
* @param integer $fuzziness Fuzziness
* @param array $search_fields Search Fields
* @param array $args Array of ES args
* @return array
*/
public function set_fuzziness( $fuzziness, $search_fields, $args ) {
if ( ( ! is_admin() || apply_filters( 'ep_ajax_wp_query_integration', false ) ) && ! empty( $args['s'] ) ) {
return 'auto';
}
return $fuzziness;
} @felipeelia is this a viable solution? |
It is @moritzlang, but unfortunately, as you already know, that will be overwritten when you upgrade the plugin. I've created #2148 summarizing our findings here, so I'll close this one and we can track it there. It would be great if you could give a look at the new issue and create a PR to address it. What do you think? Thanks!! |
Hello,
I am not able to get correct results through ajax requests. Everything works fine when settings
"ep_integrate" => false
, so the problem has to be related to elasticpress.I realized that the requests through ajax do not use the meta fields + weights e.g.
meta.headlines.value
. See this logs:Elasticsearch log of a query (no ajax):
Elasticsearch log of a query (through ajax):
This is the query I use:
I also set
ep_ajax_wp_query_integration
totrue
.I use:
How can I debug/resolve the issue?
Thank you for your help!
The text was updated successfully, but these errors were encountered: