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

Add filter to remove search weighting engine #2522

Merged
merged 8 commits into from
Jan 7, 2022
Merged

Add filter to remove search weighting engine #2522

merged 8 commits into from
Jan 7, 2022

Conversation

rebeccahum
Copy link
Contributor

Description of the Change

Add a filter, so there's an option to turn off the weighting engine.

Alternate Designs

#2512, but there are too many moving parts.

Benefits

The user will be able to simplify the below query (i.e. using the example searching for "hello"):

{
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must": [
                        {
                          "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,
                            "operator": "AND"
                          }
                        },
                        {
                          "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": {
                      "must": [
                        {
                          "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,
                            "operator": "AND"
                          }
                        },
                        {
                          "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
}

Compared to the simplified query:

{
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "hello",
                "type": "phrase",
                "fields": [
                  "post_title",
                  "post_excerpt",
                  "post_content"
                ],
                "boost": 3,
                "operator": "AND"
              }
            },
            {
              "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
}

Possible Drawbacks

None that I can think of.

Verification Process

Use add_filter( 'ep_load_search_weighting', '__return_false' ); and observe the change in ES query.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Added: Filter ep_load_search_weighting to disable search weighting engine.

@felipeelia
Copy link
Member

Hey @rebeccahum, thanks for the PR! Do you mind moving the filter to the Weighting->setup() method (early returning if true) and also adding the doc block? This will be introduced in 4.0.0 (for the @since tag.) Thanks!

@rebeccahum
Copy link
Contributor Author

@felipeelia Done!

@felipeelia felipeelia changed the base branch from develop to 4.x.x January 4, 2022 19:01
@felipeelia
Copy link
Member

Thanks @rebeccahum! As we need it merged to 4.x.x rather than develop I'll work on that conflict and merge it asap.

@felipeelia felipeelia merged commit eeb8d5e into 10up:4.x.x Jan 7, 2022
@rebeccahum rebeccahum deleted the add/filter_to_remove_weighting branch January 7, 2022 16:34
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.

3 participants