-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Extend accounts table for custom search attributes #27832
Changes from all commits
3422d3d
8e00ed3
9d75572
29fcb72
25b9ce4
5ebc7bc
db21e77
e010774
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* @author Tom Needham <[email protected]> | ||
* | ||
* @copyright Copyright (c) 2017, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
namespace OC\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use OCP\Migration\ISchemaMigration; | ||
|
||
/** | ||
* Adds a string column to the accounts table that can contain search | ||
* attributes provided by user backends | ||
*/ | ||
class Version20170510143952 implements ISchemaMigration { | ||
|
||
public function changeSchema(Schema $schema, array $options) { | ||
$prefix = $options['tablePrefix']; | ||
$table = $schema->getTable("{$prefix}accounts"); | ||
|
||
// Add column to hold additional search attributes | ||
$table->addColumn('search_attributes', 'string', [ | ||
'notnull' => false, | ||
'length' => 185, // Max index length | ||
]); | ||
|
||
// Add index for search attributes | ||
$table->addIndex(['search_attributes'], 'search_attributes_index'); | ||
|
||
// Index to improve search performance of display_name column | ||
$table->addIndex(['display_name'], 'display_name_index'); | ||
|
||
// Index to improve search performance of email column | ||
$table->addIndex(['email'], 'email_index'); | ||
|
||
// Index to improve search performance of lower_user_id column | ||
$table->addUniqueIndex(['lower_user_id'], 'lower_user_id_index'); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,25 @@ public function search($fieldName, $pattern, $limit, $offset) { | |
return $this->findEntities($qb->getSQL(), $qb->getParameters(), $limit, $offset); | ||
} | ||
|
||
/** | ||
* @param string $pattern | ||
* @param integer $limit | ||
* @param integer $offset | ||
* @return Account[] | ||
*/ | ||
public function find($pattern, $limit, $offset) { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('*') | ||
->from($this->getTableName()) | ||
->where($qb->expr()->Like('user_id', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter(strtolower($pattern)) . '%'))) | ||
->orWhere($qb->expr()->iLike('display_name', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoting comment from http://stackoverflow.com/a/34029944:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we have hidden magic then it's ok. |
||
->orWhere($qb->expr()->iLike('email', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))) | ||
->orWhere($qb->expr()->iLike('search_attributes', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))) | ||
->orderBy('display_name'); | ||
|
||
return $this->findEntities($qb->getSQL(), $qb->getParameters(), $limit, $offset); | ||
} | ||
|
||
public function getUserCountPerBackend($hasLoggedIn) { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select(['backend', $qb->createFunction('count(*) as `count`')]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need a combined index if one query searches in multiple columns
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomneedham any update ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope. we need an index per or part of a query:
then again like kills the indexes on sqlite anyway:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh well. the
%test%
like comparison kills the index on all dbms. If we usetest%
it is much better:sqlite never seems to use an index for like
mysql can use separate indexes as well as a combined index ... if we don't do medial searches.
for ldap we have
'user_ldap.enable_medial_search'
in config.php to allow medial searches. we could do the same for sql. say ...'accounts.enable_medial_search'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also see http://use-the-index-luke.com/de/sql/where/bereiche/like-filter