Skip to content

Commit f320999

Browse files
Merge pull request #134 from marxjohnson/docoffset-310
Backport docoffset fix
2 parents 7651133 + b19cca0 commit f320999

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

classes/document.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ class document extends \core_search\document {
111111
),
112112
);
113113

114+
/**
115+
* @var mixed $config Search plugin configuration.
116+
*/
117+
protected $config;
118+
119+
/** @var bool Is file indexing enabled? */
120+
protected $fileindexing;
114121

115122
/**
116123
* Constructor for document class.

classes/engine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,10 @@ public function execute_query($filters, $accessinfo, $limit = 0) {
830830
$totalhits = $results->hits->total;
831831
}
832832
$docs = array_merge($docs, $this->compile_results($results, $limit));
833-
$docoffest = count($docs);
833+
$docoffest += count($results->hits->hits);
834834
}
835835

836-
} while ((count($docs) < $limit) && ($totalhits > \search_elastic\query::MAX_RESULTS));
836+
} while ((count($docs) < $limit) && ($totalhits > \search_elastic\query::MAX_RESULTS) && ($docoffest < $totalhits));
837837

838838
// TODO: handle negative cases and errors.
839839
return $docs;

classes/enrich/base/base_enrich.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
*/
3434
abstract class base_enrich {
3535

36+
/**
37+
* @var mixed $config Search plugin configuration.
38+
*/
39+
protected $config;
40+
3641
/**
3742
* The constructor for the class, will be overwritten in most cases.
3843
*

tests/engine_test.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
require_once($CFG->dirroot . '/search/engine/elastic/tests/fixtures/mock_search_area.php');
3333
require_once($CFG->dirroot . '/search/engine/elastic/tests/fixtures/testable_engine.php');
3434

35+
use core_mocksearch\search\mock_boost_area;
36+
use core_mocksearch\search\mock_search_area;
3537
use \GuzzleHttp\Handler\MockHandler;
3638
use \GuzzleHttp\HandlerStack;
3739
use \GuzzleHttp\Psr7\Response;
@@ -60,6 +62,21 @@ class engine_test extends \advanced_testcase {
6062
*/
6163
protected $engine = null;
6264

65+
/**
66+
* @var string the Apache Lucene version of the attached Elasticsearch / OpenSearch service.
67+
*/
68+
protected $luceneversion;
69+
70+
/**
71+
* @var mock_search_area
72+
*/
73+
protected $area;
74+
75+
/**
76+
* @var mock_boost_area
77+
*/
78+
protected $areaboost;
79+
6380
public function setUp(): void {
6481
$this->resetAfterTest();
6582
set_config('enableglobalsearch', true);
@@ -1015,6 +1032,8 @@ public function test_search_wildcardstart_enabled() {
10151032
* Test the wildcard search functionality when wildcardend is enabled.
10161033
*/
10171034
public function test_search_wildcardend_enabled() {
1035+
$searchuser = $this->getDataGenerator()->create_user();
1036+
$this->setUser($searchuser);
10181037
set_config('wildcardend', 1, 'search_elastic');
10191038

10201039
// Construct the search object and add it to the engine.
@@ -1088,4 +1107,45 @@ public function test_broken_index() {
10881107
$this->assertFalse($result);
10891108
}
10901109

1110+
/** Test docoffset is incremented correctly for multiple pages of search results. */
1111+
public function test_execute_query_docoffset() {
1112+
$searchuser = $this->getDataGenerator()->create_user();
1113+
$this->setUser($searchuser);
1114+
// Generate 2000 indexed documents.
1115+
for ($i = 1, $j = 2000; $i <= $j; $i++) {
1116+
$rec = (object)['content' => 'test message ' . $i];
1117+
// Deny user access to all but 100 documents, spaced evenly throughout the index.
1118+
// The engine returns 1000 results at a time, so there should be 50 visible in the first set, and 50 in the second.
1119+
if ($i % 20 != 0) {
1120+
$rec->denyuserids = [$searchuser->id];
1121+
}
1122+
$area = $this->area;
1123+
$record = $this->generator->create_record($rec);
1124+
$doc = $area->get_document($record);
1125+
$this->engine->add_document($doc, false, $this->luceneversion);
1126+
}
1127+
// We need to wait for Elastic search to update its index
1128+
// this happens in near realtime, not immediately.
1129+
sleep(1);
1130+
1131+
$querydata = (object) [
1132+
'q' => '*',
1133+
'timestart' => 0,
1134+
'timeend' => 0,
1135+
];
1136+
1137+
// Execute the search.
1138+
$results = $this->search->search($querydata);
1139+
1140+
$this->assertCount(100, $results);
1141+
1142+
// Confirm that each result is unique. This proves that we are not re-querying the same documents multiple times.
1143+
$seenresults = [];
1144+
foreach ($results as $result) {
1145+
$content = $result->get('content');
1146+
$this->assertFalse(in_array($content, $seenresults), "'{$content}' included multiple times in search results.");
1147+
$seenresults[] = $content;
1148+
}
1149+
}
1150+
10911151
}

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
defined('MOODLE_INTERNAL') || die();
2626

27-
$plugin->version = 2023092000;
28-
$plugin->release = '4.1 Build (2023012400)'; // Build same as version.
27+
$plugin->version = 2023092001;
28+
$plugin->release = '4.1 Build (2023092001)'; // Build same as version.
2929
$plugin->requires = 2016052304;
3030
$plugin->component = 'search_elastic';
3131
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)