33

I'm trying to send elasticserach multi search request via postman as below:

POST - http://localhost:9200/_msearch
content-type : x-www-form-urlencoded
body:
{"index":"accounts"}
{"query":{"bool":{"should":[{"match":{"owner.first_name":"Creeple"}}]}}}

However, I'm getting following error:

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "Failed to derive xcontent"
      }
    ],
    "type": "parse_exception",
    "reason": "Failed to derive xcontent"
  },
  "status": 400
}

Note that if I perform same request via my play code, results are succesfully fetched.

WS.url("localhost:9200/_msearch").withHeaders("Content-type" -> "application/x-www-form-urlencoded").post(query)
Ra Ka
  • 2,995
  • 3
  • 23
  • 31

3 Answers3

61

Three things are important here:

  1. When inserting body, select raw radiobutton and Text (or JSON) from dropdown.
  2. Add header: Content-type: application/x-ndjson
  3. Most important: put new line after the last line of your query

Body: enter image description here

Header:

enter image description here

Curl version:

curl -X POST \
  http://127.0.0.1:9200/_msearch \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/x-ndjson' \
  -d '{"index":"script","type":"test"}
{"query":{"match_all":{}}}
'
Joanna Mamczynska
  • 2,148
  • 16
  • 14
  • Thank you Joanna. I miss the part to set `content-type` to `application/x-ndjson`. – Ra Ka Aug 01 '17 at 16:50
  • 1
    Thank you, thank you, thank you! That last empty line on the bottom of the body is totally crucial. :-) – Luis Milanese Mar 29 '18 at 18:14
  • 1
    @Mikhail, please check official docs about [Request Body Search](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#search-request-body), especially this statement: *Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well.* Personally, when I send request with message body, I prefer to use POST. – Joanna Mamczynska Aug 25 '18 at 09:19
  • Am I the only one, where application/x-ndjson does not work, but application/json ? – Wolfgang Blessen Aug 12 '19 at 15:00
  • 3
    Something I was missing is that json should be squashed into one line. Don't know if this is stupid, but I certainly pulled my hair out because of the issue with json being properly formatted. I was receiving `Unexpected end-of-input` errors – BozanicJosip Mar 31 '20 at 16:32
5

You can also make your request body be json format and change your Content-Type be application/json, please take a look as below

Header with Content-Type

Your search request with json type

Response data

Peter.Chu
  • 357
  • 5
  • 4
  • 1
    The question was about Elasticsearch multi-search (_msearch), not _search. The most upvoted answer showing an example with _msearch and application/x-ndjson is correct. – ewilan Feb 14 '20 at 13:14
0

I have found error for "Expected [START_OBJECT] but found [null]" when I use _msearch API in elasticsearch.

Response of this api

Request parameters and url