1313
1414use Doctrine \Common \Annotations \AnnotationReader ;
1515use Doctrine \Common \Cache \Cache ;
16+ use ONGR \App \Document \DummyDocument ;
17+ use ONGR \App \Document \TestDocument ;
1618use ONGR \App \Entity \DummyDocumentInTheEntityDirectory ;
1719use ONGR \ElasticsearchBundle \Mapping \DocumentParser ;
1820use PHPUnit \Framework \TestCase ;
21+ use Symfony \Component \Yaml \Yaml ;
1922
2023class DocumentParserTest extends TestCase
2124{
2225 public function testDocumentParsing ()
2326 {
2427
2528 $ parser = new DocumentParser (new AnnotationReader (), $ this ->createMock (Cache::class));
26- ;
2729
2830 $ indexMetadata = $ parser ->getIndexMetadata (new \ReflectionClass (DummyDocumentInTheEntityDirectory::class));
2931
@@ -41,4 +43,98 @@ public function testDocumentParsing()
4143
4244 $ this ->assertEquals ($ expected , $ indexMetadata );
4345 }
46+
47+ public function testParsingWithMultiFieldsMapping ()
48+ {
49+ $ parser = new DocumentParser (new AnnotationReader (), $ this ->createMock (Cache::class));
50+
51+ $ indexMetadata = $ parser ->getIndexMetadata (new \ReflectionClass (TestDocument::class));
52+
53+ // Mapping definition for field "title" should be there
54+ $ this ->assertNotEmpty ($ indexMetadata ['mappings ' ]['_doc ' ]['properties ' ]['title ' ]);
55+ $ title_field_def = $ indexMetadata ['mappings ' ]['_doc ' ]['properties ' ]['title ' ];
56+
57+ // title should have `fields` sub-array
58+ $ this ->assertArrayHasKey ('fields ' , $ title_field_def );
59+
60+ // `fields` should look like so:
61+ $ expected = [
62+ 'raw ' => ['type ' => 'keyword ' ],
63+ 'increment ' => ['type ' => 'text ' , 'analyzer ' => 'incrementalAnalyzer ' ],
64+ 'sorting ' => ['type ' => 'keyword ' , 'normalizer ' => 'lowercase_normalizer ' ]
65+ ];
66+
67+ $ this ->assertEquals ($ expected , $ title_field_def ['fields ' ]);
68+ }
69+
70+ public function testGetAnalysisConfig ()
71+ {
72+ // Global analysis settings used for this test, usually set in the bundle configuration
73+ // sets custom analyzer, filter, and normalizer
74+ $ config_analysis = [
75+ 'analyzer ' => [
76+ 'incrementalAnalyzer ' => [
77+ 'type ' => 'custom ' ,
78+ 'tokenizer ' => 'standard ' ,
79+ 'filter ' => [
80+ 0 => 'lowercase ' ,
81+ 1 => 'edge_ngram_filter ' ,
82+ ],
83+ ],
84+ 'unusedAnalyzer ' => [
85+ 'type ' => 'custom ' ,
86+ 'tokenizer ' => 'standard '
87+ ]
88+ ],
89+ 'filter ' => [
90+ 'edge_ngram_filter ' => [
91+ 'type ' => 'edge_ngram ' ,
92+ 'min_gram ' => 1 ,
93+ 'max_gram ' => 20 ,
94+ ],
95+ ],
96+ 'normalizer ' => [
97+ 'lowercase_normalizer ' => [
98+ 'type ' => 'custom ' ,
99+ 'filter ' => ['lowercase ' ]
100+ ],
101+ 'unused_normalizer ' => [
102+ 'type ' => 'custom '
103+ ]
104+ ]
105+ ];
106+
107+ $ parser = new DocumentParser (new AnnotationReader (), $ this ->createMock (Cache::class), $ config_analysis );
108+ $ analysis = $ parser ->getAnalysisConfig (new \ReflectionClass (TestDocument::class));
109+
110+ $ expected = [
111+ 'analyzer ' => [
112+ 'incrementalAnalyzer ' => [
113+ 'type ' => 'custom ' ,
114+ 'tokenizer ' => 'standard ' ,
115+ 'filter ' => [
116+ 0 => 'lowercase ' ,
117+ 1 => 'edge_ngram_filter ' ,
118+ ],
119+ ],
120+ ],
121+ // 'unusedAnalyzer' must not be there because it is not used
122+ 'filter ' => [
123+ 'edge_ngram_filter ' => [
124+ 'type ' => 'edge_ngram ' ,
125+ 'min_gram ' => 1 ,
126+ 'max_gram ' => 20 ,
127+ ],
128+ ],
129+ 'normalizer ' => [
130+ 'lowercase_normalizer ' => [
131+ 'type ' => 'custom ' ,
132+ 'filter ' => ['lowercase ' ]
133+ ]
134+ // 'unused_normalizer' must not be there
135+ ]
136+ ];
137+
138+ $ this ->assertEquals ($ expected , $ analysis );
139+ }
44140}
0 commit comments