Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
humanities
gargantext
Commits
aaaa31b1
Commit
aaaa31b1
authored
Nov 17, 2014
by
Mathieu Rodic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[BUGFIX] Improved all CorpusController methods: now only children are used
parent
3765084c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
45 deletions
+49
-45
api.py
gargantext_web/api.py
+49
-45
No files found.
gargantext_web/api.py
View file @
aaaa31b1
...
...
@@ -11,6 +11,26 @@ from django.db import connection
# from node.models import Node, NodeType, Node_Resource, Project, Corpus
# from node.admin import CorpusForm, ProjectForm, ResourceForm
_sql_cte
=
'''
WITH RECURSIVE cte ("depth", "path", "ordering", "id") AS (
SELECT 1 AS depth,
array[T."id"] AS path,
array[T."id"] AS ordering,
T."id"
FROM
%
s T
WHERE T."parent_id" IS NULL
UNION ALL
SELECT cte.depth + 1 AS depth,
cte.path || T."id",
cte.ordering || array[T."id"],
T."id"
FROM
%
s T
JOIN cte ON T."parent_id" = cte."id"
)
'''
%
(
Node
.
_meta
.
db_table
,
Node
.
_meta
.
db_table
,
)
def
DebugHttpResponse
(
data
):
return
HttpResponse
(
'<html><body style="background:#000;color:#FFF"><pre>
%
s</pre></body></html>'
%
(
str
(
data
),
))
...
...
@@ -58,19 +78,28 @@ class CorpusController:
raise
ValidationError
(
'The order parameter should take one of the following values: '
+
', '
.
join
(
_ngrams_order_columns
),
400
)
order_column
=
_ngrams_order_columns
[
order
]
# query building
ngramsQuery
=
Ngram
.
objects
.
filter
(
nodes__parent
=
corpus
,
terms__startswith
=
request
.
GET
.
get
(
'startswith'
,
''
)
)
.
annotate
(
count
=
Count
(
'id'
))
# how should we order this?
orderColumn
=
{
"frequency"
:
"-count"
,
"alphabetical"
:
"terms"
}
.
get
(
request
.
GET
.
get
(
'order'
,
'frequency'
),
'-count'
)
ngramsQuery
=
ngramsQuery
.
order_by
(
orderColumn
)
cursor
=
connection
.
cursor
()
cursor
.
execute
(
_sql_cte
+
'''
SELECT ngram.terms
FROM cte
INNER JOIN
%
s AS node ON node.id = cte.id
INNER JOIN
%
s AS nodetype ON nodetype.id = node.type_id
INNER JOIN
%
s AS node_ngram ON node_ngram.node_id = node.id
INNER JOIN
%
s AS ngram ON ngram.id = node_ngram.ngram_id
WHERE (NOT cte.id =
\'
%
d
\'
) AND (
\'
%
d
\'
= ANY(cte."path"))
AND nodetype.name = 'Document'
AND ngram.terms LIKE '
%
s
%%
'
GROUP BY ngram.terms
ORDER BY SUM(node_ngram.weight) DESC
'''
%
(
Node
.
_meta
.
db_table
,
NodeType
.
_meta
.
db_table
,
Node_Ngram
.
_meta
.
db_table
,
Ngram
.
_meta
.
db_table
,
corpus
.
id
,
corpus
.
id
,
request
.
GET
.
get
(
'startwith'
,
''
)
.
replace
(
"'"
,
"
\\
'"
),
))
# # how should we order this?
# orderColumn = {
# "frequency" : "-count",
# "alphabetical" : "terms"
# }.get(request.GET.get('order', 'frequency'), '-count')
# response building
return
JsonHttpResponse
({
"list"
:
[
ngram
.
terms
for
ngram
in
ngramsQuery
],
"list"
:
[
row
[
0
]
for
row
in
cursor
.
fetchall
()
],
})
@
classmethod
...
...
@@ -79,19 +108,17 @@ class CorpusController:
corpus
=
cls
.
get
(
corpus_id
)
# query building
cursor
=
connection
.
cursor
()
cursor
.
execute
(
''' SELECT
key,
COUNT(*) AS count
cursor
.
execute
(
_sql_cte
+
'''
SELECT key
FROM (
SELECT skeys(metadata) AS key
FROM
%
s
FROM cte
INNER JOIN
%
s AS node ON node.id = cte.id
WHERE (NOT cte.id =
\'
%
d
\'
) AND (
\'
%
d
\'
= ANY(cte."path"))
) AS keys
GROUP BY
key
ORDER BY
count DESC
'''
%
(
Node
.
_meta
.
db_table
,
))
GROUP BY key
ORDER BY COUNT(*) DESC
'''
%
(
Node
.
_meta
.
db_table
,
corpus
.
id
,
corpus
.
id
,
))
# response building
return
JsonHttpResponse
({
"list"
:
[
row
[
0
]
for
row
in
cursor
.
fetchall
()],
...
...
@@ -145,30 +172,7 @@ class CorpusController:
else
:
raise
ValidationError
(
'Unrecognized "filter[]=
%
s"'
%
(
filter
,
))
# query building: initializing SQL
sql
=
str
(
corpus
.
descendants
()
.
query
)
sql_array_select
=
sql
.
split
(
'SELECT'
)
sql_array_from
=
sql_array_select
[
-
1
]
.
split
(
'FROM'
)
sql_array_where
=
sql_array_from
[
-
1
]
.
split
(
'WHERE'
)
sql_array_order
=
sql_array_where
[
-
1
]
.
split
(
'ORDER'
)
sql_0
=
''' WITH RECURSIVE cte (
"depth", "path", "ordering", "id") AS (
SELECT 1 AS depth,
array[T."id"] AS path,
array[T."id"] AS ordering,
T."id"
FROM node_node T
WHERE T."parent_id" IS NULL
UNION ALL
SELECT cte.depth + 1 AS depth,
cte.path || T."id",
cte.ordering || array[T."id"],
T."id"
FROM node_node T
JOIN cte ON T."parent_id" = cte."id")
'''
sql_0
=
_sql_cte
sql_1
=
'
\n
SELECT '
sql_2
=
'
\n
FROM
%
s
\n
INNER JOIN cte ON cte."id" =
%
s.id'
%
(
Node
.
_meta
.
db_table
,
Node
.
_meta
.
db_table
,
)
sql_3
=
'
\n
WHERE ((NOT cte.id =
\'
%
d
\'
) AND (
\'
%
d
\'
= ANY(cte."path")))'
%
(
corpus
.
id
,
corpus
.
id
,
)
...
...
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