estool pagination

22 views
Skip to first unread message

aDNers

unread,
Sep 18, 2018, 9:37:56 AM9/18/18
to dotCMS User Group
Hello,

I am trying to get pagination using estool.search to work, in order to get it to work I need to be able to insert "size" and "from" in the beginning of the json query. 
It works when adding the number manually, but not if I try to take the value from a variable, the below example also works if given "size": "3". 

I can't however get example 2, where I have a variable instead of "size" to work. I think I have tried every single escape variant but I guess it is something simple.. Does anyone know? 

Of course if there is a fully functional vtl that uses estool for search, with pagination that would work too ;-) 

BR
Anders



EXAMPLE 1 - hardcoded size:

#set($searchresults = $estool.search('{
    "size": 3,
    "query": {
        "query_string" : {
            "query" : "News.title:*"
        }
    }
}
'
))

#foreach($result in $searchresults)
$result.title
#end 

EXAMPLE 2 - variable value for size: 

#set($theSize = "3")

#set($searchresults = $estool.search('{
    "size": $theSize,
    "query": {
        "query_string" : {
            "query" : "News.title:*"
        }
    }
}
'
))

#foreach($result in $searchresults)
$result.title
#end 


Nathan Keiter

unread,
Sep 18, 2018, 9:42:58 AM9/18/18
to dot...@googlegroups.com

Did you try putting it in quotes?

 

Nathan I. Keiter | Lead Network Applications Programmer | Benefits Advisory Council Member | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993

https://www.gettysburg.edu

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To post to this group, send email to dot...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/c05d4059-f02b-461e-93bf-a5086b3d6a09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathan Keiter

unread,
Sep 18, 2018, 9:44:31 AM9/18/18
to dot...@googlegroups.com

Also, what happens if you build the argument JSON outside of the call? Then do $estool.search( $args )

 

Nathan I. Keiter | Lead Network Applications Programmer | Benefits Advisory Council Member | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993

https://www.gettysburg.edu

 

Falzone, Chris

unread,
Sep 18, 2018, 2:09:47 PM9/18/18
to dot...@googlegroups.com
Using single quotes variables are NOT interpolated. 
Using double quotes they are.

Example.  The following:
#set($foo = 'Example')
#set($bar = '$foo')
#set($baz = "$foo")
$foo - $bar - $baz

Would output:
Example - $foo - Example

So you need to use double quotes if you want the interpolation.  You can then either escape the double quotes with backslashes or use $esc.q to get a double quote. 
Your code might look like:

#set($theSize = "3")

#set($searchresults = $estool.search("{
    ${esc.q}size${esc.q}: $theSize,
    ${esc.q}query${esc.q}: {
        ${esc.q}query_string${esc.q} : {
            ${esc.q}query${esc.q} : ${esc.q}News.title:*${esc.q}
        }
    }
}"))

#foreach($result in $searchresults)
  $result.title
#end


Make sense?  Hope it helps! 

aDNers

unread,
Sep 19, 2018, 6:17:25 AM9/19/18
to dotCMS User Group
Nathan and Chris: Thank you for the input!

I did tried to put everything in a variable before feeding it to $estool but my main problem seems to have been not understanding how to escape correctly, with the code suggested by Chris everything seems to work. I will now try to build the pagination using dynamic "from" and "size" fields. 

Again, you guys rock.
Reply all
Reply to author
Forward
0 new messages