The Elasticsearch viewtool allows a Widget to perform an Elasticsearch query (using full Elasticsearch JSON syntax) and return a list of results which you can iterate over using Velocity. Elasticsearch queries can be tested in the Elasticsearch Portlet before using them with the viewtool.
The following example shows how the Elasticsearch Viewtool is mapped in the toolbox.xml file:
<tool>
<key>estool</key>
<scope>request</scope>
<class>com.dotcms.rendering.velocity.viewtools.ESContentTool</class>
</tool>
In the following example, an Elasticsearch query is supplied to the $estool.search
viewtool method to search every Content title containing the word “gas” in the dotCMS content store. The query also prevents matches of any non-English versions of the content, using the “must_not” range for any languageid greater than 1.
#[[
#set($results = $estool.search('{
"query": {
"bool": {
"must": {
"term": {
"title": "gas"
}
},
"must_not": {
"range": {
"languageid": {
"from": 2,
"to": 20
}
}
}
}
}
}'
))
#foreach($con in $results)
$con.title
#end
$results.response
]]#
For more examples of Elasticsearch queries, please see the Elasticsearch API documentation.