-
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
Advanced search #24792
Closed
Closed
Advanced search #24792
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14679a9
add ScoredResult
georgehrke 3659d26
add IIndixer interface and DatabaseIndexer placeholder
georgehrke 3171124
extend common storage classes
georgehrke ec1c911
use \OCA\Files\Search\DatabaseIndexer as default indexer
georgehrke 246154a
add SearchController
georgehrke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
/** | ||
* @author Georg Ehrke <georg@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @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 OCA\Files\Controller; | ||
|
||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Controller; | ||
use OCP\Files\NotFoundException; | ||
use OCP\Files\Search\IIndexer; | ||
use OCP\IConfig; | ||
use OCP\IRequest; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\Http\DataDisplayResponse; | ||
use OCP\AppFramework\Http\Response; | ||
use OCP\AppFramework\Http\NotFoundResponse; | ||
use OCP\Files\Folder; | ||
use OCP\IUserSession; | ||
use \OCP\Files\IRootFolder; | ||
use OCP\Search\ScoredResult; | ||
|
||
|
||
/** | ||
* Class SearchController | ||
* | ||
* @package OCA\Files\Controller | ||
*/ | ||
class SearchController extends Controller { | ||
|
||
/** IUserSession */ | ||
private $userSession; | ||
/** string */ | ||
private $userId; | ||
/** IConfig */ | ||
private $config; | ||
/** IRootFolder */ | ||
protected $rootFolder; | ||
|
||
/** | ||
* SearchController constructor. | ||
* | ||
* @param string $appName | ||
* @param IRequest $request | ||
* @param IUserSession $userSession | ||
* @param IConfig $config | ||
* @param IRootFolder $rootFolder | ||
*/ | ||
public function __construct($appName, | ||
IRequest $request, | ||
IUserSession $userSession, | ||
IConfig $config, | ||
IRootFolder $rootFolder) { | ||
parent::__construct($appName, $request); | ||
$this->userSession = $userSession; | ||
$this->config = $config; | ||
$this->rootFolder = $rootFolder; | ||
|
||
$user = $this->userSession->getUser(); | ||
if (!$user) { | ||
// TODO - throw some exception | ||
} | ||
|
||
$this->userId = $user->getUID(); | ||
} | ||
|
||
/** | ||
* @NoCSRFRequired | ||
* @NoAdminRequired | ||
* | ||
* @param string $phrase phrase to search | ||
* @param string $path limit path to search | ||
* @param integer $page page of pagination | ||
* @param integer $size size of pagination | ||
*/ | ||
public function search($phrase, $path='/', $page=null, $size=null) { | ||
if ($path === '') { | ||
$path = '/'; | ||
} | ||
$path = '/' . $this->userId . '/files' . $path; | ||
|
||
$indexStorageMap = []; | ||
|
||
// Collect mounts to search | ||
try { | ||
$mounts = $this->rootFolder->getMountsIn($path); | ||
foreach($mounts as $mount) { | ||
/** @var \OCP\Files\Mount\IMountPoint $mount */ | ||
/** @var \OCP\Files\Storage $storage */ | ||
$storage = $mount->getStorage(); | ||
$indexerIdentifier = $storage->getIndexerIdentifier(); | ||
|
||
if (!isset($indexStorageMap[$indexerIdentifier])) { | ||
$indexStorageMap[$indexerIdentifier] = []; | ||
} | ||
$indexStorageMap[$indexerIdentifier][] = $storage; | ||
} | ||
} catch(NotFoundException $ex) { | ||
|
||
} | ||
|
||
$indexers = []; | ||
foreach($indexStorageMap as $singleIndexer => $storages) { | ||
if (!class_exists($singleIndexer)) { | ||
continue; | ||
} | ||
|
||
try { | ||
/** @var IIndexer $instance */ | ||
$instance = new $singleIndexer(); | ||
$instance->setStorages($storages); | ||
$indexers[] = $instance; | ||
} catch(\Exception $ex) { | ||
//TODO | ||
} | ||
} | ||
|
||
// Search Indexers from 0 to $page + $size | ||
// Some better approximation of what $page should look like | ||
// should be added in a later version | ||
$indexerSize = $page + $size; | ||
|
||
$results = []; | ||
foreach($indexers as $indexer) { | ||
/** @var IIndexer $indexer */ | ||
$results = array_merge($results, $indexer->search($phrase, $path, 0, $indexerSize)); | ||
} | ||
|
||
usort($results, function(ScoredResult $a, ScoredResult $b) { | ||
return ($a->score < $b->score) ? -1 : 1; | ||
}); | ||
$results = array_slice($results, $page, $size); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* @author Georg Ehrke <georg@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @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 OCA\Files\Search; | ||
|
||
use OCP\Files\Search\IIndexer; | ||
|
||
class DatabaseIndexer implements IIndexer { | ||
|
||
/** | ||
* @var \OCP\Files\Storage\IStorage[] | ||
*/ | ||
protected $storages; | ||
|
||
/** | ||
* @param \OCP\Files\Storage\IStorage[] $storages | ||
*/ | ||
public function setStorages($storages) { | ||
$this->storages = $storages; | ||
} | ||
|
||
/** | ||
* @param $query | ||
* @param null|integer $page | ||
* @param null|integer $size | ||
* @return \OCP\Search\ScoredResult[] | ||
*/ | ||
public function search($query, $page=null, $size=null) { | ||
return []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* @author Georg Ehrke <georg@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @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 OCP\Files\Search; | ||
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. Folders on the path need to be uppercase |
||
|
||
/** | ||
* indexes storage to provide performant search | ||
* | ||
* @since 9.1.0 | ||
*/ | ||
interface IIndexer { | ||
|
||
/** | ||
* list of storages indexer is supposed to query | ||
* @param \OCP\Files\Storage\IStorage[] $storages | ||
*/ | ||
public function setStorages($storages); | ||
|
||
/** | ||
* search storages for query | ||
* @param string $query | ||
* @param string $path limit search to a certain subdirectory | ||
* @param null|integer $page | ||
* @param null|integer $size | ||
* @return \OCP\Search\ScoredResult[] | ||
* @since 9.1.0 | ||
*/ | ||
public function search($query, $path, $page=null, $size=null); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* @author Georg Ehrke <georg@ownCloud.com> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @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 OCP\Search; | ||
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. File can not be loaded, search folder needs to be uppercase |
||
|
||
/** | ||
* The generic result of a search | ||
* @since 9.1.0 | ||
*/ | ||
class ScoredResult extends Result { | ||
|
||
/** | ||
* A score for the result, ranks between 0 and 1 | ||
* @var float | ||
* @since 9.1.0 | ||
*/ | ||
public $score; | ||
|
||
/** | ||
* Highlight to what part of the result matches the search | ||
* @var string | ||
* @since 9.1.0 | ||
*/ | ||
public $highlight; | ||
|
||
/** | ||
* Create a new scored search result | ||
* @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' | ||
* @param string $name displayed text of result | ||
* @param string $link URL to the result within its app | ||
* @param float $score score the result | ||
* @param string $highlight for little preview of result | ||
* @since 9.1.0 | ||
*/ | ||
public function __construct($id = null, $name = null, $link = null, $score = null, $highlight = null) { | ||
parent::__construct($id, $name, $link); | ||
$this->score = $score; | ||
$this->highlight = $highlight; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I assume this is temporal until we do search by webdav ....
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.
Yes, I thought Webdav search was not somethings thats mandatory for 9.1