0

i have elastic API and i try to fetch it with javascript and query on the fetch, but i don't know where to place the query, i only know how to fetch it just with authorization, can we fetch the elastic api with query ?

The Elastic Query:

GET http://192.168.150.155:9900/em-employeedata/_search
{
  "size": 10000,
  "query": {
    "match_all": {}
  }
}

The Javascript Code:

var url = 'http://192.168.150.155:9900/em-employeedata/_search/';

async function getapi(url) {

const response = await fetch(url, {method:'GET', 
headers: {'Authorization': 'Basic ' + btoa('em_user:3md@t@2o22')}})

var data = await response.json();

}

getapi(url);

1 Answer 1

1

You can use Elasticsearch Javascript client for same.

You can check this documentation where they have given example of Search.

Below is sample example:

const { Client } = require('@elastic/elasticsearch')
const client = new Client({
  cloud: { id: '<cloud-id>' },
  auth: { apiKey: 'base64EncodedKey' }
})

const result = await client.search({
    index: 'game-of-thrones',
    query: {
      match: {
        quote: 'winter'
      }
    }
  })

  console.log(result.hits.hits)

Above example show connecting using cloud_id and apiKey but there are other options also available. Please check this link.

Sign up to request clarification or add additional context in comments.

2 Comments

can i use it without node js? because i don't use node js, i use a front end javascript
If you are not using node.js then you can simply set fetch method to POST and pass body with json request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.