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
aea1ea565d4e
Commit
aea1ea565d4e
authored
8 years ago
by
Arthur Lutz
Browse files
Options
Downloads
Patches
Plain Diff
skip indexing when locations and index-name are not configured
parent
21b992410538
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
ccplugin.py
+32
-29
32 additions, 29 deletions
ccplugin.py
hooks.py
+11
-10
11 additions, 10 deletions
hooks.py
site_cubicweb.py
+6
-4
6 additions, 4 deletions
site_cubicweb.py
test/test_elastic_search.py
+3
-1
3 additions, 1 deletion
test/test_elastic_search.py
with
52 additions
and
44 deletions
ccplugin.py
+
32
−
29
View file @
aea1ea56
...
...
@@ -45,11 +45,12 @@
config
=
cnx
.
vreg
.
config
schema
=
cnx
.
vreg
.
schema
locations
=
config
[
'
elasticsearch-locations
'
]
index_name
=
config
.
get
(
'
index-name
'
)
or
'
cubicweb
'
es
=
Elasticsearch
(
locations
and
locations
.
split
(
'
,
'
)
or
None
)
if
not
self
.
config
.
dry_run
:
# ignore 400 caused by IndexAlreadyExistsException when creating an
# index
es
.
indices
.
create
(
index
=
index_name
,
body
=
index_settings
(),
ignore
=
400
)
index_name
=
config
[
'
index-name
'
]
if
locations
and
index_name
:
es
=
Elasticsearch
(
locations
)
if
not
self
.
config
.
dry_run
:
# ignore 400 caused by IndexAlreadyExistsException when creating an
# index
es
.
indices
.
create
(
index
=
index_name
,
body
=
index_settings
(),
ignore
=
400
)
...
...
@@ -55,8 +56,2 @@
if
self
.
config
.
debug
:
print
(
u
'
found indexable_types {}
'
.
format
(
'
,
'
.
join
(
indexable_types
(
schema
))))
for
etype
in
indexable_types
(
schema
):
rset
=
cnx
.
execute
(
'
Any X WHERE X is %(etype)s
'
%
{
'
etype
'
:
etype
})
if
self
.
config
.
debug
:
...
...
@@ -62,15 +57,7 @@
if
self
.
config
.
debug
:
print
(
u
'
indexing {} {}
'
.
format
(
etype
,
len
(
rset
)))
for
entity
in
rset
.
entities
():
# TODO add specific IFTIES adapter
serializer
=
entity
.
cw_adapt_to
(
'
ISerializable
'
)
json
=
serializer
.
serialize
()
# TODO remove non indexable data or (better) serialize only
if
not
self
.
config
.
dry_run
:
es
.
index
(
index
=
index_name
,
doc_type
=
etype
,
body
=
json
)
# TODO optimize with elasticsearch.helpers.bulk
# or elasticsearch.helpers.parallel_bulk
# or elasticsearch.helpers.streaming_bulk
# TODO optimisation : json serialize on one side, send to ES on the other
# TODO progress bar
print
(
u
'
found indexable_types {}
'
.
format
(
'
,
'
.
join
(
indexable_types
(
schema
))))
for
etype
in
indexable_types
(
schema
):
rset
=
cnx
.
execute
(
'
Any X WHERE X is %(etype)s
'
%
{
'
etype
'
:
etype
})
if
self
.
config
.
debug
:
...
...
@@ -76,3 +63,20 @@
if
self
.
config
.
debug
:
print
(
u
'
.
'
,
end
=
u
''
)
print
(
u
'
indexing {} {}
'
.
format
(
etype
,
len
(
rset
)))
for
entity
in
rset
.
entities
():
# TODO add specific IFTIES adapter
serializer
=
entity
.
cw_adapt_to
(
'
ISerializable
'
)
json
=
serializer
.
serialize
()
# TODO remove non indexable data or (better) serialize only
if
not
self
.
config
.
dry_run
:
es
.
index
(
index
=
index_name
,
doc_type
=
etype
,
body
=
json
)
# TODO optimize with elasticsearch.helpers.bulk
# or elasticsearch.helpers.parallel_bulk
# or elasticsearch.helpers.streaming_bulk
# TODO optimisation : json serialize on one side, send to ES on the other
# TODO progress bar
if
self
.
config
.
debug
:
print
(
u
'
.
'
,
end
=
u
''
)
if
self
.
config
.
debug
:
print
(
u
''
)
else
:
if
self
.
config
.
debug
:
...
...
@@ -78,5 +82,4 @@
if
self
.
config
.
debug
:
print
(
u
''
)
print
(
u
'
no elasticsearch configuration found, skipping
'
)
CWCTL
.
register
(
IndexInES
)
This diff is collapsed.
Click to expand it.
hooks.py
+
11
−
10
View file @
aea1ea56
...
...
@@ -46,13 +46,14 @@
def
__call__
(
self
):
locations
=
self
.
_cw
.
vreg
.
config
[
'
elasticsearch-locations
'
]
index_name
=
self
.
_cw
.
vreg
.
config
[
'
index-name
'
]
serializer
=
self
.
entity
.
cw_adapt_to
(
'
ISerializable
'
)
json
=
serializer
.
serialize
()
es
=
Elasticsearch
(
locations
and
locations
.
split
(
'
,
'
)
or
None
)
try
:
# TODO option pour coté async ?
es
.
index
(
index
=
index_name
,
doc_type
=
self
.
entity
.
cw_etype
,
body
=
json
)
except
(
ConnectionError
,
ProtocolError
):
log
.
debug
(
'
Failed to index in hook, could not connect to ES
'
)
if
locations
and
index_name
:
serializer
=
self
.
entity
.
cw_adapt_to
(
'
ISerializable
'
)
json
=
serializer
.
serialize
()
es
=
Elasticsearch
(
locations
and
locations
.
split
(
'
,
'
))
try
:
# TODO option pour coté async ?
es
.
index
(
index
=
index_name
,
doc_type
=
self
.
entity
.
cw_etype
,
body
=
json
)
except
(
ConnectionError
,
ProtocolError
):
log
.
debug
(
'
Failed to index in hook, could not connect to ES
'
)
This diff is collapsed.
Click to expand it.
site_cubicweb.py
+
6
−
4
View file @
aea1ea56
...
...
@@ -3,10 +3,11 @@
{
'
type
'
:
'
string
'
,
'
default
'
:
''
,
'
help
'
:
'
Elastic Search location (eg. 192.168.0.23:9200),
'
'
this can be a list of locations (192.168.0.23:9200,192.168.0.24:9200,
'
'
you can also include the scheme (eg. http://192.168.0.23:9200)
'
,
'
this can be a list of locations (192.168.0.23:9200,192.168.0.24:9200,
'
'
you can also include the scheme (eg. http://192.168.0.23:9200)
'
'
warning: if this is not defined indexing will be disabled (no localhost default)
'
,
'
group
'
:
'
elasticsearch
'
,
'
level
'
:
5
,
}),
(
'
index-name
'
,
{
'
type
'
:
'
string
'
,
...
...
@@ -8,10 +9,11 @@
'
group
'
:
'
elasticsearch
'
,
'
level
'
:
5
,
}),
(
'
index-name
'
,
{
'
type
'
:
'
string
'
,
'
default
'
:
'
cubicweb
'
,
'
help
'
:
'
Elastic Search index name (eg. cubicweb)
'
,
'
default
'
:
''
,
'
help
'
:
'
Elastic Search index name (eg. cubicweb)
'
'
warning: if this is not defined indexing will be disabled (no index name default)
'
,
'
group
'
:
'
elasticsearch
'
,
'
level
'
:
5
,
}),
...
...
This diff is collapsed.
Click to expand it.
test/test_elastic_search.py
+
3
−
1
View file @
aea1ea56
...
...
@@ -25,6 +25,8 @@
self
.
orig_config_for
=
CubicWebConfiguration
.
config_for
config_for
=
lambda
appid
:
self
.
config
# noqa
CubicWebConfiguration
.
config_for
=
staticmethod
(
config_for
)
self
.
config
[
'
elasticsearch-locations
'
]
=
'
http://10.1.1.1:9200
'
self
.
config
[
'
index-name
'
]
=
'
unittest_index_name
'
def
to_test_etypes
(
self
):
with
self
.
admin_access
.
repo_cnx
()
as
cnx
:
...
...
@@ -79,7 +81,7 @@
self
.
assert_
(
cnx
.
execute
(
'
Any X WHERE X is %(etype)s
'
%
{
'
etype
'
:
indexable_types
(
cnx
.
repo
)[
0
]}))
create
.
assert_called_with
(
ignore
=
400
,
index
=
'
cubicweb
'
,
body
=
index_settings
())
ignore
=
400
,
index
=
'
unittest_index_name
'
,
body
=
index_settings
())
index
.
assert_called
()
# TODO ? check called data
...
...
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