Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getScore example
$sort
[
'order'
]
=
$sort
[
'direction'
]
;
unset
(
$sort
[
'direction'
]
)
;
}
unset
(
$sort
)
;
}
if
(
\
count
(
$criteria
->
getQueries
(
)
)
)
{
$array
[
'query'
]
=
[
]
;
foreach
(
$criteria
->
getQueries
(
)
as
$query
)
{
$arrayQuery
=
[
'score' =>
$query
->
getScore
(
)
,
'scoreField' =>
$query
->
getScoreField
(
)
,
'extensions' =>
$query
->
getExtensions
(
)
,
]
;
$arrayQuery
[
'query'
]
= QueryStringParser::
toArray
(
$query
->
getQuery
(
)
)
;
$array
[
'query'
]
[
]
=
$arrayQuery
;
}
}
if
(
\
count
(
$criteria
->
getGroupFields
(
)
)
)
{
$array
[
'grouping'
]
=
[
]
;
$queries
=
$criteria
->
getQueries
(
)
;
if
(
empty
(
$queries
)
)
{
return
;
}
$bool
=
new
BoolQuery
(
)
;
foreach
(
$queries
as
$query
)
{
$parsed
=
$this
->parser->
parseFilter
(
$query
->
getQuery
(
)
,
$definition
,
$definition
->
getEntityName
(
)
,
$context
)
;
if
(
$parsed
instanceof MatchQuery
)
{
$score
=
(string)
$query
->
getScore
(
)
;
$parsed
->
addParameter
(
'boost',
$score
)
;
$parsed
->
addParameter
(
'fuzziness', '2'
)
;
}
$bool
->
add
(
$parsed
, BoolQuery::SHOULD
)
;
}
$bool
->
addParameter
(
'minimum_should_match', '1'
)
;
$search
->
addQuery
(
$bool
)
;
}
$this
->
getContainer
(
)
->
get
(
'product.repository'
)
->
create
(
$products
, Context::
createDefaultContext
(
)
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
addQuery
(
new
ScoreQuery
(
new
ContainsFilter
(
'name', 'John'
)
, 100
)
)
;
$criteria
->
addQuery
(
new
ScoreQuery
(
new
ContainsFilter
(
'name', 'Doe'
)
, 100
)
)
;
$criteria
->
addQuery
(
new
ScoreQuery
(
new
ContainsFilter
(
'tags.name', 'Doe'
)
, 100
)
)
;
$criteria
->
addQuery
(
new
ScoreQuery
(
new
ContainsFilter
(
'categories.name', 'Doe'
)
, 100
)
)
;
$result
=
$this
->
getContainer
(
)
->
get
(
'product.repository'
)
->
searchIds
(
$criteria
, Context::
createDefaultContext
(
)
)
;
static
::
assertEquals
(
100,
$result
->
getScore
(
$ids
->
get
(
'john'
)
)
)
;
static
::
assertEquals
(
200,
$result
->
getScore
(
$ids
->
get
(
'john.doe'
)
)
)
;
static
::
assertEquals
(
100,
$result
->
getScore
(
$ids
->
get
(
'doe'
)
)
)
;
}
public
function
testIdSearchResultHelpers
(
)
: void
{
$ids
=
new
IdsCollection
(
)
;
$this
->
getContainer
(
)
->
get
(
Connection::
class
)
->
executeStatement
(
'DELETE FROM product'
)
;
$products
=
[
(
if
(
$field
instanceof AssociationField
)
{
$queries
=
array_merge
(
$queries
,
$this
->
buildScoreQueries
(
$term
,
$field
->
getReferenceDefinition
(
)
,
$select
,
$context
,
$ranking
)
)
;
continue
;
}
$queries
[
]
=
new
ScoreQuery
(
new
EqualsFilter
(
$select
,
$term
->
getOriginal
(
)
->
getTerm
(
)
)
,
$ranking
*
$term
->
getOriginal
(
)
->
getScore
(
)
)
;
$queries
[
]
=
new
ScoreQuery
(
new
ContainsFilter
(
$select
,
$term
->
getOriginal
(
)
->
getTerm
(
)
)
,
$ranking
*
$term
->
getOriginal
(
)
->
getScore
(
)
* 0.5
)
;
if
(
$flag
&& !
$flag
->
tokenize
(
)
)
{
continue
;
}
\
assert
(
$primary
instanceof StorageAware
)
;
$select
= 'SUM(' .
implode
(
' + ',
$queries
->
getWheres
(
)
)
. ') / ' . \
sprintf
(
'COUNT(%s.%s)',
$definition
->
getEntityName
(
)
,
$primary
->
getStorageName
(
)
)
;
$query
->
addSelect
(
$select
. ' as _score'
)
;
// Sort by _score primarily if the criteria has a score query or search term
if
(
!
$this
->
hasScoreSorting
(
$criteria
)
)
{
$criteria
->
addSorting
(
new
FieldSorting
(
'_score', FieldSorting::DESCENDING
)
)
;
}
$minScore
=
array_map
(
fn
(
ScoreQuery
$query
)
=>
$query
->
getScore
(
)
,
$criteria
->
getQueries
(
)
)
;
\
assert
(
!
empty
(
$minScore
)
)
;
$minScore
=
min
(
$minScore
)
;
$query
->
andHaving
(
'_score >= :_minScore'
)
;
$query
->
setParameter
(
'_minScore',
$minScore
)
;
$query
->
addState
(
'_score'
)
;
foreach
(
$queries
->
getParameters
(
)
as
$key
=>
$value
)
{
$query
->
setParameter
(
$key
,
$value
,
$queries
->
getType
(
$key
)
)
;
}
}
$term
=
trim
(
$term
)
;
if
(
empty
(
$term
)
)
{
throw
RoutingException::
missingRequestParameter
(
'search'
)
;
}
$pattern
=
$this
->interpreter->
interpret
(
$term
,
$context
->
getContext
(
)
)
;
foreach
(
$pattern
->
getTerms
(
)
as
$searchTerm
)
{
$criteria
->
addQuery
(
new
ScoreQuery
(
new
EqualsFilter
(
'product.searchKeywords.keyword',
$searchTerm
->
getTerm
(
)
)
,
$searchTerm
->
getScore
(
)
,
'product.searchKeywords.ranking'
)
)
;
}
$criteria
->
addQuery
(
new
ScoreQuery
(
new
ContainsFilter
(
'product.searchKeywords.keyword',
$pattern
->
getOriginal
(
)
->
getTerm
(
)
)
,
$pattern
->
getOriginal
(
)
->
getScore
(
)
,
'product.searchKeywords.ranking'
)
)
;
foreach
(
$parsed
->
getWheres
(
)
as
$where
)
{
if
(
$query
->
getScoreField
(
)
)
{
$field
=
$this
->queryHelper->
getFieldAccessor
(
$query
->
getScoreField
(
)
,
$definition
,
$root
,
$context
)
;
$result
->
addWhere
(
sprintf
(
'IF(%s , %s * %s, 0)',
$where
,
(string)
$this
->connection->
quote
(
$query
->
getScore
(
)
)
,
$field
)
)
;
continue
;
}
$result
->
addWhere
(
sprintf
(
'IF(%s , %s, 0)',
$where
,
(string)
$this
->connection->
quote
(
$query
->
getScore
(
)
)
)
)
;
}
foreach
(
$parsed
->
getParameters
(
)
as
$key
=>
$parameter
)
{