complete testing

This commit is contained in:
Daniël Klabbers 2022-11-10 12:54:26 +01:00
parent 29ba864849
commit 4bc89949ec
7 changed files with 31 additions and 26 deletions

View File

@ -19,5 +19,11 @@ return [
->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class),
(new Flarum\Console)
->command(Commands\BuildCommand::class)
->command(Commands\BuildCommand::class),
(new Flarum\Settings)
->default('blomstra-search.search-discussion-subjects', true)
->default('blomstra-search.search-post-bodies', true)
->default('blomstra-search.match-sentences', true)
->default('blomstra-search.match-words', true)
];

2
js/dist/admin.js vendored
View File

@ -1,2 +1,2 @@
(()=>{var a={n:e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return a.d(t,{a:t}),t},d:(e,t)=>{for(var r in t)a.o(t,r)&&!a.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:(a,e)=>Object.prototype.hasOwnProperty.call(a,e),r:a=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(a,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(a,"__esModule",{value:!0})}},e={};(()=>{"use strict";a.r(e);const t=flarum.core.compat["admin/app"];var r=a.n(t);r().initializers.add("blomstra-search",(function(){var a=new Map;["arabic","armenian","basque","bengali","brazilian","bulgarian","catalan","cjk","czech","danish","dutch","english","estonian","finnish","french","galician","german","greek","hindi","hungarian","indonesian","irish","italian","latvian","lithuanian","norwegian","persian","portuguese","romanian","russian","sorani","spanish","swedish","turkish","thai"].forEach((function(e){a.set(e,e)})),r().extensionData.for("blomstra-search").registerSetting({setting:"blomstra-search.elastic-endpoint",label:r().translator.trans("blomstra-search.admin.elastic-endpoint"),type:"input"}).registerSetting({setting:"blomstra-search.elastic-username",label:r().translator.trans("blomstra-search.admin.elastic-username"),type:"input"}).registerSetting({setting:"blomstra-search.elastic-password",label:r().translator.trans("blomstra-search.admin.elastic-password"),type:"password"}).registerSetting({setting:"blomstra-search.elastic-index",label:r().translator.trans("blomstra-search.admin.elastic-index"),default:"flarum",type:"input"}).registerSetting({setting:"blomstra-search.analyzer-language",label:r().translator.trans("blomstra-search.admin.analyzer.label"),help:r().translator.trans("blomstra-search.admin.analyzer.help"),type:"select",options:Object.fromEntries(a.entries()),default:"english"}).registerSetting({setting:"blomstra-search.elastic-index",label:r().translator.trans("blomstra-search.admin.elastic-index"),default:"flarum",type:"input"})}))})(),module.exports=e})();
(()=>{var a={n:t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return a.d(e,{a:e}),e},d:(t,e)=>{for(var s in e)a.o(e,s)&&!a.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},o:(a,t)=>Object.prototype.hasOwnProperty.call(a,t),r:a=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(a,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(a,"__esModule",{value:!0})}},t={};(()=>{"use strict";a.r(t);const e=flarum.core.compat["admin/app"];var s=a.n(e);s().initializers.add("blomstra-search",(function(){var a=new Map;["arabic","armenian","basque","bengali","brazilian","bulgarian","catalan","cjk","czech","danish","dutch","english","estonian","finnish","french","galician","german","greek","hindi","hungarian","indonesian","irish","italian","latvian","lithuanian","norwegian","persian","portuguese","romanian","russian","sorani","spanish","swedish","turkish","thai"].forEach((function(t){a.set(t,t)})),s().extensionData.for("blomstra-search").registerSetting({setting:"blomstra-search.elastic-endpoint",label:s().translator.trans("blomstra-search.admin.elastic-endpoint"),type:"input"}).registerSetting({setting:"blomstra-search.elastic-username",label:s().translator.trans("blomstra-search.admin.elastic-username"),type:"input"}).registerSetting({setting:"blomstra-search.elastic-password",label:s().translator.trans("blomstra-search.admin.elastic-password"),type:"password"}).registerSetting({setting:"blomstra-search.elastic-index",label:s().translator.trans("blomstra-search.admin.elastic-index"),default:"flarum",type:"input"}).registerSetting({setting:"blomstra-search.analyzer-language",label:s().translator.trans("blomstra-search.admin.analyzer.label"),help:s().translator.trans("blomstra-search.admin.analyzer.help"),type:"select",options:Object.fromEntries(a.entries()),default:"english"}).registerSetting({setting:"blomstra-search.elastic-index",label:s().translator.trans("blomstra-search.admin.elastic-index"),default:"flarum",type:"input"}).registerSetting({setting:"blomstra-search.search-discussion-subjects",label:s().translator.trans("blomstra-search.admin.search-discussion-subjects"),type:"switch"}).registerSetting({setting:"blomstra-search.search-post-bodies",label:s().translator.trans("blomstra-search.admin.search-post-bodies"),type:"switch"}).registerSetting({setting:"blomstra-search.match-sentences",label:s().translator.trans("blomstra-search.admin.match-sentences"),type:"switch"}).registerSetting({setting:"blomstra-search.match-words",label:s().translator.trans("blomstra-search.admin.match-words"),type:"switch"})}))})(),module.exports=t})();
//# sourceMappingURL=admin.js.map

