Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
elasticsearch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cubicweb
cubes
elasticsearch
Commits
70db85e13042
Commit
70db85e13042
authored
8 years ago
by
Arthur Lutz
Browse files
Options
Downloads
Patches
Plain Diff
[views] pagination & updated tests
parent
9360d782237b
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
__pkginfo__.py
+1
-0
1 addition, 0 deletions
__pkginfo__.py
test/test_elastic_search.py
+2
-2
2 additions, 2 deletions
test/test_elastic_search.py
views.py
+49
-7
49 additions, 7 deletions
views.py
with
52 additions
and
9 deletions
__pkginfo__.py
+
1
−
0
View file @
70db85e1
...
...
@@ -14,6 +14,7 @@
web
=
'
http://www.cubicweb.org/project/%s
'
%
distname
__depends__
=
{
'
cubicweb
'
:
'
>= 3.22.2
'
,
'
six
'
:
'
>= 1.4.0
'
,
'
cwtags
'
:
None
,
'
elasticsearch
'
:
None
,
'
elasticsearch-dsl
'
:
None
,
'
bs4
'
:
None
,
...
...
This diff is collapsed.
Click to expand it.
test/test_elastic_search.py
+
2
−
2
View file @
70db85e1
...
...
@@ -7,7 +7,7 @@
from
mock
import
patch
from
elasticsearch_dsl.
result
import
Response
from
elasticsearch_dsl.
faceted_search
import
Faceted
Response
from
cubicweb.devtools
import
testlib
from
cubicweb.cwconfig
import
CubicWebConfiguration
...
...
@@ -112,7 +112,7 @@
'
title
'
:
'
test
'
},
'
_type
'
:
'
BaseContent
'
,
'
_score
'
:
1
}
return
Response
({
'
hits
'
:
{
'
hits
'
:
[
result
],
return
Faceted
Response
({
'
hits
'
:
{
'
hits
'
:
[
result
],
'
total
'
:
1
}})
...
...
This diff is collapsed.
Click to expand it.
views.py
+
49
−
7
View file @
70db85e1
...
...
@@ -16,5 +16,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
elasticsearch search views
"""
from
elasticsearch
import
Elasticsearch
from
elasticsearch.exceptions
import
NotFoundError
...
...
@@ -20,8 +19,7 @@
from
elasticsearch.exceptions
import
NotFoundError
from
elasticsearch_dsl
import
Search
from
elasticsearch_dsl.connections
import
connections
from
elasticsearch_dsl
import
FacetedSearch
,
TermsFacet
,
DateHistogramFacet
from
bs4
import
BeautifulSoup
from
logilab.mtconverter
import
xml_escape
...
...
@@ -22,10 +20,11 @@
from
elasticsearch_dsl.connections
import
connections
from
elasticsearch_dsl
import
FacetedSearch
,
TermsFacet
,
DateHistogramFacet
from
bs4
import
BeautifulSoup
from
logilab.mtconverter
import
xml_escape
import
cwtags.tag
as
t
from
cubicweb.view
import
StartupView
from
cubes.elasticsearch.es
import
indexable_types
...
...
@@ -28,7 +27,8 @@
from
cubicweb.view
import
StartupView
from
cubes.elasticsearch.es
import
indexable_types
def
get_connection
(
config
):
try
:
connections
.
get_connection
()
...
...
@@ -40,4 +40,5 @@
index
=
index_name
,
timeout
=
20
)
class
CWFacetedSearch
(
FacetedSearch
):
...
...
@@ -43,3 +44,2 @@
class
CWFacetedSearch
(
FacetedSearch
):
#doc_types = ['NewsContent', 'CommemorationItem', 'Circular',]
# fields that should be searched
...
...
@@ -45,5 +45,5 @@
# fields that should be searched
fields
=
[
"
title^3
"
,
"
name^3
"
,
"
content^2
"
,
'
content
'
,
'
_all
'
,
]
fields
=
[
"
title^3
"
,
"
name^3
"
,
"
content^2
"
,
'
content
'
,
'
_all
'
]
facets
=
{
# use bucket aggregations to define facets
...
...
@@ -52,6 +52,7 @@
'
commemoration_year
'
:
DateHistogramFacet
(
field
=
'
commemoration_year
'
,
interval
=
'
year
'
),
'
year
'
:
DateHistogramFacet
(
field
=
'
year
'
,
interval
=
'
year
'
),
}
def
__init__
(
self
,
query
=
None
,
filters
=
{},
doc_types
=
None
):
if
doc_types
:
self
.
doc_types
=
doc_types
...
...
@@ -62,9 +63,10 @@
Add custom highlighting
"""
return
search
.
highlight
(
*
self
.
fields
)
\
.
highlight_options
(
pre_tags
=
""
,
post_tags
=
""
,
fragment_size
=
150
)
.
highlight_options
(
pre_tags
=
""
,
post_tags
=
""
,
fragment_size
=
150
)
class
ElasticSearchView
(
StartupView
):
__regid__
=
"
esearch
"
...
...
@@ -104,6 +106,7 @@
def
display_results
(
self
,
response
):
self
.
w
(
u
'
<div id=
"
main-center
"
class=
"
col-xs-10
"
role=
"
main
"
>
'
)
self
.
pagination
(
response
)
self
.
w
(
u
'
<ul>
'
)
for
result
in
response
:
self
.
w
(
u
'
<li>
'
)
...
...
@@ -125,5 +128,6 @@
pass
self
.
w
(
u
'
</li>
'
)
self
.
w
(
u
'
</ul>
'
)
self
.
pagination
(
response
)
self
.
w
(
u
'
</div>
'
)
...
...
@@ -128,5 +132,41 @@
self
.
w
(
u
'
</div>
'
)
def
pagination
(
self
,
response
):
if
response
.
hits
.
total
<
10
:
return
url_params
=
self
.
_cw
.
form
.
copy
()
with
t
.
ul
(
self
.
w
,
klass
=
"
pagination
"
)
as
ul
:
current_page
=
int
(
url_params
.
get
(
'
page
'
,
1
))
url_params
[
'
page
'
]
=
current_page
-
1
if
current_page
-
1
>=
1
:
ul
(
t
.
li
(
t
.
a
(
'
<<<
'
,
href
=
self
.
_cw
.
build_url
(
**
url_params
))))
else
:
ul
(
t
.
li
(
t
.
a
(
'
<<<
'
)))
for
i
in
range
(
0
,
min
((
response
.
hits
.
total
/
10
)
+
1
,
10
)):
page
=
i
+
1
url_params
[
'
page
'
]
=
page
url
=
self
.
_cw
.
build_url
(
**
url_params
)
if
page
==
current_page
:
ul
(
t
.
li
(
t
.
a
(
t
.
b
(
page
),
href
=
url
)))
else
:
ul
(
t
.
li
(
t
.
a
(
page
,
href
=
url
)))
if
response
.
hits
.
total
/
10
>
10
:
ul
(
t
.
li
(
t
.
a
(
"
...
"
)))
for
i
in
range
((
response
.
hits
.
total
/
10
)
-
3
,
response
.
hits
.
total
/
10
):
url_params
[
'
page
'
]
=
i
url
=
self
.
_cw
.
build_url
(
**
url_params
)
ul
(
t
.
li
(
t
.
a
(
i
,
href
=
url
)))
url_params
[
'
page
'
]
=
current_page
+
1
if
current_page
+
1
>=
(
response
.
hits
.
total
/
10
):
ul
(
t
.
li
(
t
.
a
(
'
>>>
'
)))
else
:
ul
(
t
.
li
(
t
.
a
(
'
>>>
'
,
href
=
self
.
_cw
.
build_url
(
**
url_params
))))
def
display_facets
(
self
,
response
):
self
.
w
(
u
'''
<aside id=
"
aside-main-left
"
class=
"
col-xs-2 cwjs-aside
"
>
...
...
@@ -137,6 +177,8 @@
'''
)
for
attribute
in
(
'
cw_etype
'
,
'
creation_date
'
):
url_params
=
self
.
_cw
.
form
.
copy
()
if
'
page
'
in
url_params
:
del
url_params
[
'
page
'
]
self
.
w
(
u
'
<div class=
"
facetBody vocabularyFacet
"
>
'
)
self
.
w
(
u
'
<div class=
"
facetTitle
"
>{}</div>
'
.
format
(
attribute
))
for
(
tag
,
count
,
selected
)
in
response
.
facets
[
attribute
]:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment