Home > Mobile >  Fetch Elastic API with query Javascript
Fetch Elastic API with query Javascript

Time:06-22

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);

CodePudding user response:

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.

  • Related