Request body or source parameter is required – Elasticsearch Error How To Solve Related Issues

Request body or source parameter is required – Elasticsearch Error How To Solve Related Issues

Opster Team

July-20, Version: 1.7-8.0

 

To understand why this error appeared and how to remedy the errors in your query, you should run the Elasticsearch Error Check-Up.

This guide will help you understand issues realted to the log “request body or source parameter is required”. To get started, read the general overview on common issues and tips related to the Elasticsearch concepts: rest, request and source.

Background

Analyze API is used to perform analysis on a text string and return the resulting tokens. The above exception arises when the CURL request of Analyze API is malformed. The proper format, as detailed below in the “How to fix” section, is required to get the tokens using this API.

How to reproduce this exception

In the CURL format, if the Analyze request is written like this:

Analyze API

curl -XGET "localhost:9200/my-index/_analyze?analyzer=standard&pretty" -H -d "Opster"

Then the response generated will be:

{
 "error": {
   "root_cause": [
     {
       "type": "parse_exception",
       "reason": "request body or source parameter is required"
     }
   ],
   "type": "parse_exception",
   "reason": "request body or source parameter is required"
 },
 "status": 400
}

How to fix this exception

The curl request is malformed. The query can be modified in the following way:

In curl format

curl -XGET "http://localhost:9800/my-index/_analyze?pretty" -H 'Content-Type: application/json' -d '{ "analyzer": "standard", "text": "Opster" }'

GET /my-index/_analyze?pretty
{
 "analyzer" : "standard",
 "text" : "opster"
}

The tokens generated from Above Analyze API are:

{
 "tokens": [
   {
     "token": "opster",
     "start_offset": 0,
     "end_offset": 4,
     "type": "<ALPHANUM>",
     "position": 0
   }
 ]
}

To learn more about tokens, you can refer to text analysis for more information.

Log Context

Log”request body or source parameter is required”classname  is RestRequest.java We extracted the following from Elasticsearch source code for those seeking an in-depth context :

* Get the content of the request or the contents of the {@code source} param or throw an exception if both are missing.
  * Prefer {@link #contentOrSourceParamParser()} or {@link #withContentOrSourceParamParserOrNull(CheckedConsumer)} if you need a parser.
  */
  public final Tuple contentOrSourceParam() {
  if (hasContentOrSourceParam() == false) {
  throw new ElasticsearchParseException("request body or source parameter is required");
  } else if (hasContent()) {
  return new Tuple(xContentType.get(); requiredContent());
  }
  String source = param("source");
  String typeParam = param("source_content_type");

 

Optimize Elasticsearch Performance

Try The Tool