Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…elasticsearch into aggregations
  • Loading branch information
tomasz-grobelny committed Sep 5, 2018
2 parents e306ea6 + 85e5b93 commit 4dd31a2
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 39 deletions.
26 changes: 15 additions & 11 deletions lib/Platform/ElasticSearchPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Exception;
use OCA\FullTextSearch\Exceptions\InterruptException;
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
use OCA\FullTextSearch\IFullTextSearchPlatform;
use OCA\FullTextSearch\IFullTextSearchProvider;
use OCA\FullTextSearch\Model\DocumentAccess;
use OCA\FullTextSearch\Model\Index;
use OCA\FullTextSearch\Model\IndexDocument;
use OCA\FullTextSearch\Model\Runner;
use OCA\FullTextSearch\Model\SearchRequest;
use OCA\FullTextSearch\Model\SearchResult;
use OCA\FullTextSearch_ElasticSearch\AppInfo\Application;
use OCA\FullTextSearch_ElasticSearch\Exceptions\AccessIsEmptyException;
use OCA\FullTextSearch_ElasticSearch\Exceptions\ConfigurationException;
Expand Down Expand Up @@ -132,8 +131,7 @@ public function setRunner(Runner $runner) {
* @param $action
* @param bool $force
*
* @throws InterruptException
* @throws TickDoesNotExistException
* @throws Exception
*/
private function updateRunnerAction($action, $force = false) {
if ($this->runner === null) {
Expand Down Expand Up @@ -258,10 +256,6 @@ public function indexDocuments(IFullTextSearchProvider $provider, $documents) {
* @param IndexDocument $document
*
* @return Index
* @throws AccessIsEmptyException
* @throws ConfigurationException
* @throws InterruptException
* @throws TickDoesNotExistException
*/
public function indexDocument(IFullTextSearchProvider $provider, IndexDocument $document) {

Expand Down Expand Up @@ -317,8 +311,7 @@ public function indexDocument(IFullTextSearchProvider $provider, IndexDocument $
* @return array
* @throws AccessIsEmptyException
* @throws ConfigurationException
* @throws InterruptException
* @throws TickDoesNotExistException
* @throws \Exception
*/
private function indexDocumentError(
IFullTextSearchProvider $provider, IndexDocument $document, Exception $e
Expand Down Expand Up @@ -392,7 +385,18 @@ public function deleteIndexes($indexes) {
public function searchDocuments(
IFullTextSearchProvider $provider, DocumentAccess $access, SearchRequest $request
) {
return $this->searchService->searchDocuments($this->client, $provider, $access, $request);
return null;
// return $this->searchService->searchDocuments($this->client, $provider, $access, $request);
}



/**
* {@inheritdoc}
* @throws Exception
*/
public function searchRequest(SearchResult $result, DocumentAccess $access) {
$this->searchService->searchRequest($this->client, $result, $access);
}


Expand Down
8 changes: 3 additions & 5 deletions lib/Service/SearchMappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ public function __construct(ConfigService $configService, MiscService $miscServi
* @throws ConfigurationException
* @throws SearchQueryGenerationException
*/
public function generateSearchQuery(
IFullTextSearchProvider $provider, DocumentAccess $access, SearchRequest $request
) {
$query['params'] = $this->generateSearchQueryParams($provider, $access, $request);
public function generateSearchQuery(SearchRequest $request, DocumentAccess $access, IFullTextSearchProvider $provider) {
$query['params'] = $this->generateSearchQueryParams($request, $access, $provider);

return $query;
}
Expand All @@ -84,7 +82,7 @@ public function generateSearchQuery(
* @throws SearchQueryGenerationException
*/
public function generateSearchQueryParams(
IFullTextSearchProvider $provider, DocumentAccess $access, SearchRequest $request
SearchRequest $request, DocumentAccess $access, IFullTextSearchProvider $provider
) {
$params = [
'index' => $this->configService->getElasticIndex(),
Expand Down
104 changes: 87 additions & 17 deletions lib/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,113 @@ public function __construct(
}


// /**
// * @param Client $client
// * @param IFullTextSearchProvider $provider
// * @param DocumentAccess $access
// * @param SearchRequest $request
// *
// * @throws ConfigurationException
// * @throws Exception
// */
// public function searchDocuments(
// Client $client, IFullTextSearchProvider $provider, DocumentAccess $access,
// SearchRequest $request
// ) {
// try {
// $query = $this->searchMappingService->generateSearchQuery($provider, $access, $request);
// } catch (SearchQueryGenerationException $e) {
// return null;
// }
//
// try {
// $result = $client->search($query['params']);
// } catch (Exception $e) {
// $this->miscService->log(
// 'debug - request: ' . json_encode($request) . ' - query: ' . json_encode($query)
// );
// throw $e;
// }
//
// $searchResult = $this->generateSearchResultFromResult($result);
//
// foreach ($result['hits']['hits'] as $entry) {
// $searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
// }
// }

/**
* @param Client $client
* @param IFullTextSearchProvider $provider
* @param SearchResult $searchResult
* @param DocumentAccess $access
* @param SearchRequest $request
*
* @return SearchResult
* @throws ConfigurationException
* @throws Exception
*/
public function searchDocuments(
Client $client, IFullTextSearchProvider $provider, DocumentAccess $access,
SearchRequest $request
public function searchRequest(Client $client, SearchResult $searchResult, DocumentAccess $access
) {
try {
$query = $this->searchMappingService->generateSearchQuery($provider, $access, $request);
$query = $this->searchMappingService->generateSearchQuery(
$searchResult->getRequest(), $access, $searchResult->getProvider()
);
} catch (SearchQueryGenerationException $e) {
return null;
return;
}

try {
$result = $client->search($query['params']);
} catch (Exception $e) {
$this->miscService->log(
'debug - request: ' . json_encode($request) . ' - query: ' . json_encode($query)
'debug - request: ' . json_encode($searchResult->getRequest()) . ' - query: '
. json_encode($query)
);
throw $e;
}

$searchResult = $this->generateSearchResultFromResult($result);
$this->updateSearchResult($searchResult, $result);

foreach ($result['hits']['hits'] as $entry) {
$searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
}

return $searchResult;
}
// /**
// * @param Client $client
// * @param IFullTextSearchProvider $provider
// * @param DocumentAccess $access
// * @param SearchResult $result
// *
// * @return SearchResult
// * @throws ConfigurationException
// */
// public function fillSearchResult(
// Client $client, IFullTextSearchProvider $provider, DocumentAccess $access,
// SearchResult $searchResult
// ) {
// try {
// $query = $this->searchMappingService->generateSearchQuery(
// $provider, $access, $searchResult->getRequest()
// );
// } catch (SearchQueryGenerationException $e) {
// return null;
// }
//
// try {
// $result = $client->search($query['params']);
// } catch (Exception $e) {
// $this->miscService->log(
// 'debug - request: ' . json_encode($searchResult->getRequest()) . ' - query: '
// . json_encode($query)
// );
// throw $e;
// }
//
// $this->updateSearchResult($searchResult, $result);
//
// foreach ($result['hits']['hits'] as $entry) {
// $searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
// }
//
// return $searchResult;
// }


/**
Expand All @@ -121,6 +191,8 @@ public function getDocument(Client $client, $providerId, $documentId) {
$index->setMetaTags($result['_source']['metatags']);
$index->setSubTags($result['_source']['subtags']);
$index->setTags($result['_source']['tags']);
$index->setMore($result['_source']['more']);
// $index->setInfo($result['_source']['info']);
$index->setHash($result['_source']['hash']);
$index->setSource($result['_source']['source']);
$index->setTitle($result['_source']['title']);
Expand All @@ -132,12 +204,10 @@ public function getDocument(Client $client, $providerId, $documentId) {


/**
* @param SearchResult $searchResult
* @param array $result
*
* @return SearchResult
*/
private function generateSearchResultFromResult($result) {
$searchResult = new SearchResult();
private function updateSearchResult(SearchResult $searchResult, $result) {
$searchResult->setRawResult(json_encode($result));

$searchResult->setTotal($result['hits']['total']);
Expand Down
8 changes: 2 additions & 6 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
namespace OCA\FullTextSearch_ElasticSearch\Settings;

use Exception;
use OCA\FullTextSearch\AppInfo\Application as FullTextSearch;
use OCA\FullTextSearch_ElasticSearch\AppInfo\Application;
use OCA\FullTextSearch_ElasticSearch\Service\ConfigService;
use OCA\FullTextSearch_ElasticSearch\Service\MiscService;
Expand Down Expand Up @@ -58,7 +57,8 @@ class Admin implements ISettings {
* @param MiscService $miscService
*/
public function __construct(
IL10N $l10n, IURLGenerator $urlGenerator, ConfigService $configService, MiscService $miscService
IL10N $l10n, IURLGenerator $urlGenerator, ConfigService $configService,
MiscService $miscService
) {
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
Expand All @@ -80,10 +80,6 @@ public function getForm() {
* @return string the section ID, e.g. 'sharing'
*/
public function getSection() {
if (class_exists(FullTextSearch::class)) {
return FullTextSearch::APP_NAME;
}

return 'fulltextsearch';
}

Expand Down

0 comments on commit 4dd31a2

Please sign in to comment.