【问题标题】:Indexing user query with Appbaseio and ReactiveSearch使用 Appbaseio 和 ReactiveSearch 索引用户查询
【发布时间】:2019-01-29 05:12:54
【问题描述】:

我正在尝试使用 ReactiveSearch 的 DataSearch 组件和 appbase-js 为用户的查询建立索引。

所以我已经为 appbase-js 与 appbaseio 交互制作了我的 Node/Express 应用程序。 在 app.js 中:

...
const search = require('./routes/search');
...
app.use('/api/search', search);

那么这里是我的 search.js

const express = require('express');
const Appbase = require('appbase-js');

// create an appbase.io app's instance
const appbaseRef = new Appbase({
  url: "https://scalr.api.appbase.io",
  app: "index-query",
  credentials: "####-####"
});

const router = express.Router();

/* GET search. */
router.get('/test', (req, res, next) => {
  res.send('This is the SEARCH route - and it WORKS!');
});


router.post('/query', (req, res, next) => {
  appbaseRef.index({
    type: "autocomplete",
    body: value
  }).then('data', response => {
    console.log("@index success: ", response);
  }),('error', error => {
    console.log("@index error: ", error);
  });
});

module.exports = router;

然后这是我的 DataSearch 组件:

<DataSearch
   componentId="SearchSensor"
   dataField={["suggestions"]}
   className="search-bar"
   iconPosition="right"
   innerclassName={{
     list: "text-item"
   }}
   onValueSelected{
     (value) => {
      ????
      }
   } 
  />

有人建议我在另一个问题不要这样做

onValueSelected={(value) => {
    fetch('YOUR_SERVER_URL' or 'Elasticsearch URL', { method: 'POST', body: {...} })
  }

以免在客户端暴露敏感信息

我不确定如何从我的 React 前端获取值(用户的查询)到我的 Node/Express 后端,以便可以将其索引到 Appbaseio 上的 ES 应用程序?

【问题讨论】:

    标签: node.js reactjs elasticsearch indexing reactivesearch


    【解决方案1】:

    假设你的服务器托管在'SERVER_URL',关键是通过fetch请求将数据从前端发送到服务器:

    <DataSearch
      ...
      onValueSelected={(value) => {
        fetch('SERVER_URL/api/search/query', {
          method: 'POST',
          body: JSON.stringify({ value })
        }).then(() => handle response client side))
      }}
    />
    

    然后你可以添加body-parser中间件in express

    app.use(bodyParser.json())
    

    在您的路线中,您可以使用来自 body 的 valueindex 它到 elasticsearch。您可以使用您在此处使用的 appbase-js 中的 index 方法。

    router.post('/query', (req, res, next) => {
      appbaseRef.index({
        type: "autocomplete",
        body: { value: req.body.value }
      }).then('data', response => {
        console.log("@index success: ", response);
      }),('error', error => {
        console.log("@index error: ", error);
      });
    });
    

    【讨论】:

    • 感谢您的出色回答!据我了解,基本上发生了什么是我需要一个安装了 appbase-js 并设置为与我的 Appbaseio ES 应用程序交互的小型 Node/Express 应用程序......对吗?在我完成他的所有工作后,我就准备好了。
    • SERVER_URL 应该是 localhost:5000/api/search/query(现在)而不是 Appbase url,对吧?
    • 是的,它应该是您的后端网址
    • 我收到带有 500 错误代码的“访问控制允许来源”错误。我需要在 localhost 服务器上设置代理吗?所以基本上处理查询索引请求和响应的小型 Node/Express 应用程序应该充当我的 Appbaseio ES 应用程序的代理服务器?
    • 这可能是一个 cors 问题。你能试试添加cors中间件吗
    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2013-05-25
    • 2013-05-19
    • 1970-01-01
    • 2020-07-15
    相关资源
    最近更新 更多