nicemayi
nicemayi

Reputation: 173

Sending requests to Elasticsearch with axios

I'm working on a react app which needs to fetch data from elsaticsearch. In frontend, actually I'm trying to use axios to do the request:

const query = {
  query: {
    match: {
      "_id": "AV12n5KzsohD5gXzTnOr"
    }
  }
};

axios.get('http://localhost:9200/my-index/my-type/_search', query)
  .then((res) => {
    console.log(res);
  });

I want to get the specific document with some ID. The above query actually works inside kibana. However, the above query returns all the documents inside my-type, what am I doing wrong here?

Upvotes: 8

Views: 15044

Answers (4)

AEF
AEF

Reputation: 157

Just for others if they come here. Following way worked for me: (Don't pay attention to sample data)

axios({
    url: 'your ES url',
    method: 'POST',
    timeout: 0,
    headers: {
      'Content-Type': 'application/json'
    },
    data: JSON.stringify({
      query: {
        bool: {
          filter: [
            { match_all: {} },
            { match_phrase: { 'data.gateway': { query: 'gateway1' } } },
            { match_phrase: { 'data.sensor': { query: '10001' } } },
            { range: { 'data.dateTime': { lte: '2020-05-26 20:25:00' } } },
            {
              range: {
                receivedInES: {
                  format: 'strict_date_optional_time',
                  gte: '2020-05-25T19:37:23.621Z',
                  lte: '2020-05-26T19:37:23.621Z'
                }
              }
            }
          ]
        }
      }
    })
  })

Upvotes: 1

HauntedSmores
HauntedSmores

Reputation: 1406

Just use .post()

From the Elasticsearch docs

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

Upvotes: 6

user94559
user94559

Reputation: 60133

I think the below should work. Although the Axios README says that data is specifically only for PUT, POST, and PATCH requests, I didn't see anything in the code that enforces this, and a simplified test shows that the request body is indeed sent for GET requests:

axios.get('http://localhost:9200/my-index/my-type/_search', {
  data: JSON.stringify(query),
}).then((res) => {
  console.log(res);
});

EDIT

Note that I've only tested this in Node.js, not in a browser. Browsers may be less inclined to include request bodies with GET requests.

EDIT 2

Elasticsearch seems to allow sending the request body in a parameter instead, perhaps because of this very issue.

This should do the trick:

axios.get('http://localhost:9200/my-index/my-type/_search', {
  params: {
    source: JSON.stringify(query),
    source_content_type: 'application/json'
  }
}).then((res) => {
  console.log(res);
});

EDIT 3

This does indeed seem to be a general restriction on making GET requests in browsers. Per the documentation for XMLHttpRequest.send:

If the request method is GET or HEAD, the argument is ignored and request body is set to null.

Upvotes: 19

Zepplock
Zepplock

Reputation: 29135

try this

axios.get(`http://localhost:9200/my-index/my-type/_search?q=${_id:AV12n5KzsohD5gXzTnOr}`)
  .then((res) => {
    console.log(res);
});

Upvotes: 1

Related Questions