XWQL query on comments

Last modified by Vincent Massol on 2021/03/18 11:28

cogHow to filter XWiki documents with a XWQL query based on two values contained in the same or different comments
TypeSnippet
Category
Developed by

Ricardo Rodríguez, xwiki:XWiki.IsabelListaGarcia

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Comments can be of great help and easy use to store non-structured information on XWiki documents. Any structure given to the comment content by itself can even be more useful. For instance, you can use a "tag" like "InformationItem01:" to label any comment related with a given real fact. This could be for sure also applied to XWiki tags or notes, but this simple example applies only to comments: the obvious and conspicuous mean of adding information to a document other than the document edition!
Once you have a number of comments, the devil is in designing the way of querying this particular repository. XWQL is of great help for doing that! As currently there is no direct way of setting two different conditions on all comments of a document, we do need a NOT IN construction here.
And here we go!

The question

How many documents with 'string2' and without 'string1' in any comment's content does the current xwiki's database contain?

The code

{{velocity}}
#set ($string1 = 'string1')
#set ($string2 = 'string2')
##
#set ($xwqlquery = "SELECT doc2.fullName FROM Document doc2, doc2.object(XWiki.XWikiComments) comm2 
WHERE doc2.translation = 0 and comm2.comment LIKE :string1")
#set ($results = $services.query.xwql($xwqlquery).bindValue("string1","$string1").execute())
##
#foreach ($item in $results)
  #set ($str=$str+"'"+$item+"'"+",")
#end
#set ($strl = $str.length())
#set ($strls = $strl - 1)
#set ($nicestr = $str.substring(4,$strls))
##
#set ($xwqlquery1 = "FROM doc.object(XWiki.XWikiComments) AS comm 
WHERE doc.translation = 0 AND comm.comment like :string2 and doc.fullName NOT IN ($nicestr)")
#set ($results1 = $services.query.xwql($xwqlquery1).bindValue("string2","$string2").execute())
$results1.size()
{{/velocity}}
Tags: xwql comments
     

Get Connected