File diff suppressed because one or more lines are too long

View File

@ -98,10 +98,5 @@ app.initializers.add('blomstra-search', () => {
setting: 'blomstra-search.match-words',
label: app.translator.trans('blomstra-search.admin.match-words'),
type: 'switch',
})
.registerSetting({
setting: 'blomstra-search.match-fragments',
label: app.translator.trans('blomstra-search.admin.match-fragments'),
type: 'switch',
});
});

View File

@ -9,3 +9,7 @@ blomstra-search:
help: |
The analyzer makes search understand stop words and undertakes language
specific improvements for indexing.
search-discussion-subjects: Search inside discussion titles
search-post-bodies: Search inside comments
match-sentences: Match search term against full sentence
match-words: Match search term against full words

View File

@ -39,18 +39,24 @@ class SearchController extends ListDiscussionsController
'commentCount' => 'comment_count'
];
protected iterable $searchers;
protected Collection $searchers;
protected bool $matchSentences;
protected bool $matchWords;
protected bool $matchFragments;
public function __construct(protected Client $elastic, protected UrlGenerator $uri, Container $container, SettingsRepositoryInterface $settings)
{
$this->searchers = $container->tagged('blomstra.search.searchers');
$this->searchers = $this->gatherSearchers($container->tagged('blomstra.search.searchers'), $container);
$this->matchSentences = (bool) $settings->get('blomstra-search.match-sentences', true);
$this->matchWords = (bool) $settings->get('blomstra-search.match-words', true);
$this->matchFragments = (bool) $settings->get('blomstra-search.match-fragments', true);
}
protected function gatherSearchers(iterable $searchers, Container $container)
{
return collect($searchers)
->map(fn ($searcher) => new $searcher)
->filter(fn (Searcher $searcher) => $searcher->enabled());
}
protected function data(ServerRequestInterface $request, Document $document)
@ -75,7 +81,6 @@ class SearchController extends ListDiscussionsController
if ($this->matchSentences) $filterQuery->add($this->sentenceMatch($search));
if ($this->matchWords) $filterQuery->add($this->wordMatch($search, 'and'));
if ($this->matchWords) $filterQuery->add($this->wordMatch($search, 'or'));
if ($this->matchFragments) $filterQuery->add($this->partialMatch($search));
}
$builder = (new Builder($this->elastic))
@ -231,21 +236,23 @@ class SearchController extends ListDiscussionsController
return $query;
}
protected function boolQuery(Query $parent, float $boost = 1)
protected function boolQuery(Query $parent, float $boost = 1): Query
{
$bool = new BoolQuery;
/** @var Searcher $searcher */
foreach ($this->searchers as $searcher) {
$searcher = new $searcher;
$parent->add(
$bool->add(
BoolQuery::create()
->add(TermQuery::create('type', $searcher->type()), 'filter')
->add($parent->boost($boost * $searcher->boost())),
->add(clone $parent->boost($boost * $searcher->boost())),
'should'
);
}
return $parent;
return $bool;
}
protected function sentenceMatch(string $q): Query
@ -255,7 +262,7 @@ class SearchController extends ListDiscussionsController
return $this->boolQuery($query, 2);
}
protected function wordMatch(string $q, string $operator = 'or')
protected function wordMatch(string $q, string $operator = 'or'): Query
{
$query = (new MatchQuery('content', $q))
->operator($operator);
@ -265,13 +272,6 @@ class SearchController extends ListDiscussionsController
return $this->boolQuery($query, $boost);
}
protected function partialMatch(string $q)
{
$query = (new MatchQuery('content', $q));
return $this->boolQuery($query, .6);
}
protected function getGroups(User $actor): Collection
{

View File

@ -10,7 +10,7 @@ class CommentPostSearcher extends Searcher
public function enabled(): bool
{
$enabled = $this->setting('blomstra-search.admin.search-post-bodies', true);
$enabled = $this->setting('blomstra-search.search-post-bodies', true);
return boolval($enabled);
}