[英]Indexing user query with Appbaseio and ReactiveSearch
我正在嘗試使用ReactiveSearch的DataSearch組件和appbase-js為用戶的查詢建立索引。
因此,我制作了Node / Express應用程序,用於與appbaseio進行appbase-js交互。 在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應用程序?
假設您的服務器托管在'SERVER_URL'
,關鍵是通過fetch
請求將數據從前端發送到服務器:
<DataSearch
...
onValueSelected={(value) => {
fetch('SERVER_URL/api/search/query', {
method: 'POST',
body: JSON.stringify({ value })
}).then(() => handle response client side))
}}
/>
然后,您可以在express中添加body-parser
中間件。
app.use(bodyParser.json())
在您的路線中,您可以使用body中的value
並將其index
到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);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.