Summary: I want to get search results from an Elasticsearch instance via Javascript. The POST
call fails, while the GET
one is fine.
EDIT: I found the solution through another question (I will flag mine as a duplicate), though I do not understand why the need for JSON.stringify()
.
Details: I am using the following function to request the total number of (filtered) hits:
function nrVulns() {
$.ajax({
type: "POST",
crossDomain: true,
url: 'http://elk.example.com:9200/scan/vulnerability/_search',
data: {
"query": {
"bool": {
"must": [
{
"match": {
"risk_factor": "critical"
}
}
]
}
},
"size": 0
}
}).success(function (data) {
$('#nrvuln').text(data["hits"]["total"]);
})
}
It fails with a 400 Bad Request
reply from Elasticsearch:
However:
- the same function with
type: "GET"
goes though, returning the total number of hits (not filtered byquery
). It looks like thedata
section is not taken into account (which is OK for aGET
request) - the
data
in the query above, copied and pasted verbatim into Sense and sent though aPOST
yields correct results (["hits"]["total"]
shows the correct number of hits, filtered byquery
) - a JSONLint confirms that the
data
string is correct JSON.
What is wrong with the POST
version of this query?