diff --git a/views.py b/views.py index 51da60b6496983edfba73c58d18555d81b7b5982_dmlld3MucHk=..b31c7fa7d65efccd944513d7ebc9c77e7b2c203f_dmlld3MucHk= 100644 --- a/views.py +++ b/views.py @@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. """elasticsearch search views""" +from six import text_type as unicode + from elasticsearch.exceptions import NotFoundError from elasticsearch_dsl import FacetedSearch, TermsFacet, DateHistogramFacet from bs4 import BeautifulSoup @@ -25,7 +27,7 @@ import cwtags.tag as t from cubicweb.view import StartupView -from cubes.elasticsearch.es import indexable_types +from cubes.elasticsearch.es import indexable_types, get_connection from cubes.elasticsearch.search_helpers import compose_search @@ -83,12 +85,8 @@ if search_comp: search_comp.render(w=self.w) - def call(self, **kwargs): - # TODO if no ES configuration, redirect or display warning - self.render_search_comp() - self.w(u'<h1>%s</h1>' % self._cw._('Recherche')) - query_string = xml_escape(self._cw.form.get('search', '')) - self.w(u'<h2>Resultats pour : <em>%s</em></h2>' % query_string) + def do_search(self, query_string): + get_connection(self._cw.vreg.config) facet_selections = {} start, stop = 0, 10 parents_for = children_for = None @@ -112,7 +110,7 @@ self.w(unicode(json.dumps(search._s.to_dict()))) self.w(u'<br/>') try: - response = search.execute() + return search.execute() except NotFoundError: self.w(u'index not found in elasticsearch') return @@ -116,9 +114,19 @@ except NotFoundError: self.w(u'index not found in elasticsearch') return - self.w(u'Resultats: %s' % response.hits.total) - if hasattr(response, 'facets'): - self.display_facets(response) + + def call(self, **kwargs): + # TODO if no ES configuration, redirect or display warning + self.render_search_comp() + query_string = self._cw.form.get('search', '') + self.w(u'<h1>%s</h1>' % self._cw._('Recherche')) + response = self.do_search(query_string) + if response.hits.total: + self.w(u'<h2>Resultats pour : <em>%s</em></h2>' % + xml_escape(query_string)) + self.w(u'Resultats: %s' % response.hits.total) + if hasattr(response, 'facets'): + self.display_facets(response) self.display_results(response) def customize_search(self, query_string, facet_selections,