Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 62 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| LexemeFullTextQueryBuilder | |
0.00% |
0 / 62 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| newFromGlobals | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| buildDegraded | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildEntitySearchQuery | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
6 | |||
| buildSimpleAllFilter | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikibase\Lexeme\Search\Elastic; |
| 4 | |
| 5 | use CirrusSearch\Query\FullTextQueryBuilder; |
| 6 | use CirrusSearch\Search\SearchContext; |
| 7 | use Elastica\Query\BoolQuery; |
| 8 | use Elastica\Query\DisMax; |
| 9 | use Elastica\Query\MatchQuery; |
| 10 | use Elastica\Query\Term; |
| 11 | use MediaWiki\Context\RequestContext; |
| 12 | use MediaWiki\Language\Language; |
| 13 | use Wikibase\DataModel\Entity\EntityIdParser; |
| 14 | use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory; |
| 15 | use Wikibase\Repo\WikibaseRepo; |
| 16 | use Wikibase\Search\Elastic\EntitySearchUtils; |
| 17 | |
| 18 | /** |
| 19 | * Builder for Lexeme fulltext queries |
| 20 | */ |
| 21 | class LexemeFullTextQueryBuilder implements FullTextQueryBuilder { |
| 22 | /** |
| 23 | * Default profile name for lexemes |
| 24 | */ |
| 25 | public const LEXEME_DEFAULT_PROFILE = 'lexeme_fulltext'; |
| 26 | /** |
| 27 | * Lexeme fulltext search context name |
| 28 | */ |
| 29 | public const CONTEXT_LEXEME_FULLTEXT = 'wikibase_lexeme_fulltext'; |
| 30 | public const LEXEME_FULL_TEXT_MARKER = 'lexeme_full_text'; |
| 31 | |
| 32 | /** |
| 33 | * @var array |
| 34 | */ |
| 35 | private $settings; |
| 36 | /** |
| 37 | * @var EntityIdParser |
| 38 | */ |
| 39 | private $entityIdParser; |
| 40 | /** |
| 41 | * @var Language User language code |
| 42 | */ |
| 43 | private $userLanguage; |
| 44 | /** |
| 45 | * @var FallbackLabelDescriptionLookupFactory |
| 46 | */ |
| 47 | private $lookupFactory; |
| 48 | |
| 49 | /** |
| 50 | * @param array $settings Settings from EntitySearchProfiles.php |
| 51 | * @param FallbackLabelDescriptionLookupFactory $lookupFactory |
| 52 | * @param EntityIdParser $entityIdParser |
| 53 | * @param Language $userLanguage User's display language |
| 54 | */ |
| 55 | public function __construct( |
| 56 | array $settings, |
| 57 | FallbackLabelDescriptionLookupFactory $lookupFactory, |
| 58 | EntityIdParser $entityIdParser, |
| 59 | Language $userLanguage |
| 60 | ) { |
| 61 | $this->settings = $settings; |
| 62 | $this->entityIdParser = $entityIdParser; |
| 63 | $this->userLanguage = $userLanguage; |
| 64 | $this->lookupFactory = $lookupFactory; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Create fulltext builder from global environment. |
| 69 | * @param array $settings Configuration from config file |
| 70 | * @return LexemeFullTextQueryBuilder |
| 71 | */ |
| 72 | public static function newFromGlobals( array $settings ) { |
| 73 | return new static( |
| 74 | $settings, |
| 75 | WikibaseRepo::getFallbackLabelDescriptionLookupFactory(), |
| 76 | WikibaseRepo::getEntityIdParser(), |
| 77 | RequestContext::getMain()->getLanguage() |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Search articles with provided term. |
| 83 | * |
| 84 | * @param SearchContext $searchContext |
| 85 | * @param string $term term to search |
| 86 | */ |
| 87 | public function build( SearchContext $searchContext, $term ) { |
| 88 | if ( $searchContext->areResultsPossible() && !$searchContext->isSpecialKeywordUsed() ) { |
| 89 | // We use entity search query if we did not find any advanced syntax |
| 90 | // and the base builder did not reject the query |
| 91 | $this->buildEntitySearchQuery( $searchContext, $term ); |
| 92 | } |
| 93 | // if we did find advanced query, we keep the old setup but change the result type |
| 94 | // FIXME: make it dispatch by content model |
| 95 | $searchContext->setResultsType( new LexemeFulltextResult( $this->entityIdParser, |
| 96 | $this->userLanguage, |
| 97 | $this->lookupFactory ) ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param SearchContext $searchContext |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function buildDegraded( SearchContext $searchContext ) { |
| 105 | // Not doing anything for now |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Build a fulltext query for Wikibase entity. |
| 111 | * @param SearchContext $searchContext |
| 112 | * @param string $term Search term |
| 113 | */ |
| 114 | protected function buildEntitySearchQuery( SearchContext $searchContext, $term ) { |
| 115 | $searchContext->setProfileContext( self::CONTEXT_LEXEME_FULLTEXT ); |
| 116 | $searchContext->addSyntaxUsed( self::LEXEME_FULL_TEXT_MARKER, 10 ); |
| 117 | /* |
| 118 | * Overall query structure is as follows: |
| 119 | * - Bool with: |
| 120 | * Filter of namespace = N |
| 121 | * OR (Should with 1 mininmum) of: |
| 122 | * title.keyword = QUERY |
| 123 | * lexeme_forms.id = QUERY |
| 124 | * fulltext match query |
| 125 | * |
| 126 | * Fulltext match query is: |
| 127 | * Filter of: |
| 128 | * at least one of: all, all.plain matching |
| 129 | * OR (should with 0 minimum) of: |
| 130 | * DISMAX query of: {lemma|form}.near_match |
| 131 | * OR (should with 0 minimum) of: |
| 132 | * all |
| 133 | * all.plain |
| 134 | */ |
| 135 | |
| 136 | $profile = $this->settings; |
| 137 | // $fields is collecting all the fields for dismax query to be used in |
| 138 | // scoring match |
| 139 | $fields = [ |
| 140 | [ "lemma.near_match", $profile['exact'] ], |
| 141 | [ "lemma.near_match_folded", $profile['folded'] ], |
| 142 | [ |
| 143 | "lexeme_forms.representation.near_match", |
| 144 | $profile['exact'] * $profile['form-discount'], |
| 145 | ], |
| 146 | [ |
| 147 | "lexeme_forms.representation.near_match_folded", |
| 148 | $profile['folded'] * $profile['form-discount'], |
| 149 | ], |
| 150 | ]; |
| 151 | |
| 152 | $titleMatch = new Term( [ |
| 153 | 'title.keyword' => EntitySearchUtils::normalizeId( $term, $this->entityIdParser ), |
| 154 | ] ); |
| 155 | // lexeme_forms.id is a lowercase_keyword so use Match to apply the analyzer |
| 156 | $formIdMatch = new MatchQuery( 'lexeme_forms.id', |
| 157 | EntitySearchUtils::normalizeId( $term, $this->entityIdParser ) ); |
| 158 | |
| 159 | // Main query filter |
| 160 | $filterQuery = $this->buildSimpleAllFilter( $term ); |
| 161 | |
| 162 | // Near match ones, they use constant score |
| 163 | $nearMatchQuery = new DisMax(); |
| 164 | $nearMatchQuery->setTieBreaker( $profile['tie-breaker'] ?? 0 ); |
| 165 | foreach ( $fields as $field ) { |
| 166 | $nearMatchQuery->addQuery( EntitySearchUtils::makeConstScoreQuery( $field[0], $field[1], |
| 167 | $term ) ); |
| 168 | } |
| 169 | |
| 170 | // Tokenized ones |
| 171 | $tokenizedQuery = $this->buildSimpleAllFilter( $term, 'OR', $profile['any'] ); |
| 172 | |
| 173 | // Main labels/desc query |
| 174 | $fullTextQuery = new BoolQuery(); |
| 175 | $fullTextQuery->addFilter( $filterQuery ); |
| 176 | $fullTextQuery->addShould( $nearMatchQuery ); |
| 177 | $fullTextQuery->addShould( $tokenizedQuery ); |
| 178 | |
| 179 | // Main query |
| 180 | $query = new BoolQuery(); |
| 181 | |
| 182 | // Match either labels or exact match to title |
| 183 | $query->addShould( $titleMatch ); |
| 184 | $query->addShould( $formIdMatch ); |
| 185 | $query->addShould( $fullTextQuery ); |
| 186 | $query->setMinimumShouldMatch( 1 ); |
| 187 | |
| 188 | $searchContext->setMainQuery( $query ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Builds a simple filter on all and all.plain when all terms must match |
| 193 | * |
| 194 | * @param string $query |
| 195 | * @param string $operator |
| 196 | * @param null $boost |
| 197 | * @return BoolQuery |
| 198 | */ |
| 199 | private function buildSimpleAllFilter( $query, $operator = 'AND', $boost = null ) { |
| 200 | $filter = new BoolQuery(); |
| 201 | // FIXME: We can't use solely the stem field here |
| 202 | // - Depending on languages it may lack stopwords, |
| 203 | // A dedicated field used for filtering would be nice |
| 204 | foreach ( [ 'all', 'all.plain' ] as $field ) { |
| 205 | $m = new MatchQuery(); |
| 206 | $m->setFieldQuery( $field, $query ); |
| 207 | $m->setFieldOperator( $field, $operator ); |
| 208 | if ( $boost ) { |
| 209 | $m->setFieldBoost( $field, $boost ); |
| 210 | } |
| 211 | $filter->addShould( $m ); |
| 212 | } |
| 213 | return $filter; |
| 214 | } |
| 215 | |
| 216 | } |