How to send elasticsearch multi search request in Postman?
Asked Answered
B

4

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)
Besotted answered 1/8, 2017 at 11:59 Comment(1)
Not sure if this is the problem but msearch is GET and not POSTEnvenom
H
62

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":{}}}
'
Hadria answered 1/8, 2017 at 13:55 Comment(5)
Thank you Joanna. I miss the part to set content-type to application/x-ndjson.Besotted
Thank you, thank you, thank you! That last empty line on the bottom of the body is totally crucial. :-)Lei
@Mikhail, please check official docs about Request Body Search, 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.Hadria
Am I the only one, where application/x-ndjson does not work, but application/json ?Leprechaun
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 errorsCasebook
S
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

Steinke answered 15/11, 2018 at 6:9 Comment(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.Glory
A
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

Ansela answered 31/7, 2020 at 12:31 Comment(1)
Please include the code here instead of the screenshot of it.Langham
F
0

I have use this CURL and it's working fine in my case

curl --location 'localhost:9200/_search' \
--header 'Content-Type: application/json' \
--data '{
    "query": {
        "match" : {
            "message" : "Text Here"
        }
    }
}'
Faith answered 16/1 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.