Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubes
elasticsearch
Commits
a703bafa3e54
Commit
29699120
authored
Dec 14, 2020
by
Katia Saurfelt
Browse files
WIP search
parent
80f2ffcd8751
Pipeline
#51817
failed with stage
in 3 minutes
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
cubicweb_elasticsearch/search_helpers.py
View file @
a703bafa
...
...
@@ -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
.
F
uzzy
(
alltext
=
element
))
#
else:
#
if fuzzy:
#
should.append(
Q('f
uzzy
',
alltext=
{"value": element, "fuzziness": "auto:4,5"}
))
bool_query
=
dsl_query
.
Bool
(
must
=
must
,
must_not
=
must_not
,
should
=
should
,
...
...
cubicweb_elasticsearch/views.py
View file @
a703bafa
...
...
@@ -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
=
1
50
,
encoder
=
'html'
)
return
search
.
highlight_options
(
fragment_size
=
4
50
,
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
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment