hjhuzr
hjhuzr

Reputation: 11

post bulk.json with axios elasticsearch

I am trying to make bulk post with elasticsearch and axios , I have a problem with post request :

 axios.put('http://localhost:9200/indexrandom881', {'products-bulk.json'

});

For adding and deleting index it work : Deleting and index

axios.delete('http://localhost:9200/indexrandom'+x, {
    });

Adding an index

  axios.put('http://localhost:9200/indexrandom881'+x, {

    });

Please do anyone has an idea. In brief I need this command in axios form

curl -H "Content-Type: application/x-ndjson" -XPOST http://localhost:9200/products/_bulk --data-binary "@products-bulk.json"

Thanks

Upvotes: 1

Views: 1865

Answers (2)

axiosmanjjk
axiosmanjjk

Reputation: 11

Thanks @Joe Sorocin but that working only in node.js , I need to implement it in react, in react it shows error fs.readfile is not a function the full essay is :

File : App.js

function App() {
    const axios = require('axios');
    const fs = require('fs');
    const filePath = __dirname + '/national-es-bulk-index22.json';
    const indexName = 'indexrandom881';
    const url = `http://localhost:9200/${indexName}/_bulk`;
    
    fs.readFile(filePath, async (err, jsonData) => {
        if (err) {
            console.error({ err });
            return;
        }

        const { data } = await axios.post(url, jsonData, {
            headers: {
                'Content-Type': 'application/x-ndjson'
            }
        });

        console.info({ data });
    });
    return (
        <div className="App">
            <h1>test</h1>
        </div>
    );
}

export default App;

Upvotes: 1

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

Use post instead of put. Also, you'll need to first read the file using fs before you pass it along to Elasticsearch with the application/x-ndjson header:

const axios = require('axios');
const fs = require('fs');

const filePath = __dirname + '/products-bulk.json';

const indexName = 'indexrandom881';
const url = `http://localhost:9200/${indexName}/_bulk`;

fs.readFile(filePath, async (err, jsonData) => {
    if (err) {
        console.error({ err });
        return;
    }

    const { data } = await axios.post(url, jsonData, {
        headers: {
            'Content-Type': 'application/x-ndjson'
        }
    });

    console.info({ data });
});

Upvotes: 0

Related Questions