Skip to content
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

NGSTACK-956 search boost per content type/field #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$this->addIndexableFieldTypeSection($rootNode);
$this->addSearchResultExtractorSection($rootNode);
$this->addAsynchronousIndexingSection($rootNode);
$this->addFulltextBoostSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -73,4 +74,61 @@ private function addAsynchronousIndexingSection(ArrayNodeDefinition $nodeDefinit
->end()
->end();
}

private function addFulltextBoostSection(ArrayNodeDefinition $nodeDefinition): void
{
$nodeDefinition
->children()
->arrayNode('search_boost')
->info('Search boost configuration')
->addDefaultsIfNotSet()
->children()
->arrayNode('content_types')
->info('Define boost value per content type')
->useAttributeAsKey('name')
->normalizeKeys(false)
->arrayPrototype()
->children()
->integerNode('id')
->info('Content type id')
->isRequired()
->end()
->floatNode('boost_value')
->info('Boost value for the content type')
->isRequired()
->end()
->end()
->end()
->end()
->arrayNode('raw_fields')
->info('Boost values for raw fields')
->useAttributeAsKey('name')
->normalizeKeys(false)
->floatPrototype()
->info('Boost value for the raw field')
->end()
->end()
->arrayNode('meta_fields')
->info('Boost values for meta fields')
->useAttributeAsKey('name')
->normalizeKeys(false)
->floatPrototype()
->info('Boost value for the meta field')
->end()
->end()
->end()
->end()
->arrayNode('field_mapper_custom_fulltext_field_config')
->info('Custom fulltext field mapping')
->useAttributeAsKey('name')
->normalizeKeys(false)
->arrayPrototype()
->scalarPrototype()
->info('List of mapped fields')
->end()
->end()
->end()
->end();

}
}
31 changes: 31 additions & 0 deletions bundle/DependencyInjection/NetgenIbexaSearchExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private function processExtensionConfiguration(array $configs, ContainerBuilder
$this->processIndexableFieldTypeConfiguration($configuration, $container);
$this->processSearchResultExtractorConfiguration($configuration, $container);
$this->processAsynchronousIndexingConfiguration($configuration, $container);
$this->processFullTextBoostConfiguration($configuration, $container);
}

private function processSearchResultExtractorConfiguration(array $configuration, ContainerBuilder $container): void
Expand Down Expand Up @@ -117,4 +118,34 @@ private function processAsynchronousIndexingConfiguration(array $configuration,
$configuration['use_asynchronous_indexing'],
);
}

private function processFullTextBoostConfiguration(array $configuration, ContainerBuilder $container)
{
$fullTextBoostConfig = $container->getParameter('netgen_ibexa_search_extra')['search_boost'];

$container->setParameter(
'netgen_ibexa_search_extra.search_boost',
$configuration['search_boost'] ?? [],
);

$container->setParameter(
'netgen_ibexa_search_extra.field_mapper_custom_fulltext_field_config',
$configuration['field_mapper_custom_fulltext_field_config'] ?? [],
);

if (!array_key_exists('content_types', $container->getParameter('netgen_ibexa_search_extra.search_boost'))) {
$fullTextBoostConfig['content_types'] = null;
}
if (!array_key_exists('raw_fields', $container->getParameter('netgen_ibexa_search_extra.search_boost'))) {
$fullTextBoostConfig['raw_fields'] = null;
}
if (!array_key_exists('meta_fields', $container->getParameter('netgen_ibexa_search_extra.search_boost'))) {
$fullTextBoostConfig['meta_fields'] = null;
}

$container->setParameter(
'netgen_ibexa_search_extra.search_boost',
$fullTextBoostConfig,
);
}
}
142 changes: 142 additions & 0 deletions lib/API/Values/Content/Query/Criterion/FullText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Netgen\IbexaSearchExtra\API\Values\Content\Query\Criterion;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator\Specifications;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CustomFieldInterface;
use InvalidArgumentException;
use Netgen\IbexaSearchExtra\API\Values\Content\SpellcheckQuery;
use RuntimeException;

