@@ -21,15 +21,59 @@ public function __construct(
2121
2222
2323 public function renderDefault ($ queryString ): void
24+ {
25+ $ query = $ this ->buildQuery ($ queryString );
26+
27+ try {
28+ $ products = $ this ->productService ->getAllBy ($ query );
29+
30+ } catch (\Spameri \Elastic \Exception \ElasticSearchException $ exception ) {
31+ $ products = [];
32+ }
33+
34+ $ this ->getTemplate ()->add (
35+ 'products ' ,
36+ $ products
37+ );
38+ $ this ->getTemplate ()->add (
39+ 'queryString ' ,
40+ $ queryString
41+ );
42+ }
43+
44+
45+ public function createComponentSearchForm () :\Nette \Application \UI \Form
46+ {
47+ $ form = new \Nette \Application \UI \Form ();
48+ $ form ->addText ('queryString ' , 'query ' )
49+ ->setAttribute ('class ' , 'inp-text suggest ' );
50+
51+ $ form ->addSubmit ('search ' , 'Search ' );
52+
53+ $ form ->onSuccess [] = function () use ($ form ) {
54+ $ this ->redirect (
55+ 301 ,
56+ ':Product:SimpleProductList:default ' ,
57+ [
58+ 'queryString ' => $ form ->getValues ()->queryString ,
59+ ]
60+ );
61+ };
62+
63+ return $ form ;
64+ }
65+
66+
67+ public function buildQuery (string $ queryString ): \Spameri \ElasticQuery \ElasticQuery
2468 {
2569 $ query = new \Spameri \ElasticQuery \ElasticQuery ();
2670 $ subQuery = new \Spameri \ElasticQuery \Query \QueryCollection ();
2771 $ subQuery ->addShouldQuery (
2872 new \Spameri \ElasticQuery \Query \Match (
2973 'name ' ,
3074 $ queryString ,
31- 10 ,
32- \Spameri \ElasticQuery \Query \Match \Operator::OR ,
75+ 3 ,
76+ \Spameri \ElasticQuery \Query \Match \Operator:: OR ,
3377 new \Spameri \ElasticQuery \Query \Match \Fuzziness (\Spameri \ElasticQuery \Query \Match \Fuzziness::AUTO )
3478 )
3579 );
@@ -52,7 +96,7 @@ public function renderDefault($queryString): void
5296 'content ' ,
5397 $ queryString ,
5498 1 ,
55- \Spameri \ElasticQuery \Query \Match \Operator::OR ,
99+ \Spameri \ElasticQuery \Query \Match \Operator:: OR ,
56100 new \Spameri \ElasticQuery \Query \Match \Fuzziness (\Spameri \ElasticQuery \Query \Match \Fuzziness::AUTO )
57101 )
58102 );
@@ -61,48 +105,13 @@ public function renderDefault($queryString): void
61105 $ query ->addShouldQuery (
62106 new \Spameri \ElasticQuery \Query \Match (
63107 'availability ' ,
64- 'Skladem '
108+ 'Skladem ' ,
109+ 10
65110 )
66111 );
67112 $ query ->options ()->changeSize (20 );
68113
69- try {
70- $ products = $ this ->productService ->getAllBy ($ query );
71-
72- } catch (\Spameri \Elastic \Exception \ElasticSearchException $ exception ) {
73- $ products = [];
74- }
75-
76- $ this ->getTemplate ()->add (
77- 'products ' ,
78- $ products
79- );
80- $ this ->getTemplate ()->add (
81- 'queryString ' ,
82- $ queryString
83- );
84- }
85-
86-
87- public function createComponentSearchForm () :\Nette \Application \UI \Form
88- {
89- $ form = new \Nette \Application \UI \Form ();
90- $ form ->addText ('queryString ' , 'query ' )
91- ->setAttribute ('class ' , 'inp-text suggest ' );
92-
93- $ form ->addSubmit ('search ' , 'Search ' );
94-
95- $ form ->onSuccess [] = function () use ($ form ) {
96- $ this ->redirect (
97- 301 ,
98- ':Product:SimpleProductList:default ' ,
99- [
100- 'queryString ' => $ form ->getValues ()->queryString ,
101- ]
102- );
103- };
104-
105- return $ form ;
114+ return $ query ;
106115 }
107116
108117}
0 commit comments