-
Notifications
You must be signed in to change notification settings - Fork 2
NGSTACK-809 index file content #21
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
Open
AntePrkacin
wants to merge
17
commits into
master
Choose a base branch
from
NGSTACK-809-index-file-content
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
67f4b41
NGSTACK-809 add FileTextExtractor service that uses Apache Tika to ex…
AntePrkacin 2640e24
NGSTACK-809 override Indexable BinaryFile SearchField by adding 'file…
AntePrkacin 12915d1
NGSTACK-809 add Apache Tika jar file
AntePrkacin f9d8e96
NGSTACK-809 add new config params for 'file_text_extraction' key to b…
AntePrkacin 802748f
NGSTACK-809 remove tika-app.jar file from bin directory
AntePrkacin f492a5d
NGSTACK-809 require vaites/php-apache-tika bundle
AntePrkacin 061152d
NGSTACK-809 update config params for apache_tika node and instantiate…
AntePrkacin 0cea80a
NGSTACK-809 update FileTextExtractor service to use newly required Va…
AntePrkacin f9e5121
NGSTACK-809 change configuration params to include 'file_indexing' an…
ac0745c
NGSTACK-809 inject fileIndexingEnabled config param into BinaryFile/S…
57a884b
NGSTACK-809 extract service definitions for SearchField and FileTextE…
d0e8115
NGSTACK-809 move 'vaites/php-apache-tika' bundle into 'suggest' part …
cdb458f
NGSTACK-809 update .gitignore file with .idea folder
69ccef6
NGSTACK-809 add defaults for 'file_indexing' configuration
349a7a1
NGSTACK-809 make 'apache.tika_client' dependency optional for FileTex…
796d58c
NGSTACK-809 update README.md with file_indexing info and config
4a53f1a
NGSTACK-809 remove FullTextField from getIndexDefinition() method in …
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 hidden or 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ composer.lock | |
var | ||
__solr | ||
docs/_build | ||
.idea/ |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\IbexaSearchExtra\Core\FieldType\BinaryFile; | ||
|
||
use Ibexa\Contracts\Core\FieldType\Indexable; | ||
use Ibexa\Contracts\Core\Persistence\Content\Field; | ||
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition; | ||
use Ibexa\Contracts\Core\Search; | ||
use Netgen\IbexaSearchExtra\Core\Search\Common\PageIndexing\TextExtractor\FileTextExtractor; | ||
|
||
use function trim; | ||
|
||
/** | ||
* Indexable definition for BinaryFile field type. | ||
*/ | ||
final class SearchField implements Indexable | ||
{ | ||
/** @var array<int, array<int, string>> */ | ||
private array $cache = []; | ||
|
||
public function __construct( | ||
private readonly Indexable $innerField, | ||
private readonly FileTextExtractor $fileTextExtractor, | ||
private readonly bool $fileIndexingEnabled, | ||
) {} | ||
|
||
public function getIndexData(Field $field, FieldDefinition $fieldDefinition): array | ||
{ | ||
$searchFields = $this->innerField->getIndexData($field, $fieldDefinition); | ||
|
||
if (!$this->fileIndexingEnabled) { | ||
return $searchFields; | ||
} | ||
|
||
$text = $this->extractText($field); | ||
|
||
if ($text !== '') { | ||
$searchFields[] = new Search\Field( | ||
'file_text', | ||
$text, | ||
new Search\FieldType\FullTextField(), | ||
); | ||
} | ||
|
||
return $searchFields; | ||
} | ||
|
||
public function getIndexDefinition(): array | ||
{ | ||
return $this->innerField->getIndexDefinition(); | ||
} | ||
|
||
public function getDefaultMatchField(): ?string | ||
{ | ||
return $this->innerField->getDefaultMatchField(); | ||
} | ||
|
||
public function getDefaultSortField(): ?string | ||
{ | ||
return $this->innerField->getDefaultSortField(); | ||
} | ||
|
||
private function extractText(Field $field): string | ||
{ | ||
if (!isset($this->cache[$field->id][$field->versionNo])) { | ||
$this->cache = []; | ||
$this->cache[$field->id][$field->versionNo] = trim($this->fileTextExtractor->extractFromPersistenceField($field)); | ||
} | ||
|
||
return $this->cache[$field->id][$field->versionNo]; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
lib/Core/Search/Common/PageIndexing/TextExtractor/FileTextExtractor.php
This file contains hidden or 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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\IbexaSearchExtra\Core\Search\Common\PageIndexing\TextExtractor; | ||
|
||
use Ibexa\Contracts\Core\Persistence\Content\Field; | ||
use Ibexa\Core\FieldType\BinaryBase\Value; | ||
use Ibexa\Core\IO\IOServiceInterface; | ||
use RuntimeException; | ||
use Vaites\ApacheTika\Client as ApacheTikaClient; | ||
|
||
use function in_array; | ||
use function sprintf; | ||
|
||
/** | ||
* Extract text from Ibexa file based field types. | ||
*/ | ||
final class FileTextExtractor | ||
{ | ||
public function __construct( | ||
private readonly IOServiceInterface $binaryFileIoService, | ||
private readonly ?ApacheTikaClient $apacheTikaClient, | ||
private readonly array $allowedMimeTypes, | ||
) {} | ||
|
||
public function extractFromPersistenceField(Field $field): string | ||
{ | ||
return isset($field->value->externalData['id']) | ||
? $this->extractByFileId($field->value->externalData['id']) | ||
: ''; | ||
} | ||
|
||
public function extractFromValue(Value $value): string | ||
{ | ||
return $value->id === null ? '' : $this->extractByFileId($value->id); | ||
} | ||
|
||
public function extractByFileId(string $fileId): string | ||
{ | ||
if ($this->apacheTikaClient === null) { | ||
return ''; | ||
} | ||
|
||
$mimeType = $this->binaryFileIoService->getMimeType($fileId); | ||
|
||
if (!in_array($mimeType, $this->allowedMimeTypes, true)) { | ||
return ''; | ||
} | ||
|
||
$file = $this->binaryFileIoService->loadBinaryFile($fileId); | ||
|
||
try { | ||
return $this->apacheTikaClient->getText(sprintf('public%s', $file->uri)); | ||
} catch (\Exception $e) { | ||
throw new RuntimeException( | ||
sprintf( | ||
'Could not extract text from file with ID "%s": %s (%s)', | ||
$fileId, | ||
$e->getMessage(), | ||
$e->getCode(), | ||
), | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
We can make this optional if we check in the bundle Extension whether the package exists. The dependency should then be moved to
suggest
section ofcomposer.json
.Uh oh!
There was an error while loading. Please reload this page.
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.
d0e8115
Moved
vaites/php-apache-tika
bundle under "suggest" section ofcomposer.json
file. In the bundle Extension, I check if theCLIClient
andWebClient
classes fromvaites/php-apache-tika
bundle exist and then loadsearch/file_indexing.yaml
.