class FullText extends Criterion implements CustomFieldInterface, FulltextSpellcheck
{
/**
* Fuzziness of the fulltext search.
*
* May be a value between 0. (fuzzy) and 1. (sharp).
*/
public float $fuzziness = 1.;
/**
* Boost for certain fields.
*
* Array of boosts to apply for certain fields – the array should look like
* this:
*
* <code>
* array(
* 'title' => 2,
* …
* )
* </code>
*
* @var array<string, mixed>
*/
public array $boost = [];
/**
* Boost for certain solr fields.
*
* Array of boosts to apply for certain fields – the array should look like
* this:
*
* <code>
* array(
* 'meta_content__name_t' => 2,
* …
* )
* </code>
*
* @var array<string, mixed>
*/
public array $solrFieldsBoost = [];
/**
* Boost for certain content types.
*
* Array of boosts to apply for certain content type – the array should look like
* this:
*
* <code>
* array(
* 'content_type_identifier' => array(
* 'id' => 2,
* 'boost' => 3
* )
* …
* )
* </code>
*
* @var array<string, mixed>
*/
public array $contentTypeBoost = [];
/**
* Boost for certain fulltext meta fields.
*
* Array of boosts to apply for certain meta fields – the array should look like
* this:
*
* <code>
* array(
* 'meta_field_key' => 2,
* …
* )
* </code>
*
* @var array<string, mixed>
*/
public array $metaFieldsBoost = [];
/**
* Analyzer configuration.
*/
public mixed $analyzers;
/**
* Analyzer wildcard handling configuration.
*/
public mixed $wildcards;
/**
* Custom field definitions to query instead of default field.
*
* @var array<string, mixed>
*/
private array $customFields = [];
/**
* @param array<string, mixed> $properties
*/
public function __construct(mixed $value, array $properties = [])
{
parent::__construct(null, Operator::LIKE, $value);
foreach ($properties as $name => $propertyValue) {
if (!property_exists($this, $name)) {
throw new InvalidArgumentException(sprintf('Unknown property %s.', $name));
}
$this->{$name} = $propertyValue;
}

}
public function getSpecifications(): array
{
return [
new Specifications(Operator::LIKE, Specifications::FORMAT_SINGLE),
];
}
public function setCustomField(string $type, string $field, string $customField): void
{
$this->customFields[$type][$field] = $customField;
}
public function getCustomField(string $type, string $field): ?string
{
return $this->customFields[$type][$field] ?? null;
}
public function getSpellcheckQuery(): SpellcheckQuery
{
if (!is_string($this->value)) {
throw new RuntimeException(
sprintf('FullText criterion value should be a string, %s given', get_debug_type($this->value)),
);
}
$spellcheckQuery = new SpellcheckQuery();
$spellcheckQuery->query = $this->value;
$spellcheckQuery->count = 10;
return $spellcheckQuery;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Netgen\IbexaSearchExtra\Core\Search\Solr\FieldMapper\ContentTranslation;

use Ibexa\Contracts\Core\Persistence\Content as SPIContent;
use Ibexa\Contracts\Solr\FieldMapper\ContentTranslationFieldMapper;
use Ibexa\Contracts\Core\Persistence\Content\Type as ContentType;
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition;
use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as ContentTypeHandler;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Search\Field;
use Ibexa\Contracts\Core\Search\FieldType\FullTextField;
use Ibexa\Contracts\Core\Search\FieldType\TextField;
use Ibexa\Core\Search\Common\FieldRegistry;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use function count;
use function in_array;
use function sprintf;
class CustomFulltextFieldMapper extends ContentTranslationFieldMapper
{
/**
* @var array<string, mixed>
*/
private array $fieldConfig = [];
public function __construct(
private readonly ContentTypeHandler $contentTypeHandler,
private readonly FieldRegistry $fieldRegistry,
private readonly ParameterBagInterface $parameterBag,
) {}
/**
* @param string $languageCode
*/
public function accept(SPIContent $content, $languageCode): bool
{
$this->fieldConfig = $this->parameterBag->get('ibexa_search_extra.search_boost')['field_mapper_custom_fulltext_field_config'];
return count($this->fieldConfig) > 0;
}
public function mapFields(SPIContent $content, $languageCode): array
{
$fields = [];
try {
$contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
} catch (NotFoundException) {
return $fields;
}
foreach ($content->fields as $field) {
if ($field->languageCode !== $languageCode) {
continue;
}
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
if (!$fieldDefinition->isSearchable) {
continue;
}
if ($fieldDefinition->id !== $field->fieldDefinitionId) {
continue;
}
$fieldNames = $this->getFieldNames($fieldDefinition, $contentType);
if (count($fieldNames) === 0) {
continue;
}
$fieldType = $this->fieldRegistry->getType($field->type);
$indexFields = $fieldType->getIndexData($field, $fieldDefinition);
foreach ($indexFields as $indexField) {
if ($indexField->value === null) {
continue;
}
if (!$indexField->getType() instanceof FullTextField) {
continue;
}
$this->appendField($fields, $indexField, $fieldNames);
}
}
}
return $fields;
}
/**
* @param array<string, mixed> $fields
* @param array<string, mixed> $fieldNames
*/
private function appendField(
array &$fields,
Field $indexField,
array $fieldNames,
): void {
foreach ($fieldNames as $fieldName) {
$fields[] = new Field(
sprintf('meta_%s__text', $fieldName),
(string) $indexField->value,
new TextField(),
);
}
}
/**
* @return array<string>
*/
private function getFieldNames(FieldDefinition $fieldDefinition, ContentType $contentType): array
{
$fieldNames = [];
foreach ($this->fieldConfig as $fieldName => $fieldIdentifiers) {
if ($this->isMapped($fieldDefinition, $contentType, $fieldIdentifiers)) {
$fieldNames[] = $fieldName;
}
}
return $fieldNames;
}
/**
* @param array<string> $fieldIdentifiers
*/
private function isMapped(FieldDefinition $fieldDefinition, ContentType $contentType, array $fieldIdentifiers): bool
{
if (in_array($fieldDefinition->identifier, $fieldIdentifiers, true)) {
return true;
}
$needle = sprintf('%s/%s', $contentType->identifier, $fieldDefinition->identifier);
if (in_array($needle, $fieldIdentifiers, true)) {
return true;
}
return false;
}
}
Loading
Loading