Skip to content

Commit b8e34f9

Browse files
johannessteudaniellienert
authored andcommitted
TASK: Add Transformer documentation (#82)
1 parent c857237 commit b8e34f9

File tree

2 files changed

+142
-7
lines changed

2 files changed

+142
-7
lines changed

Documentation/Transformer.rst

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
===========
2+
Transformer
3+
===========
4+
5+
The Transformer classes allows you to transform your data before it's stored in elasticsearch.
6+
7+
Transformer annotations
8+
=======================
9+
This package ships several Transformer by default:
10+
11+
* CollectionStringCastTransformer:
12+
13+
* Iterates over an Collection/Array and casts all items to a string and returns an array with strings
14+
15+
* DateTransformer
16+
17+
* Converts a DateTime-Object to formatted string. Defaults to Y-m-d
18+
19+
* ObectIdentifierTransformer
20+
21+
* Converts an object to it's persistence identifier
22+
23+
* StringCastTransformer
24+
25+
* Converts a value to a string
26+
27+
* TextCastTransformer
28+
29+
* Converts a value to a string
30+
31+
32+
Usage
33+
=====
34+
35+
To transform an objects property at index time you need to annotate your value like this:
36+
37+
*Example: Use a Transformer* ::
38+
39+
/**
40+
* @var \DateTime
41+
* @ElasticSearch\Transform(options={"format" = "Y-m-d H:m:s"}, type="date")
42+
*/
43+
protected $date;
44+
45+
46+
The `type` option is usded to determine the corresponding transformer class. Date would resolve to `Flowpack\ElasticSearch\Indexer\Object\Transform\DateTransformer`.
47+
and call it's implementation of `transformByAnnotation($source, TransformAnnotation $annotation)`.
48+
All default transformer can be used just like this.
49+
50+
51+
Implement a custom Transformer
52+
==============================
53+
If you need a custom transformer you need to implement the TrnasformerInterface.
54+
It declares two methods:
55+
56+
* getTargetMappingType: return value is used as mapping type in elasticsearch (one of https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html)
57+
58+
* transformByAnnotation: actual implementation of your transform process
59+
60+
61+
*Example: Custom Transformer that implements TransformerInterface and crops the source length* ::
62+
63+
<?php
64+
namespace Some\Vendor\Indexer\Object\Transform;
65+
66+
use Flowpack\ElasticSearch\Annotations\Transform as TransformAnnotation;
67+
use Neos\Flow\Annotations as Flow;
68+
69+
/**
70+
* @Flow\Scope("singleton")
71+
*/
72+
class CropTransformer implements TransformerInterface
73+
{
74+
75+
/**
76+
* Returns the Elasticsearch type that is used as mappping type
77+
*
78+
* @return string
79+
*/
80+
public function getTargetMappingType() {
81+
return 'text'
82+
}
83+
84+
/**
85+
* This is actually callled to transform the $source value
86+
*
87+
* @param mixed $source
88+
* @param TransformAnnotation $annotation
89+
* @return mixed
90+
*/
91+
public function transformByAnnotation($source, TransformAnnotation $annotation) {
92+
93+
if ($source === null) {
94+
return '';
95+
}
96+
97+
if ($annotation->options['length']) {
98+
return substr((string) $source, 0, $annotation->options['length']);
99+
}
100+
101+
return (string) $source
102+
}
103+
}
104+
105+
106+
107+
*Example: Annotation usage:* ::
108+
109+
/**
110+
* @Flow\Entity
111+
* @ElasticSearch\Indexable("twitter", typeName="tweet")
112+
*/
113+
class Tweet {
114+
115+
/**
116+
* @var string
117+
*/
118+
protected $username;
119+
120+
/**
121+
* @var string
122+
* @ElasticSearch\Transform(options={"length" = 20}, type="Some\Vendor\Indexer\Object\Transform\CropTransformer")
123+
*/
124+
protected $message;
125+
126+
/**
127+
* @var \DateTime
128+
*/
129+
protected $date;
130+
}
131+
132+
133+
With this configuration the message will always be cropped to 20 chars when it's indexed
134+

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# Flow Framework ElasticSearch Integration
22

3-
*Supporting Elasticsearch versions 5.x and 6.x*
3+
_Supporting Elasticsearch versions 5.x and 6.x_
44

55
This project connects the Flow Framework to Elasticsearch; enabling two main functionalities:
66

7-
* Full-Text Indexing of Doctrine Entities
8-
* Index configuration based on Annotations
7+
- Full-Text Indexing of Doctrine Entities
8+
- Index configuration based on Annotations
99

1010
Related package:
1111

12-
* [Flowpack.ElasticSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.ElasticSearch.ContentRepositoryAdaptor): An adapter to support the Neos Content Repository
13-
indexing and searching
12+
- [Flowpack.ElasticSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.ElasticSearch.ContentRepositoryAdaptor): An adapter to support the Neos Content Repository
13+
indexing and searching
1414

1515
More documentation:
1616

17-
* [General Documentation](Documentation/Index.rst)
18-
* [How to index your own model ?](Documentation/Indexer.rst)
17+
- [General Documentation](Documentation/Index.rst)
18+
- [How to index your own model ?](Documentation/Indexer.rst)
19+
- [Add custom Transformer](Documentation/Indexer.rst)

0 commit comments

Comments
 (0)