I am using Ajax request for elasticsearch to get search results.
Finally, I have found the query which I have to go for. (This is a follow up question to [link](https://stackoverflow.com/questions/44608208/ajax-query-format-with-elasticsearch))

Here is the query in cURL:

```
[~]$ curl -XGET 'localhost:9200/firebase/_search?pretty' -H 'Content-Type: application/json' -d'
> {"query": {"match": {"song": {"query": "i am in", "operator": "and"}}}}'

```

> 
> Result:
> 
> 
> 

```
{
  "took" : 286,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "firebase",
        "_type" : "song",
        "_id" : "0001",
        "_score" : 0.8630463,
        "_source" : {
          "song" : "i am in california",
          "song_name" : "hello",
          "song_url" : "https://s3.ap-south-1.amazonaws.com/..."
        }
      }
    ]
  }
}

```

This query gives me result exactly what I need.
When I am using the same request with AJAX, it gives me a bad request.

**Ajax.js** 

***Case 1:***

```
$(function() {
    $('#message').keyup(function() {    
        var query = {
            "query": {
                "match": {
                    "song": {
                        "query": $("#message").val(),
                        "operator": "and"
                    }
                }
            }
        };

        console.log(JSON.stringify(query));

        $.ajax({
            type: "GET",
            url: "http://localhost:9200/firebase/_search",
            contentType: 'application/json',
            data: query,
            success: searchSuccess,
            dataType: 'json'
        });

    });

});

```

Result in console:

[![enter image description here](https://i.stack.imgur.com/47BsW.png)](https://i.stack.imgur.com/47BsW.png)

***Case 2***: If I convert the 'data' using JSON.stringify, it gives me:[![enter image description here](https://i.stack.imgur.com/hC2r3.png)](https://i.stack.imgur.com/hC2r3.png)

***Case 3***: If I change the query structure as following and use JSON.stringify on 'data':

```
var query = {
    query: {
        match: {
            song: {
                query: $("#message").val(),
                operator: "and"
            }
        }
    }
};

```

Result is:

[![enter image description here](https://i.stack.imgur.com/3hbYt.png)](https://i.stack.imgur.com/3hbYt.png)

I am not sure what's the issue here, the query is correct because using the same with cURL I am getting correct results in bash. Question is how to correctly create the Ajax request here. Pointers would be appreciated.

**Edit**: And yes I have enabled CORS in elasticsearch.yml:

```
http.cors.enabled : true
http.cors.allow-origin : "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length

```

**Edit1**: Chrome, Network data underneath so as to provide more information:
[![enter image description here](https://i.stack.imgur.com/cf4Ms.png)](https://i.stack.imgur.com/cf4Ms.png)

**Edit2**: Full error response is below:

```
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/firebase/_search] contains unrecognized parameters: [query[match][song][operator]], [query[match][song][query]]"}],"type":"illegal_argument_exception","reason":"request [/firebase/_search] contains unrecognized parameters: [query[match][song][operator]], [query[match][song][query]]"},"status":400}

```