Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
Elasticsearch DSL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Public
Elasticsearch DSL
Commits
b6cf5eb6
Commit
b6cf5eb6
authored
9 years ago
by
Simonas Šerlinskas
Browse files
Options
Downloads
Patches
Plain Diff
simplifying highlight functionality
parent
c0ec9f99
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Highlight/Field.php
+0
-211
0 additions, 211 deletions
src/Highlight/Field.php
src/Highlight/Highlight.php
+26
-163
26 additions, 163 deletions
src/Highlight/Highlight.php
with
26 additions
and
374 deletions
src/Highlight/Field.php
deleted
100644 → 0
+
0
−
211
View file @
c0ec9f99
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
ONGR\ElasticsearchDSL\Highlight
;
use
ONGR\ElasticsearchDSL\BuilderInterface
;
use
ONGR\ElasticsearchDSL\NamedBuilderInterface
;
/**
* This class holds data for highlighting field.
*/
class
Field
implements
NamedBuilderInterface
{
const
TYPE_PLAIN
=
'plain'
;
const
TYPE_POSTINGS
=
'postings'
;
const
TYPE_FVH
=
'fvh'
;
/**
* @var string Field name.
*/
private
$name
;
/**
* @var string Highlighter type. By default 'plain'.
*/
private
$type
;
/**
* @var int Size of the highlighted fragment in characters. By default 100.
*/
private
$fragmentSize
;
/**
* @var int Maximum number of fragments to return. By default 5.
*/
private
$numberOfFragments
;
/**
* @var array Combine matches on multiple fields to highlight a single field.
*/
private
$matchedFields
;
/**
* @var array BuilderInterface query to highlight.
*/
private
$highlightQuery
;
/**
* @var int Show part of string even if there are no matches to highlight. Defaults to 0.
*/
private
$noMatchSize
;
/**
* @var bool Highlight fields based on the source.
*/
private
$forceSource
;
/**
* Creates a highlight for a field.
*
* @param string $name Field name.
*/
public
function
__construct
(
$name
)
{
$this
->
name
=
$name
;
$this
->
setMatchedFields
([
$name
]);
}
/**
* {@inheritdoc}
*/
public
function
getName
()
{
return
$this
->
name
;
}
/**
* Sets highlighter type (forces). Available options 'plain', 'postings', 'fvh'.
*
* @param string $type
*
* @return Field
*/
public
function
setHighlighterType
(
$type
)
{
$reflection
=
new
\ReflectionClass
(
__CLASS__
);
if
(
in_array
(
$type
,
$reflection
->
getConstants
()))
{
$this
->
type
=
$type
;
}
return
$this
;
}
/**
* Sets field fragment size.
*
* @param int $fragmentSize
*
* @return Field
*/
public
function
setFragmentSize
(
$fragmentSize
)
{
$this
->
fragmentSize
=
$fragmentSize
;
return
$this
;
}
/**
* Sets maximum number of fragments to return.
*
* @param int $numberOfFragments
*
* @return Field
*/
public
function
setNumberOfFragments
(
$numberOfFragments
)
{
$this
->
numberOfFragments
=
$numberOfFragments
;
return
$this
;
}
/**
* Set fields to match.
*
* @param array $matchedFields
*
* @return Field
*/
public
function
setMatchedFields
(
$matchedFields
)
{
$this
->
matchedFields
=
$matchedFields
;
return
$this
;
}
/**
* Set query to highlight.
*
* @param BuilderInterface $query
*
* @return Field
*/
public
function
setHighlightQuery
(
BuilderInterface
$query
)
{
$this
->
highlightQuery
=
[
$query
->
getType
()
=>
$query
->
toArray
()];
return
$this
;
}
/**
* Shows set length of a string even if no matches found.
*
* @param int $noMatchSize
*
* @return Field
*/
public
function
setNoMatchSize
(
$noMatchSize
)
{
$this
->
noMatchSize
=
$noMatchSize
;
return
$this
;
}
/**
* Set to force highlighting from source.
*
* @param bool $forceSource
*
* @return Field
*/
public
function
setForceSource
(
$forceSource
)
{
$this
->
forceSource
=
$forceSource
;
return
$this
;
}
/**
* {@inheritdoc}
*/
public
function
toArray
()
{
return
array_filter
(
[
'fragment_size'
=>
$this
->
fragmentSize
,
'number_of_fragments'
=>
$this
->
numberOfFragments
,
'type'
=>
$this
->
type
,
'matched_fields'
=>
$this
->
matchedFields
,
'highlight_query'
=>
$this
->
highlightQuery
,
'no_match_size'
=>
$this
->
noMatchSize
,
'force_source'
=>
$this
->
forceSource
,
]
);
}
/**
* {@inheritdoc}
*/
public
function
getType
()
{
return
$this
->
type
;
}
}
This diff is collapsed.
Click to expand it.
src/Highlight/Highlight.php
+
26
−
163
View file @
b6cf5eb6
...
@@ -12,68 +12,34 @@
...
@@ -12,68 +12,34 @@
namespace
ONGR\ElasticsearchDSL\Highlight
;
namespace
ONGR\ElasticsearchDSL\Highlight
;
use
ONGR\ElasticsearchDSL\BuilderInterface
;
use
ONGR\ElasticsearchDSL\BuilderInterface
;
use
ONGR\ElasticsearchDSL\NamedBuilderBag
;
use
ONGR\ElasticsearchDSL\ParametersTrait
;
use
ONGR\ElasticsearchDSL\NamedBuilderInterface
;
/**
/**
* Data holder for highlight api.
* Data holder for highlight api.
*/
*/
class
Highlight
extends
NamedBuilderBag
implements
BuilderInterface
class
Highlight
implements
BuilderInterface
{
{
const
TYPE_PLAIN
=
'plain'
;
use
ParametersTrait
;
const
TYPE_POSTINGS
=
'postings'
;
const
TYPE_FVH
=
'fvh'
;
/**
/**
* @var array Holds
html tag name and class that highlight will be put in (default 'em' tag)
.
* @var array Holds
fields for highlight
.
*/
*/
private
$
tag
s
=
[];
private
$
field
s
=
[];
/**
/**
* @var
string Holds tag schema name. 'styled' is the only option yet.
* @var
array
*/
*/
private
$tags
Schema
=
null
;
private
$tags
;
/**
/**
* @var string Fragments sort type.
* @param $name
*/
* @param array $params
private
$order
=
null
;
/**
* @var string Highlighter type. By default plain.
*/
private
$type
=
null
;
/**
* @var int Size of the highlighted fragment in characters. By default 100.
*/
private
$fragmentSize
=
null
;
/**
* @var int Maximum number of fragments to return. By default 5.
*/
private
$numberOfFragments
=
null
;
/**
* {@inheritdoc}
*
* @return Highlight
*/
public
function
add
(
NamedBuilderInterface
$builder
)
{
parent
::
add
(
$builder
);
return
$this
;
}
/**
* {@inheritdoc}
*
*
* @return
Highlight
* @return
$this
*/
*/
public
function
set
(
array
$builders
)
public
function
addField
(
$name
,
array
$params
=
[]
)
{
{
parent
::
set
(
$builders
)
;
$this
->
fields
[
$name
]
=
$params
;
return
$this
;
return
$this
;
}
}
...
@@ -81,92 +47,15 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
...
@@ -81,92 +47,15 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
/**
/**
* Sets html tag and its class used in highlighting.
* Sets html tag and its class used in highlighting.
*
*
* @param
string $t
ag
* @param
array $preT
ag
s
* @param
string $clas
s
* @param
array $postTag
s
*
*
* @return
Highlight
* @return
$this
*/
*/
public
function
setTag
(
$t
ag
,
$class
=
null
)
public
function
setTag
s
(
array
$preT
ag
s
,
array
$postTags
)
{
{
$this
->
tags
[]
=
array_filter
(
$this
->
tags
[
'pre_tags'
]
=
$preTags
;
[
$this
->
tags
[
'post_tags'
]
=
$postTags
;
'tag'
=>
$tag
,
'class'
=>
$class
,
]
);
return
$this
;
}
/**
* Sets html tag and its class used in highlighting.
*
* @param string $tagsSchema
*
* @return Highlight
*/
public
function
setTagsSchema
(
$tagsSchema
)
{
$this
->
tagsSchema
=
$tagsSchema
;
return
$this
;
}
/**
* Sets fragments sort order.
*
* @param string $order
*
* @return Highlight
*/
public
function
setOrder
(
$order
)
{
$this
->
order
=
$order
;
return
$this
;
}
/**
* Sets highlighter type (forces). Available options plain, postings, fvh.
*
* @param string $type
*
* @return Highlight
*/
public
function
setHighlighterType
(
$type
)
{
$reflection
=
new
\ReflectionClass
(
__CLASS__
);
if
(
in_array
(
$type
,
$reflection
->
getConstants
()))
{
$this
->
type
=
$type
;
}
return
$this
;
}
/**
* Sets field fragment size.
*
* @param int $fragmentSize
*
* @return Highlight
*/
public
function
setFragmentSize
(
$fragmentSize
)
{
$this
->
fragmentSize
=
$fragmentSize
;
return
$this
;
}
/**
* Sets maximum number of fragments to return.
*
* @param int $numberOfFragments
*
* @return Highlight
*/
public
function
setNumberOfFragments
(
$numberOfFragments
)
{
$this
->
numberOfFragments
=
$numberOfFragments
;
return
$this
;
return
$this
;
}
}
...
@@ -184,44 +73,18 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
...
@@ -184,44 +73,18 @@ class Highlight extends NamedBuilderBag implements BuilderInterface
*/
*/
public
function
toArray
()
public
function
toArray
()
{
{
$highlight
=
array_filter
(
$output
=
[];
[
'order'
=>
$this
->
order
,
if
(
is_array
(
$this
->
tags
))
{
'type'
=>
$this
->
type
,
$output
=
$this
->
tags
;
'fragment_size'
=>
$this
->
fragmentSize
,
'number_of_fragments'
=>
$this
->
numberOfFragments
,
'tags_schema'
=>
$this
->
tagsSchema
,
'fields'
=>
$this
->
getFields
(),
]
);
foreach
(
$this
->
tags
as
$tag
)
{
if
(
isset
(
$tag
[
'tag'
]))
{
$highlight
[
'post_tags'
][]
=
sprintf
(
'</%s>'
,
$tag
[
'tag'
]);
if
(
isset
(
$tag
[
'class'
]))
{
$highlight
[
'pre_tags'
][]
=
sprintf
(
'<%s class="%s">'
,
$tag
[
'tag'
],
$tag
[
'class'
]);
}
else
{
$highlight
[
'pre_tags'
][]
=
sprintf
(
'<%s>'
,
$tag
[
'tag'
]);
}
}
}
}
return
$highlight
;
$output
=
$this
->
processArray
(
$output
);
}
/**
foreach
(
$this
->
fields
as
$field
=>
$params
)
{
* Returns fields as array.
$output
[
'fields'
][
$field
]
=
count
(
$params
)
?
$params
:
new
\stdClass
();
*
* @return array
*/
private
function
getFields
()
{
$out
=
[];
foreach
(
$this
->
all
()
as
$builder
)
{
$out
=
array_merge
(
$out
,
[
$builder
->
getName
()
=>
$builder
->
toArray
()]);
}
}
return
$out
;
return
$out
put
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment