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

Expand job listing search #2821

Merged
merged 4 commits into from
May 14, 2024
Merged

Expand job listing search #2821

merged 4 commits into from
May 14, 2024

Conversation

gikaragia
Copy link
Contributor

@gikaragia gikaragia commented May 1, 2024

Fixes #2749

Overview

There are many requests for improving the job search functionality in #1707. The most frequent one being to allow an 'OR' operator when searching. So if for example, a user enters 'software engineer' in search then the listings that contain either 'software' or 'engineer' should be returned. I don't think that we should make this the default behavior as:

  • WP core search uses terms with an AND operator which means that it returns posts that contain both terms.
  • From a user perspective the AND seems more useful as it allows to filter through many results. I would expect when adding more terms to make the search more specific than the other way around.
    If we want to ever allow this, we should specifically support 'OR' and 'AND' operators.

Another, confusing part is that WP core search does partial searches which means that if someone searches for "Java" it will return listings that contain "JavaSript".

Finally, there are also the issues mentioned in #2749. We currently also search the meta fields but not in a consistent way with core. We also don't support negations (-) and phrase searches ("") for meta fields and categories.

This PR solves the issues in #2749 by providing the same functionality for meta and term searches with WP Core (which searches title content and excerpt). This means that for meta and categories:

  • We now split search terms and look for each one individually.
  • Each term has to exist to at least one of the metas, categories, post title, content and excerpt.
  • Users can perform reverse searches with the minus sign e.g. -intern
  • Users can perform phrase searches e.g. "software developer"

Changes Proposed in this Pull Request

  • get_job_listings_keyword_search had been reimplemented to construct the search clause from the beginning. Initially, I tried solutions that appended to the existing search clause, but it wasn't possible to achieve accurate results as the core search clauses belonged to a different combination of ANDs and ORs
  • Now the SQL produced for each search term looks like this:
( ( wp_posts.post_title LIKE '%engineer%' )
                OR ( wp_posts.post_excerpt LIKE '%engineer%' )
                OR ( wp_posts.post_content LIKE '%engineer%' )
                OR wp_posts.id IN (SELECT post_id
                                   FROM   wp_postmeta
                                   WHERE  meta_key IN
                                          ( '_application',
                                            '_company_name'
                                            ,
                                            '_company_tagline',
                                                    '_company_website',
                                            '_company_twitter'
                                          )
                                          AND meta_value LIKE '%engineer%')
                OR wp_posts.id IN (SELECT object_id
                                   FROM   wp_term_relationships AS tr
                                          LEFT JOIN wp_term_taxonomy AS tt
                                                 ON tr.term_taxonomy_id =
                                                    tt.term_taxonomy_id
                                          LEFT JOIN wp_terms AS t
                                                 ON tt.term_id = t.term_id
                                   WHERE  t.name LIKE '%engineer%') )
AND ( ( wp_posts.post_title LIKE '%engin%' ) .....
  • WP core limits the search terms count to 9 so I don't think that there is a risk for the query to become too large.
  • I removed the job_listing_search_conditions hook as it wasn't possible to maintain anymore. Users can always override the whole get_job_listings_keyword_search function with the previous version to revert to the previous functionality.

Testing Instructions

  • Add listings that contain possible search terms in the title, post content, excerpt, job category, and job metas (e.g. company name or tagline)
  • Go to the frontend and perform various searches.
  • Try using minus sign and double quotes, especially for meta fields and job categories.
  • Try using category search and term search from the term plugin together with search terms.
  • Observe that:
    • Each search term is included to at least one of the searched columns.
    • When using exclusion terms, observe that these are applied last.

Release Notes

  • Expand job meta and category searching with core WordPress search functionality.

New or Updated Hooks and Templates

job_listing_search_conditions: Removed as it isn't possible to have it anymore. Users can rollback to previous functionality by overriding get_job_listings_keyword_search in wp-job-manager-functions.php

Next Steps

Regarding further improvements in job listing search in general I think that we should avoid them as I think that the implementation is already complex. Any future steps should instead focus on integrating with well known plugins that expand core WP search.


Plugin build for 07706c2
📦 Download plugin zip
▶️ Open in playground

@gikaragia gikaragia self-assigned this May 1, 2024
@gikaragia gikaragia requested review from a team and tripflex May 1, 2024 14:26
This expands job listing meta and term search to have similar
functionality to WP core's search. As the core's search includes
clauses that are aggregated with ORs and ANDs, the only way to
include meta fields in this search was to rebuild the search query
from scratch.

With this change, get_job_listings_keyword_search generates the
clauses that are generated in WP_Query::parse_search and adds
additional clauses that search the post meta and terms.
Copy link
Contributor

@yscik yscik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working great! Added a few questions and noticed one issue, but generally code looks good too.

wp-job-manager-functions.php Show resolved Hide resolved
wp-job-manager-functions.php Outdated Show resolved Hide resolved
wp-job-manager-functions.php Show resolved Hide resolved
wp-job-manager-functions.php Show resolved Hide resolved
wp-job-manager-functions.php Show resolved Hide resolved
$conditions = apply_filters( 'job_listing_search_conditions', $conditions, $job_manager_keyword );
if ( empty( $conditions ) ) {
return $search;
$default_search_columns = [ 'post_title', 'post_excerpt', 'post_content' ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more big improvement to the search would be to prioritize matches in the title — and generally to sort results by relevance. Maybe we could do multiple searches, first just in the post title, then in content, metas? It could actually be faster for the first pages in some cases, since if there are enough results in the title, we don't need to do the content/meta searches.

Just bringing it up to discuss here, it'd be its own PR probably as there would be some extra complexity, like handling pagination. Could also do some weighting maybe?

@gikaragia
Copy link
Contributor Author

In this PR I focused on fixing what seemed like buggy behavior. Although we included some meta fields in searches, it didn't work as expected previously and returned weird results.

One more big improvement to the search would be to prioritize matches in the title — and generally to sort results by relevance. Maybe we could do multiple searches, first just in the post title, then in content, metas? It could actually be faster for the first pages in some cases, since if there are enough results in the title, we don't need to do the content/meta searches.
Just bringing it up to discuss here, it'd be its own PR probably as there would be some extra complexity, like handling pagination. Could also do some weighting maybe?

These are great suggestions, there is a lot of room for improvement. We could look into implementing these in the future. Another option would be to integrate with plugins that expand WP's search capabilities as it has been suggested in the past.

@gikaragia gikaragia merged commit fa43c76 into trunk May 14, 2024
11 checks passed
@gikaragia gikaragia deleted the fix/meta-search branch May 14, 2024 14:26
@yscik yscik added this to the 2.3.1 milestone May 22, 2024
@yscik yscik modified the milestones: 2.3.1, 2.4.0 Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keyword Search: Meta queries don't match WP_Query search & Support Modifiers for AND/OR/EXACT
2 participants