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

{total_pages} only reports count of search hits for last set of custom fields #141

Closed
natzmaton opened this Issue Mar 1, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@natzmaton
Copy link

natzmaton commented Mar 1, 2019

After moving from EE2 to EE5, our search-result page now reads "n results for keyword" despite many more than n results appearing below.

I see that Search->getAllQueryParts() calls Search->build_standard_query() once, and then once again for each item in array Search->custom_fields (i.e., if there're 4 items in Search->custom_fields, Search->build_standard_query() gets called 5 times).

However, because Search->build_standard_query() overwrites Search->num_rows every time it gets called (line 1069 of mod.search.php in EE 5.1.3) …

$this->num_rows = $query->num_rows();

… $this->num_rows only reflects the count from the final call of Search->build_standard_query(), and therefore Search->do_search() only writes this lesser number to exp_search.total_results.

In turn, EE reads this this wrong count from exp_search.total_results and displays it atop the search-result page.

I have tested and confirmed that using a combined (+=) operator instead fixes this bug; I therefore propose changing line 1069 of mod.search.php to:

$this->num_rows += $query->num_rows();

@robinsowell robinsowell self-assigned this Mar 1, 2019

@natzmaton

This comment has been minimized.

Copy link
Author

natzmaton commented Mar 4, 2019

Upon review, my solution wasn't quite right, but this was:

diff --git a/system/ee/EllisLab/Addons/search/mod.search.php b/system/ee/EllisLab/Addons/search/mod.search.php
index 214be1df..68b2d978 100644
--- a/system/ee/EllisLab/Addons/search/mod.search.php
+++ b/system/ee/EllisLab/Addons/search/mod.search.php
@@ -29,6 +29,7 @@ class Search {
 
 	protected $_meta 	= array();
 	protected $custom_fields = [];
+	protected $hits = array();
 
 	/**
 	 * Do Search
@@ -1077,11 +1079,14 @@ class Search {
 		foreach ($query->result_array() as $row)
 		{
 			$return['entries'][] = $row['entry_id'];
+			$this->hits[] = $row['entry_id'];
 			$return['channel_ids'][] = $row['channel_id'];
 		}
 
 		$return['channel_ids'] = array_unique($return['channel_ids']);
 
+		$this->hits = array_unique($this->hits);
+
 		/** ----------------------------------------------
 		/**  Set sort order
 		/** ----------------------------------------------*/
@@ -1146,6 +1151,8 @@ class Search {
 			}
 		}
 
+		$this->num_rows = count($this->hits);
+
 		return $query_parts;
 	}
 

@robinsowell

This comment has been minimized.

Copy link
Contributor

robinsowell commented Mar 4, 2019

Thanks! I had confirmed the bug, but knew the first fix wasn't quite getting them all. I'll test this one out tomorrow!

@robinsowell

This comment has been minimized.

Copy link
Contributor

robinsowell commented Mar 6, 2019

OK, give this commit a try: 80fc70e

That get everything squared away for you?

@natzmaton

This comment has been minimized.

Copy link
Author

natzmaton commented Mar 7, 2019

Yes, it does! Thank you very much!!

@robinsowell

This comment has been minimized.

Copy link
Contributor

robinsowell commented Mar 7, 2019

Excellent, I'll put in a pull request. Thanks for the confirmation and the excellent debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.