Skip to content
Snippets Groups Projects
Commit b31c7fa7d65e authored by Adrien Di Mascio's avatar Adrien Di Mascio
Browse files

extract ES actual search from View.call to ease overriding

NOTE: I think it would be best if the elasticsearch didn't generate
any HTML at all.
parent 51da60b64969
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment