10up / ElasticPress

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

Ajax requests do not use meta fields #2114

Closed moritzlang closed 3 years ago

moritzlang commented 3 years ago

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):

{
  "type": "index_search_slowlog",
  "timestamp": "2021-03-03T09:58:36,720Z",
  "level": "WARN",
  "component": "i.s.s.fetch",
  "cluster.name": "docker-cluster",
  "node.name": "d057238bcfd7",
  "message": "[localhost8000-post-1][3]",
  "took": "2.9ms",
  "took_millis": "2",
  "total_hits": "51 hits",
  "stats": "[]",
  "search_type": "QUERY_THEN_FETCH",
  "total_shards": "5",
  "source": "{\"from\":0,\"size\":12,\"query\":{\"function_score\":{\"query\":{\"bool\":{\"should\":[{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"post\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"page\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^23.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^23.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^23.0\",\"post_title.suggest^22.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"freizeit\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"standort\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"job\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"lehrstelle\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"teaser\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"terms.ep_custom_result.name^9999.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"meta.header.value^1.0\",\"meta.headlines.value^1.0\",\"meta.texts.value^1.0\",\"post_title^10.0\",\"post_title.suggest^9.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"AUTO\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"filter\":[{\"match\":{\"post_type.raw\":{\"query\":\"ticket\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"functions\":[{\"filter\":{\"match_all\":{\"boost\":1.0}},\"exp\":{\"post_date_gmt\":{\"scale\":\"14d\",\"decay\":0.25,\"offset\":\"7d\"},\"multi_value_mode\":\"MIN\"}}],\"score_mode\":\"avg\",\"boost_mode\":\"sum\",\"max_boost\":3.4028235E38,\"boost\":1.0}},\"post_filter\":{\"bool\":{\"must\":[{\"terms\":{\"post_type.raw\":[\"post\",\"page\",\"freizeit\",\"standort\",\"job\",\"lehrstelle\",\"teaser\",\"ticket\"],\"boost\":1.0}},{\"term\":{\"post_status\":{\"value\":\"publish\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"sort\":[{\"_score\":{\"order\":\"desc\"}}]}",
  "cluster.uuid": "4mCWq1TQS0ucqU1uMmEZrw",
  "node.id": "vDkmYhIdT7yslv-Yj5l8XQ"
}

Elasticsearch log of a query (through ajax):

{
  "type": "index_search_slowlog",
  "timestamp": "2021-03-03T10:03:14,343Z",
  "level": "WARN",
  "component": "i.s.s.fetch",
  "cluster.name": "docker-cluster",
  "node.name": "d057238bcfd7",
  "message": "[localhost8000-post-1][3]",
  "took": "740.6micros",
  "took_millis": "0",
  "total_hits": "3 hits",
  "stats": "[]",
  "search_type": "QUERY_THEN_FETCH",
  "total_shards": "5",
  "source": "{\"from\":12,\"size\":12,\"query\":{\"function_score\":{\"query\":{\"bool\":{\"should\":[{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"post_content^1.0\",\"post_excerpt^1.0\",\"post_title^1.0\"],\"type\":\"phrase\",\"operator\":\"OR\",\"slop\":0,\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":4.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"post_content^1.0\",\"post_excerpt^1.0\",\"post_title^1.0\"],\"type\":\"best_fields\",\"operator\":\"AND\",\"slop\":0,\"fuzziness\":\"0\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":2.0}},{\"multi_match\":{\"query\":\"aus\",\"fields\":[\"post_content^1.0\",\"post_excerpt^1.0\",\"post_title^1.0\"],\"type\":\"best_fields\",\"operator\":\"OR\",\"slop\":0,\"fuzziness\":\"1\",\"prefix_length\":0,\"max_expansions\":50,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"fuzzy_transpositions\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"functions\":[{\"filter\":{\"match_all\":{\"boost\":1.0}},\"exp\":{\"post_date_gmt\":{\"scale\":\"14d\",\"decay\":0.25,\"offset\":\"7d\"},\"multi_value_mode\":\"MIN\"}}],\"score_mode\":\"avg\",\"boost_mode\":\"sum\",\"max_boost\":3.4028235E38,\"boost\":1.0}},\"post_filter\":{\"bool\":{\"must\":[{\"terms\":{\"post_type.raw\":[\"post\",\"page\",\"freizeit\",\"standort\",\"job\",\"lehrstelle\",\"teaser\",\"ticket\"],\"boost\":1.0}},{\"term\":{\"post_status\":{\"value\":\"publish\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"sort\":[{\"_score\":{\"order\":\"desc\"}}]}",
  "cluster.uuid": "4mCWq1TQS0ucqU1uMmEZrw",
  "node.id": "vDkmYhIdT7yslv-Yj5l8XQ"
}

This is the query I use:

/**
 * @param  int $page
 * @param  string $query
 * @return WP_Query The WP_Query object
 */
public function get_query($page = 1, $query = "")
{
    return new WP_Query(array(
        "ep_integrate" => true,
        "post_type" => array("post", "page", "freizeit", "standort", "job", "lehrstelle", "teaser", "ticket"),
        "post_status" => "publish",
        "posts_per_page" => 12,
        "paged" => $page,
        "s" => $query,
    ));
}

I also set ep_ajax_wp_query_integration to true.

add_filter("ep_ajax_wp_query_integration", function($integrate) {
  return true;
}, 10);

I use:

How can I debug/resolve the issue?

Thank you for your help!

felipeelia commented 3 years ago

Hi @moritzlang ,

It seems you have some other custom code affecting the query sent to Elasticsearch, is that right? I can see terms like prefix_length, max_expansions, and zero_terms_query in the query but none of them are part of the plugin. Can you please share that here too? If you didn't write that code, it can be part of another plugin you are using.

The problem you're facing now seems to be related to the search_fields parameter. If that is set, it'll override the weighting configuration.

moritzlang commented 3 years ago

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.

felipeelia commented 3 years ago

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 is_admin() will be true in AJAX requests and then the query won't be changed. Do you mind removing that part of the if and checking if it works as it should?

Thanks!

moritzlang commented 3 years ago

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 fuzziness value inside the multi_match object is set to 1 instead of AUTO.

I guess the search algorithm version 3.4 is somehow not applied in the AJAX request? Thanks for your help!

moritzlang commented 3 years ago

Removing the is_admin() check here fixed this issue.

Further improvement would be to use the ep_ajax_wp_query_integration for this:

/**
 * 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?

felipeelia commented 3 years ago

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!!