Skip to content
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

Search: Add filter_ep_enable_do_weighting to reduce ES payload if weighting is not used #3340

Merged
merged 1 commit into from
Jul 6, 2022

Conversation

rebeccahum
Copy link
Contributor

@rebeccahum rebeccahum commented Jul 6, 2022

Description

A default ES search request appears to be under the assumption that weighting is always being used: https://github.com/Automattic/ElasticPress/blob/2302acc659745e767ea0908eeb44a81f9dab755d/includes/classes/Feature/Search/Weighting.php#L644-L647

However, if weighting is not being used, we can reduce the payload by not recursively creating subqueries and thus, decrease the total ES query time. More information on the query at hand described in 10up/ElasticPress#2512.

This PR adds the filter filter_ep_enable_do_weighting, which checks to see if a weighting configuration has been saved or if any of the below filters are hooked onto outside of ElasticPress:

  • ep_weighting_configuration_for_search
  • ep_query_weighting_fields
  • ep_weighting_configuration
  • ep_weighting_fields_for_post_type

Changelog Description

Plugin Updated: Enterprise Search

Search: Add filter_ep_enable_do_weighting to reduce ES payload if weighting is not used

Checklist

Please make sure the items below have been covered before requesting a review:

  • This change works and has been tested locally (or has an appropriate fallback).
  • This change works and has been tested on a Go sandbox.
  • This change has relevant unit tests (if applicable).
  • This change has relevant documentation additions / updates (if applicable).
  • I've created a changelog description that aligns with the provided examples.

Steps to Test

  1. Spin-up a new site and do a front-end search ?s=hello and see the below ES query being executed:
{
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "multi_match": {
                            "query": "hello",
                            "type": "phrase",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.post_tag.name^1",
                              "terms.category.name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "boost": 3
                          }
                        },
                        {
                          "multi_match": {
                            "query": "hello",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.post_tag.name^1",
                              "terms.category.name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "type": "phrase",
                            "slop": 5
                          }
                        }
                      ]
                    }
                  }
                ],
                "filter": [
                  {
                    "match": {
                      "post_type.raw": "post"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "multi_match": {
                            "query": "hello",
                            "type": "phrase",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "boost": 3
                          }
                        },
                        {
                          "multi_match": {
                            "query": "hello",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "type": "phrase",
                            "slop": 5
                          }
                        }
                      ]
                    }
                  }
                ],
                "filter": [
                  {
                    "match": {
                      "post_type.raw": "page"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "post_date_gmt": {
              "scale": "360d",
              "decay": 0.9,
              "offset": "0d"
            }
          }
        },
        {
          "weight": 0.001
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "terms": {
            "post_type.raw": [
              "post",
              "page"
            ]
          }
        },
        {
          "terms": {
            "post_status": [
              "publish"
            ]
          }
        }
      ]
    }
  },
  "track_total_hits": true,
  "timeout": "2s"
}
  1. Apply PR
  2. Do step 1 and see reduced payload:
{
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "query": "hello",
                "type": "phrase",
                "fields": [
                  "post_title",
                  "post_excerpt",
                  "post_content"
                ],
                "boost": 3
              }
            },
            {
              "multi_match": {
                "query": "hello",
                "fields": [
                  "post_title",
                  "post_excerpt",
                  "post_content"
                ],
                "type": "phrase",
                "slop": 5
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "post_date_gmt": {
              "scale": "360d",
              "decay": 0.9,
              "offset": "0d"
            }
          }
        },
        {
          "weight": 0.001
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "terms": {
            "post_type.raw": [
              "post",
              "page"
            ]
          }
        },
        {
          "terms": {
            "post_status": [
              "publish"
            ]
          }
        }
      ]
    }
  },
  "track_total_hits": true,
  "timeout": "2s"
}
  1. Add filter:
add_filter(
	'ep_weighting_configuration_for_search',
	function( $weight_config ) {
		return $weight_config;
	}
);
  1. Repeat step 1 and expect to see the original results

@rebeccahum rebeccahum requested a review from a team as a code owner July 6, 2022 18:31
@rebeccahum rebeccahum force-pushed the search/weighting_request branch 2 times, most recently from 6764257 to f4ab087 Compare July 6, 2022 19:02
@codecov
Copy link

codecov bot commented Jul 6, 2022

Codecov Report

Merging #3340 (f4cc0a7) into develop (0bba658) will increase coverage by 0.12%.
The diff coverage is 94.11%.

❗ Current head f4cc0a7 differs from pull request most recent head 51b34f4. Consider uploading reports for the commit 51b34f4 to get more accurate results

@@              Coverage Diff              @@
##             develop    #3340      +/-   ##
=============================================
+ Coverage      33.00%   33.12%   +0.12%     
- Complexity      3457     3467      +10     
=============================================
  Files            213      213              
  Lines          13603    13620      +17     
=============================================
+ Hits            4489     4511      +22     
+ Misses          9114     9109       -5     
Impacted Files Coverage Δ
search/includes/classes/class-search.php 84.56% <94.11%> (-0.05%) ⬇️
search/includes/classes/class-queue.php 88.83% <0.00%> (+1.98%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0bba658...51b34f4. Read the comment docs.

@rebeccahum rebeccahum force-pushed the search/weighting_request branch 2 times, most recently from 063bc2d to f4cc0a7 Compare July 6, 2022 19:22
rinatkhaziev
rinatkhaziev previously approved these changes Jul 6, 2022
@sonarcloud
Copy link

sonarcloud bot commented Jul 6, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants