【问题标题】:Need some help regarding the Elasticsearch engine and react js需要一些关于 Elasticsearch 引擎和 react js 的帮助
【发布时间】:2021-12-18 13:12:44
【问题描述】:

我希望你做得很好,我需要一些关于 Elasticsearch 引擎的帮助。我正在做的是我正在尝试创建一个搜索引擎,我已经成功地将我的数据通过 kibana 发布到 elasticsearch 引擎。但是“但是如何将 elastyicsearch 的搜索组件添加到我的 react 应用程序中”,我在 kibana 索引中有 400 万条记录,当我尝试直接从 react 中搜索时需要很长时间使用 nodejs api 将记录显示到我的 frontapp 应用程序中。下面是 nodejs 的代码,但是这个代码的问题它只给了我 10 条记录。

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

});

有什么方法可以将 elasticsearch 组件直接添加到我的 reactjs 应用程序中。像 localhost:9200/index.. 一样直接来自 elasticsearch api?

【问题讨论】:

    标签: javascript node.js reactjs elasticsearch kibana


    【解决方案1】:

    您对 Elasticsearch 的请求对我来说有点奇怪,您是否尝试过使用 body 进行搜索,例如 documentation?这一行:

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

    似乎不是编写查询的正确方法。您要搜索哪个字段?

    我看到您尝试使用size 字段,这应该是正确的。您还可以尝试以下方法:

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

    【讨论】:

    • 以及从 react 调用它的 api 应该是什么?
    • 其实我需要创建搜索引擎。我是 elasticsearch 引擎的新手,我们可以在我们的 react App 中添加一个 elasticsearch 组件吗?
    • 你说这是你的 NodeJS 应用程序,所以它应该是你的服务器。因此,您的 React 应用程序需要调用您的端点。
    【解决方案2】:

    您可以使用SearchKit 直接从您的 react 应用中查询 elasticsearch。但请注意,在您自己的基础架构之外公开数据库服务是不好的做法。

    你可以像这样使用组件:

    import {
      SearchkitManager,
      SearchkitProvider,
      SearchkitComponent
    } from 'searchkit'
    
    const searchkit = new SearchkitManager(host)
    
    class Render extends SearchkitComponent {
    
      render(){
        let results = await this.searchkit.reloadSearch()
        return <div>{results}</div>
      }
    
    }
    
    function table(){
    
      return <SearchkitProvider searchkit={searchkit}>
                <Render />
              </SearchkitProvider>
    }
    

    【讨论】:

    • 我可以直接从我的 react 应用调用 localhost:9200 吗?
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多