Differential D18502 Diff 44447 src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php
| Show First 20 Lines • Show All 1,404 Lines • ▼ Show 20 Lines | /* -( Ferret )------------------------------------------------------------- */ | ||||
| } | } | ||||
| protected function buildFerretJoinClause(AphrontDatabaseConnection $conn) { | protected function buildFerretJoinClause(AphrontDatabaseConnection $conn) { | ||||
| if (!$this->ferretEngine) { | if (!$this->ferretEngine) { | ||||
| return array(); | return array(); | ||||
| } | } | ||||
| $op_sub = PhutilSearchQueryCompiler::OPERATOR_SUBSTRING; | $op_sub = PhutilSearchQueryCompiler::OPERATOR_SUBSTRING; | ||||
| $op_not = PhutilSearchQueryCompiler::OPERATOR_NOT; | |||||
| $engine = $this->ferretEngine; | $engine = $this->ferretEngine; | ||||
| $ngram_engine = new PhabricatorNgramEngine(); | $ngram_engine = new PhabricatorNgramEngine(); | ||||
| $stemmer = new PhutilSearchStemmer(); | $stemmer = new PhutilSearchStemmer(); | ||||
| $ngram_table = $engine->newNgramsObject(); | $ngram_table = $engine->newNgramsObject(); | ||||
| $ngram_table_name = $ngram_table->getTableName(); | $ngram_table_name = $ngram_table->getTableName(); | ||||
| $flat = array(); | $flat = array(); | ||||
| foreach ($this->ferretTokens as $fulltext_token) { | foreach ($this->ferretTokens as $fulltext_token) { | ||||
| $raw_token = $fulltext_token->getToken(); | $raw_token = $fulltext_token->getToken(); | ||||
| // If this is a negated term like "-pomegranate", don't join the ngram | |||||
| // table since we aren't looking for documents with this term. (We could | |||||
| // LEFT JOIN the table and require a NULL row, but this is probably more | |||||
| // trouble than it's worth.) | |||||
| if ($raw_token->getOperator() == $op_not) { | |||||
| continue; | |||||
| } | |||||
| $value = $raw_token->getValue(); | $value = $raw_token->getValue(); | ||||
| $length = count(phutil_utf8v($value)); | $length = count(phutil_utf8v($value)); | ||||
| if ($raw_token->getOperator() == $op_sub) { | if ($raw_token->getOperator() == $op_sub) { | ||||
| $is_substring = true; | $is_substring = true; | ||||
| } else { | } else { | ||||
| $is_substring = false; | $is_substring = false; | ||||
| ▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | /* -( Ferret )------------------------------------------------------------- */ | ||||
| protected function buildFerretWhereClause(AphrontDatabaseConnection $conn) { | protected function buildFerretWhereClause(AphrontDatabaseConnection $conn) { | ||||
| if (!$this->ferretEngine) { | if (!$this->ferretEngine) { | ||||
| return array(); | return array(); | ||||
| } | } | ||||
| $ngram_engine = new PhabricatorNgramEngine(); | $ngram_engine = new PhabricatorNgramEngine(); | ||||
| $stemmer = new PhutilSearchStemmer(); | $stemmer = new PhutilSearchStemmer(); | ||||
| $op_sub = PhutilSearchQueryCompiler::OPERATOR_SUBSTRING; | $op_sub = PhutilSearchQueryCompiler::OPERATOR_SUBSTRING; | ||||
| $op_not = PhutilSearchQueryCompiler::OPERATOR_NOT; | |||||
| $where = array(); | $where = array(); | ||||
| foreach ($this->ferretTokens as $fulltext_token) { | foreach ($this->ferretTokens as $fulltext_token) { | ||||
| $raw_token = $fulltext_token->getToken(); | $raw_token = $fulltext_token->getToken(); | ||||
| $value = $raw_token->getValue(); | $value = $raw_token->getValue(); | ||||
| $is_not = ($raw_token->getOperator() == $op_not); | |||||
| if ($raw_token->getOperator() == $op_sub) { | if ($raw_token->getOperator() == $op_sub) { | ||||
| $is_substring = true; | $is_substring = true; | ||||
| } else { | } else { | ||||
| $is_substring = false; | $is_substring = false; | ||||
| } | } | ||||
| // If we're doing substring search, we just match against the raw corpus | // If we're doing substring search, we just match against the raw corpus | ||||
| // and we're done. | // and we're done. | ||||
| if ($is_substring) { | if ($is_substring) { | ||||
| if ($is_not) { | |||||
| $where[] = qsprintf( | |||||
| $conn, | |||||
| '(ftfield.rawCorpus NOT LIKE %~)', | |||||
| $value); | |||||
| } else { | |||||
| $where[] = qsprintf( | $where[] = qsprintf( | ||||
| $conn, | $conn, | ||||
| '(ftfield.rawCorpus LIKE %~)', | '(ftfield.rawCorpus LIKE %~)', | ||||
| $value); | $value); | ||||
| } | |||||
| continue; | continue; | ||||
| } | } | ||||
| // Otherwise, we need to match against the term corpus and the normal | // Otherwise, we need to match against the term corpus and the normal | ||||
| // corpus, so that searching for "raw" does not find "strawberry". | // corpus, so that searching for "raw" does not find "strawberry". | ||||
| if ($raw_token->isQuoted()) { | if ($raw_token->isQuoted()) { | ||||
| $is_quoted = true; | $is_quoted = true; | ||||
| $is_stemmed = false; | $is_stemmed = false; | ||||
| } else { | } else { | ||||
| $is_quoted = false; | $is_quoted = false; | ||||
| $is_stemmed = true; | $is_stemmed = true; | ||||
| } | } | ||||
| // Never stem negated queries, since this can exclude results users | |||||
| // did not mean to exclude and generally confuse things. | |||||
| if ($is_not) { | |||||
| $is_stemmed = false; | |||||
| } | |||||
| $term_constraints = array(); | $term_constraints = array(); | ||||
| $term_value = ' '.$ngram_engine->newTermsCorpus($value).' '; | $term_value = ' '.$ngram_engine->newTermsCorpus($value).' '; | ||||
| if ($is_not) { | |||||
| $term_constraints[] = qsprintf( | |||||
| $conn, | |||||
| '(ftfield.termCorpus NOT LIKE %~)', | |||||
| $term_value); | |||||
| } else { | |||||
| $term_constraints[] = qsprintf( | $term_constraints[] = qsprintf( | ||||
| $conn, | $conn, | ||||
| '(ftfield.termCorpus LIKE %~)', | '(ftfield.termCorpus LIKE %~)', | ||||
| $term_value); | $term_value); | ||||
| } | |||||
| if ($is_stemmed) { | if ($is_stemmed) { | ||||
| $stem_value = $stemmer->stemToken($value); | $stem_value = $stemmer->stemToken($value); | ||||
| $stem_value = $ngram_engine->newTermsCorpus($stem_value); | $stem_value = $ngram_engine->newTermsCorpus($stem_value); | ||||
| $stem_value = ' '.$stem_value.' '; | $stem_value = ' '.$stem_value.' '; | ||||
| $term_constraints[] = qsprintf( | $term_constraints[] = qsprintf( | ||||
| $conn, | $conn, | ||||
| '(ftfield.normalCorpus LIKE %~)', | '(ftfield.normalCorpus LIKE %~)', | ||||
| $stem_value); | $stem_value); | ||||
| } | } | ||||
| if ($is_quoted) { | if ($is_not) { | ||||
| $where[] = qsprintf( | |||||
| $conn, | |||||
| '(%Q)', | |||||
| implode(' AND ', $term_constraints)); | |||||
| } else if ($is_quoted) { | |||||
| $where[] = qsprintf( | $where[] = qsprintf( | ||||
| $conn, | $conn, | ||||
| '(ftfield.rawCorpus LIKE %~ AND (%Q))', | '(ftfield.rawCorpus LIKE %~ AND (%Q))', | ||||
| $value, | $value, | ||||
| implode(' OR ', $term_constraints)); | implode(' OR ', $term_constraints)); | ||||
| } else { | } else { | ||||
| $where[] = qsprintf( | $where[] = qsprintf( | ||||
| $conn, | $conn, | ||||
| ▲ Show 20 Lines • Show All 765 Lines • Show Last 20 Lines | |||||