ES RESTful Services - Documentation topics on: apis,elasticsearch,elastic search,rest,restful,searchapi,.

ES RESTful Services

dotCMS supports pulling Content from Elasticsearch using the REST API. This document explains the endpoints, options and provides several examples of how to perform Elasticsearch queries using the REST API.

Endpoints

There are two endpoints to the Elasticsearch REST API:

EndpointDescription
/api/es/searchReturns the normal Elasticsearch response provided by dotCMS.
This is the same response you would get if performing the same Elasticsearch query in dotCMS.
/api/es/rawReturns the raw SearchResponse directly from ElasticSearch.

Pretty Print Option

By default, the value returned from an Elasticsearch REST query will be displayed in an unformatted block (without additional spaces and carriage returns to make it easily human readable). To display the results in a more easily readable format, append ?pretty to the end of the URL. For example:

curl -H "Content-Type: application/json" -XPOST http://localhost:8080/api/es/search?pretty -d '{...

Examples

Basic Content Search

The following curl command searches for the word “gas” in any field using the “_all” keyword to search within all fields.

curl -H "Content-Type: application/json" -XPOST http://localhost:8080/api/es/search -d '
    {
        "query": {
            "bool": {
                "must": {
                    "term": {
                        "_all": "gas"
                    }
                }
            }
        }
    }'

For more information on searching within all fields, please see the Search Within all Content Fields documentation.

Aggregations Query

The following curl command performs an aggregations query using the raw results endpoint:

curl -H "Content-Type: application/json" -XPOST http://localhost:8080/api/es/raw -d '
    {
        "query" : { "query_string" : {"query" : "gas*"} },
        "aggs" : {
            "tags" : { "terms" : {"field" : "news.tags"} }
        }
    }'

For more information on aggregation queries, please see the Elasticsearch Aggregations documentation.

Suggestions

The following curl performs a search to support a Suggestions feature (Did you mean?), using the raw results endpoint:

curl -H "Content-Type: application/json" -XPOST http://localhost:8080/api/es/raw -d '
    {
      "suggest" : {
        "title-suggestions" : {
          "text" : "gs pric rollrcoater",
          "term" : {
            "size" : 3,
            "field" : "title"
          }
        }
      }
    }'

For more information on implementing features such as suggestions, please see the Elasticsearch Examples documentation.