@@ -64,11 +64,11 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
6464
6565 $ form ['#method ' ] = 'GET ' ;
6666
67- if (isset ($ _GET ['term ' ]) && !empty ($ _GET ['term ' ])) {
68- $ form ['wrapper ' ]['results ' ] = [
69- '#markup ' => tripal_elasticsearch_advanced_search_results ($ _GET ),
70- ];
71- }
67+ // if (isset($_GET['term']) && !empty($_GET['term'])) {
68+ $ form ['wrapper ' ]['results ' ] = [
69+ '#markup ' => tripal_elasticsearch_advanced_search_results ($ _GET ),
70+ ];
71+ // }
7272
7373 return $ form ;
7474}
@@ -79,26 +79,18 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
7979 * @return string
8080 */
8181function tripal_elasticsearch_advanced_search_results (array $ values ) {
82- $ fields = ['content.* ' , 'title ' ];
83-
84- if (isset ($ _GET ['field ' ]) && !empty ($ _GET ['field ' ])) {
85- $ fields = ["content. {$ _GET ['field ' ]}" ];
82+ if (empty ($ values ['category ' ]) && empty ($ values ['term ' ])) {
83+ return '' ;
8684 }
8785
8886 try {
8987 $ instance = new \ES \Common \Instance ();
90- $ model = new \ES \Models \Model ();
91- $ model ->setIndexName ('entities ' );
92- $ model ->where ($ fields , trim ($ values ['term ' ]));
93-
94- $ category = trim ($ values ['category ' ] ?? '' );
95- if (!empty ($ category )) {
96- $ model ->where ('bundle_label ' , "\"$ category \"" );
88+ $ results = tripal_elasticsearch_perform_advanced_search ($ values , 15 );
89+ if ($ results ['total ' ] == 0 && !empty ($ values ['term ' ])) {
90+ $ values ['term ' ] = strtolower ($ values ['term ' ]);
91+ $ results = tripal_elasticsearch_perform_advanced_search ($ values , 15 );
9792 }
98-
99- $ model ->highlight (['content.* ' , 'title ' ]);
100- $ data = $ model ->search ();
101- $ hits = $ instance ->formatHits ($ data );
93+ $ hits = $ instance ->formatHits ($ results ['results ' ]);
10294
10395 if (count ($ hits ) === 0 ) {
10496 return theme (
@@ -109,7 +101,16 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
109101 );
110102 }
111103
112- return tripal_elasticsearch_get_website_search_result_table ($ hits );
104+ $ content = theme ('elasticsearch_results_header ' , [
105+ 'page ' => $ results ['page ' ],
106+ 'total ' => $ results ['total ' ],
107+ 'pages ' => $ results ['pages ' ],
108+ 'time ' => $ results ['time ' ]
109+ ]);
110+ $ content .= tripal_elasticsearch_get_website_search_result_table ($ hits , false );
111+ $ content .= $ results ['pager ' ];
112+
113+ return $ content ;
113114 } catch (Exception $ exception ) {
114115 watchdog (
115116 'tripal_elasticsearch ' ,
@@ -123,6 +124,40 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
123124 }
124125}
125126
127+ /**
128+ * @param $values
129+ *
130+ * @return array
131+ * @throws \Exception
132+ */
133+ function tripal_elasticsearch_perform_advanced_search ($ values , $ per_page = NULL ) {
134+ $ fields = ['content.* ' , 'title ' ];
135+
136+ if (isset ($ values ['field ' ]) && !empty ($ values ['field ' ])) {
137+ $ fields = ["content. {$ _GET ['field ' ]}" ];
138+ }
139+
140+ $ model = new \ES \Models \Model ();
141+ $ model ->setIndexName ('entities ' );
142+
143+ if (!empty ($ values ['term ' ])) {
144+ $ model ->where ($ fields , trim ($ values ['term ' ]));
145+ }
146+
147+ $ category = trim ($ values ['category ' ] ?? '' );
148+ if (!empty ($ category )) {
149+ $ model ->where ('bundle_label ' , "\"$ category \"" );
150+ }
151+
152+ $ model ->highlight (['content.* ' , 'title ' ]);
153+
154+ if (is_null ($ per_page )) {
155+ return $ model ->search ();
156+ }
157+
158+ return $ model ->paginate ($ per_page );
159+ }
160+
126161/**
127162 * AJAX Callback.
128163 *
0 commit comments