forked from ezsystems/ezpublish-kernel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ezsystems#1051 from ezsystems/impl-EZP-23396-elast…
…icsearch-fulltext Implement EZP-23396: Elasticsearch: Implement Fulltext criterion for Content search
- Loading branch information
Showing
7 changed files
with
290 additions
and
9 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
eZ/Publish/API/Repository/Tests/_fixtures/Elasticsearch/FullText.php
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,38 @@ | ||
<?php | ||
|
||
return eZ\Publish\API\Repository\Values\Content\Search\SearchResult::__set_state(array( | ||
'facets' => | ||
array ( | ||
), | ||
'searchHits' => | ||
array ( | ||
0 => | ||
eZ\Publish\API\Repository\Values\Content\Search\SearchHit::__set_state(array( | ||
'valueObject' => | ||
array ( | ||
'id' => 54, | ||
'title' => 'eZ Publish Demo Design (without demo content)', | ||
), | ||
'score' => 0.49063533999999998, | ||
'index' => NULL, | ||
'highlight' => NULL, | ||
)), | ||
1 => | ||
eZ\Publish\API\Repository\Values\Content\Search\SearchHit::__set_state(array( | ||
'valueObject' => | ||
array ( | ||
'id' => 58, | ||
'title' => 'Contact Us', | ||
), | ||
'score' => 1.0769257999999999, | ||
'index' => NULL, | ||
'highlight' => NULL, | ||
)), | ||
), | ||
'spellSuggestion' => NULL, | ||
'time' => 1, | ||
'timedOut' => NULL, | ||
'maxScore' => 1.0769257999999999, | ||
'totalCount' => 2, | ||
)); | ||
|
27 changes: 27 additions & 0 deletions
27
eZ/Publish/API/Repository/Tests/_fixtures/Elasticsearch/FullTextFiltered.php
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,27 @@ | ||
<?php | ||
|
||
return eZ\Publish\API\Repository\Values\Content\Search\SearchResult::__set_state(array( | ||
'facets' => | ||
array ( | ||
), | ||
'searchHits' => | ||
array ( | ||
0 => | ||
eZ\Publish\API\Repository\Values\Content\Search\SearchHit::__set_state(array( | ||
'valueObject' => | ||
array ( | ||
'id' => 58, | ||
'title' => 'Contact Us', | ||
), | ||
'score' => 1.0769257999999999, | ||
'index' => NULL, | ||
'highlight' => NULL, | ||
)), | ||
), | ||
'spellSuggestion' => NULL, | ||
'time' => 1, | ||
'timedOut' => NULL, | ||
'maxScore' => 1.0769257999999999, | ||
'totalCount' => 1, | ||
)); | ||
|
27 changes: 27 additions & 0 deletions
27
eZ/Publish/API/Repository/Tests/_fixtures/Elasticsearch/FullTextWildcard.php
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,27 @@ | ||
<?php | ||
|
||
return eZ\Publish\API\Repository\Values\Content\Search\SearchResult::__set_state(array( | ||
'facets' => | ||
array ( | ||
), | ||
'searchHits' => | ||
array ( | ||
0 => | ||
eZ\Publish\API\Repository\Values\Content\Search\SearchHit::__set_state(array( | ||
'valueObject' => | ||
array ( | ||
'id' => 58, | ||
'title' => 'Contact Us', | ||
), | ||
'score' => 1, | ||
'index' => NULL, | ||
'highlight' => NULL, | ||
)), | ||
), | ||
'spellSuggestion' => NULL, | ||
'time' => 1, | ||
'timedOut' => NULL, | ||
'maxScore' => 1, | ||
'totalCount' => 1, | ||
)); | ||
|
155 changes: 155 additions & 0 deletions
155
eZ/Publish/Core/Persistence/Elasticsearch/Content/Search/CriterionVisitor/FullText.php
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,155 @@ | ||
<?php | ||
/** | ||
* File containing the FullText criterion visitor class | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\CriterionVisitor; | ||
|
||
use eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\CriterionVisitorDispatcher as Dispatcher; | ||
use eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\CriterionVisitor; | ||
use eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\FieldMap; | ||
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; | ||
use eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface; | ||
|
||
/** | ||
* Visits the FullText criterion | ||
*/ | ||
class FullText extends CriterionVisitor | ||
{ | ||
/** | ||
* Field map | ||
* | ||
* @var \eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\FieldMap | ||
*/ | ||
protected $fieldMap; | ||
|
||
/** | ||
* Create from field map | ||
* | ||
* @param \eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\FieldMap $fieldMap | ||
*/ | ||
public function __construct( FieldMap $fieldMap ) | ||
{ | ||
$this->fieldMap = $fieldMap; | ||
} | ||
|
||
/** | ||
* Get field type information | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface $criterion | ||
* @return array | ||
*/ | ||
protected function getFieldTypes( CustomFieldInterface $criterion ) | ||
{ | ||
return $this->fieldMap->getFieldTypes( $criterion ); | ||
} | ||
|
||
/** | ||
* Check if visitor is applicable to current criterion | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion | ||
* | ||
* @return boolean | ||
*/ | ||
public function canVisit( Criterion $criterion ) | ||
{ | ||
return $criterion instanceof Criterion\FullText; | ||
} | ||
|
||
/** | ||
* Returns nested condition common for filter and query contexts. | ||
* | ||
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If no searchable fields are found for the given criterion target. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion | ||
* | ||
* @return array | ||
*/ | ||
protected function getCondition( Criterion $criterion ) | ||
{ | ||
/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion\FullText $criterion */ | ||
$fields = $this->getFieldTypes( $criterion ); | ||
|
||
// Add field document custom _all field | ||
$queryFields = array( "fields_doc.meta_all" ); | ||
|
||
// Add boosted fields if any | ||
foreach ( $criterion->boost as $field => $boost ) | ||
{ | ||
if ( !isset( $fields[$field] ) ) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach ( $fields[$field] as $fieldNames ) | ||
{ | ||
foreach ( $fieldNames as $fieldName ) | ||
{ | ||
$queryFields[] = sprintf( "fields_doc.{$fieldName}^%.1f", $boost ); | ||
} | ||
} | ||
} | ||
|
||
$condition = array( | ||
"query_string" => array( | ||
"query" => $criterion->value . ( $criterion->fuzziness < 1 ? "~" : "" ), | ||
"fields" => $queryFields, | ||
"fuzziness" => $criterion->fuzziness, | ||
// This one will be heavy, enabled per FullText criterion spec | ||
"allow_leading_wildcard" => true, | ||
// Might make sense to use percentage in addition | ||
"minimum_should_match" => 1, | ||
// Default is OR, changed per FullText criterion spec | ||
"default_operator" => "AND", | ||
), | ||
); | ||
|
||
return $condition; | ||
} | ||
|
||
/** | ||
* Map field value to a proper Elasticsearch filter representation | ||
* | ||
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If no searchable fields are found for the given criterion target. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion | ||
* @param \eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\CriterionVisitorDispatcher $dispatcher | ||
* | ||
* @return mixed | ||
*/ | ||
public function visitFilter( Criterion $criterion, Dispatcher $dispatcher = null ) | ||
{ | ||
return array( | ||
"nested" => array( | ||
"path" => "fields_doc", | ||
"filter" => array( | ||
"query" => $this->getCondition( $criterion ), | ||
), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Map field value to a proper Elasticsearch query representation | ||
* | ||
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If no searchable fields are found for the given criterion target. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion | ||
* @param \eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\CriterionVisitorDispatcher $dispatcher | ||
* | ||
* @return mixed | ||
*/ | ||
public function visitQuery( Criterion $criterion, Dispatcher $dispatcher = null ) | ||
{ | ||
return array( | ||
"nested" => array( | ||
"path" => "fields_doc", | ||
"query" => $this->getCondition( $criterion ), | ||
), | ||
); | ||
} | ||
} |
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