Commit 29699120 authored by Katia Saurfelt's avatar Katia Saurfelt
Browse files

WIP search

parent 80f2ffcd8751
Pipeline #51817 failed with stage
in 3 minutes
......@@ -51,6 +51,7 @@ def compose_search(search, query=None, fields=(),
must=Q('multi_match',
query=query.split(char)[1],
type="phrase",
slope=50,
fields=fields))
must = []
must_not = []
......@@ -61,6 +62,8 @@ def compose_search(search, query=None, fields=(),
# proximity booster - phrase with slop=50
# TODO find a better way to do this
print("phrase", phrase)
print("common", common)
if not phrase and not common: # invalid combination
phrase = common = True
if phrase:
......@@ -68,11 +71,20 @@ def compose_search(search, query=None, fields=(),
query=query,
type="phrase",
slop=50,
fields=fields)
fields=fields,
boost=10)
should.append(phrase_query)
if fuzzy:
fuzzy_query = Q('multi_match',
query=query,
type="most_fields",
fields=fields,
fuzziness="auto:4,6")
should.append(fuzzy_query)
# highfrequency/lowfrequency query
# https://www.elastic.co/blog/stop-stopping-stop-words-a-look-at-common-terms-query
if common:
if False:
common_query = dsl_query.Common(alltext={"query": query,
"cutoff_frequency": cutoff_frequency,
"low_freq_operator": "and",
......@@ -83,21 +95,23 @@ def compose_search(search, query=None, fields=(),
elements = query.split()
elements_lowercase = [e.lower() for e in elements]
print("elements_lowercase", elements_lowercase)
if 'or' in elements_lowercase and len(elements) >= 3:
for element in query.split('or'):
should.append(Q('multi_match',
query=element.strip(),
fields=fields))
elements = []
# CAN NOT WORK
for element in elements:
if element.startswith('-'):
must_not.append(Q('multi_match',
query=element[1:],
fields=fields))
else:
if fuzzy:
should.append(dsl_query.Fuzzy(alltext=element))
# else:
# if fuzzy:
# should.append(Q('fuzzy', alltext={"value": element, "fuzziness": "auto:4,5"}))
bool_query = dsl_query.Bool(must=must,
must_not=must_not,
should=should,
......
......@@ -62,7 +62,7 @@ class CWFacetedSearch(FacetedSearch):
doc_types=None,
index=None,
form=None,
track_total_hits=True,
track_total_hits=True, # 10000,
**kwargs):
if index:
self.index = index
......@@ -100,7 +100,7 @@ class CWFacetedSearch(FacetedSearch):
Add custom highlighting
"""
search = search.highlight(*(f if '^' not in f else f.split('^', 1)[0] for f in self.fields))
return search.highlight_options(fragment_size=150, encoder='html')
return search.highlight_options(fragment_size=450, encoder='html')
class ElasticSearchView(StartupView):
......@@ -138,10 +138,11 @@ class ElasticSearchView(StartupView):
search = self.customize_search(query_string,
facet_selections,
start, stop)
import json
print(json.dumps(search._s.to_dict(), indent=2))
if 'debug-es' in self._cw.form:
import json
self.w(u'<pre>')
self.w(unicode(json.dumps(search._s.to_dict(), indent=2)))
self.w(json.dumps(search._s.to_dict(), indent=2))
self.w(u'</pre>')
self.w(u'<br/>')
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment