Home > OS >  Need some help regarding the Elasticsearch engine and react js
Need some help regarding the Elasticsearch engine and react js

Time:11-05

I hope you are doing well, I need some help regarding the Elasticsearch engine. what I am doing is I am trying to create a search engine I have successfully post my data through kibana to elasticsearch engine. but "but how can I add the search component of elastyicsearch to my react app", I have like 4 million records into the kibana index, when I try to search directly from react it take a long time to display records into my frontapp app with nodejs api. below is the code with nodejs but the problem with this code it just gives me 10 records only.

router.get('/tweets', (req, res)=>{
let query = {
    index: 'tweets',
    // size: 10000
}
if(req.query.tweets) query.q = `*${req.query.tweets}*`;
client.search(query)
.then(resp => {
    return res.status(200).json({
        tweets: resp.body.hits.hits
    });
})
.catch(err=>{
    console.log(err);
    return res.status(500).json({
        err
    });
});

});

Is there any way to impliment elasticsearch component directly to my reactjs app. like with the localhost:9200/index.. directly from the elasticsearch api?

CodePudding user response:

You could use SearchKit to directly query elasticsearch from you react app. But be aware that exposing DB services outside of you own infrastructure is bad practice.

CodePudding user response:

Your request to Elasticsearch looks a bit strange to me, have you tried to search using a body like in the documentation? This line:

if(req.query.tweets) query.q = `*${req.query.tweets}*`;

doesn't seem like a correct way to write a query. Which field do you want to search for?

I saw that you tried to use the size field, which should be correct. You can also try the following:

client.search({
  index: 'tweets',
  body: {
    size: 1000, // You can put the size here to get more than 10 results
    query: {
      wildcard: { yourfield: `*${req.query.tweets}*` }
    }
  }
}, (err, result) => {
  if (err) console.log(err)
})
  • Related