[英]NodeJS returns no results with Elasticsearch / Mongoosastic
I'm trying to create a simple search form to search in one Model (Products). 我正在尝试创建一个简单的搜索表单以在一个模型(产品)中进行搜索。
Here is my Product Model using Mongoosastic: 这是我使用Mongoosastic的产品模型:
var ProductSchema = new Schema({
category: { type: Schema.Types.ObjectId, ref: 'Category'},
name: String,
price: Number,
image: String,
description: String
});
ProductSchema.plugin(mongoosastic, {
hosts: [
'localhost:9200'
]
});
I connect ElasticSearch with Product model like so: 我将ElasticSearch与产品模型相连接,如下所示:
Product.createMapping(function (err, mapping) {
if (err) {
console.log("error creating mapping");
console.log(err);
} else {
console.log("Mapping created");
console.log(mapping);
}
});
var stream = Product.synchronize();
var count = 0;
stream.on('data', function () {
count++;
});
stream.on('close', function () {
console.log("Indexed" + count + " documents");
});
stream.on('error', function (err) {
console.log(err);
});
After that I created the GET method to render the results page like so: 之后,我创建了GET方法来呈现结果页面,如下所示:
router.get('/search', function (req, res, next) {
if (req.query.q){
Product.search({
query_string: { query: req.query.q }
}, function (err, results) {
if (err) return next(err);
var data = results.hits.hits.map(function (hit) {
return hit;
});
res.render('main/search-result', {
query: req.query.q,
data: data
});
console.log(data);
});
}
});
And in the frontend page I loop through results like so: 在前端页面中,我遍历如下结果:
<div class="row">
<% for (var i = 0;i < data.length; i++){%>
<div class="col-md-4">
<a href="/product/<%= data[i]._source._id %>"></a>
<div class="thumbnail">
<img src="<%= data[i]._source.image%>" alt="">
<div class="caption">
<h3><%= data[i]._source.name %></h3>
<h4><%= data[i]._source.category.name %></h4>
<h5><%= data[i]._source.price %></h5>
<p><%= data[i]._source.description %></p>
</div>
</div>
</div>
<% } %>
</div>
In the terminal everything is Ok 在终端上一切都很好
Server is Running on port 3000
Mapping created
{ acknowledged: true }
Connected to the database
Indexed120 documents
Why are there no results rendered on my page? 为什么页面上没有显示结果?
Database
. Database
。 Is there any record in the document? URL
with Postman
. Postman
检查您的URL
。 In the following code of yours, 在您的以下代码中,
router.get('/search', function (req, res, next) {
if (req.query.q){
Product.search({
query_string: { query: req.query.q }
}, function (err, results) {
if (err) return next(err);
var data = results.hits.hits.map(function (hit) {
return hit;
});
res.render('main/search-result', {
query: req.query.q,
data: data
});
console.log(data);
});
}
});
the first parameter of .search() function is the QUERY parameter so you don't need the query_string here. .search()函数的第一个参数是QUERY参数,因此您在这里不需要query_string。 Just use a
terms
or match
query. 只需使用
terms
或match
查询即可。
as in, do the following changes, 与之类似,请进行以下更改,
router.get('/search', function (req, res, next) {
if (req.query.q){
Product.search({
terms: { query: req.query.q }
}, function (err, results) {
if (err) return next(err);
var data = results.hits.hits.map(function (hit) {
return hit;
});
res.render('main/search-result', {
query: req.query.q,
data: data
});
console.log(data);
});
}
});
And it will start working. 它将开始工作。 I just ran into this error a few days back.
几天前我刚遇到这个错误。 This is how I could solve it.
这就是我可以解决的方法。
If you need any more help, just drop a comment. 如果您需要更多帮助,只需发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.