我如何使用 mongoosastic 在弹性搜索中获得搜索结果?
How would I able to get search results in elastic search using mongoosastic?
我正在尝试使用 mongoosastic search() 但它没有给我结果。
数据保存在 MongoDB 和 elasticsearch 中。
下面是猫鼬模式
const mongoose = require('mongoose');
const mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;
mongoose.connect(`mongodb://localhost:27017/elasticsearch`, function(err) {
if (err) {
console.error(err);
}
console.log('connected.... unless you see an error the line before this!');
});
var ItemSchema = new Schema({
name : {type : String, es_indexed : true},
description : {type : String, es_indexed : true}
});
ItemSchema.plugin(mongoosastic, {
"host": "localhost",
"port": 9200
});
var ItemModel = mongoose.model("Item", ItemSchema);
ItemModel.createMapping((err, mapping) => {
console.log('mapping created');
});
我试图用来获得所需搜索结果的搜索查询。
app.get("/search", (req, res) => {
console.log(req.query.item);
ItemModel.search({
"multi_match": {
"fields": ["multi_field"],
"query": req.query.item,
"fuzziness": "AUTO"
}
},
function (err, results) {
if (err) {
return console.log(JSON.stringify(err, null, 4));
}
return console.log(JSON.stringify(results, null, 4));
});
});
以下是存储在 mongoDB.
上的结果
{ "_id" : ObjectId("627204f3ec9b3ac2cee45c75"), "name" : "water bottle", "description" : "used to store water", "__v" : 0 }
{ "_id" : ObjectId("62720516ec9b3ac2cee45c77"), "name" : "chair", "description" : "used to sit", "__v" : 0 }
{ "_id" : ObjectId("6272060aec9b3ac2cee45c84"), "name" : "Dettol hand sanitizer", "description" : "used to sanitize hands", "__v" : 0 }
{ "_id" : ObjectId("62724a5b9c2c700433afab84"), "name" : "laptop charger", "description" : "used to charge laptop", "__v" : 0 }
Elasticsearch 中的映射
{
"items" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
elasticsearch上保存的数据集
Data saved on Elasticsearch
终于对我有用了...
const item = req.params.item;
const results = await ItemModel.search({
query_string: {
query: item
}
});
if (results.body.hits.hits) {
res.json(results.body.hits.hits);
} else {
res.status(400).json({ success: false });
}
我正在尝试使用 mongoosastic search() 但它没有给我结果。 数据保存在 MongoDB 和 elasticsearch 中。 下面是猫鼬模式
const mongoose = require('mongoose');
const mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;
mongoose.connect(`mongodb://localhost:27017/elasticsearch`, function(err) {
if (err) {
console.error(err);
}
console.log('connected.... unless you see an error the line before this!');
});
var ItemSchema = new Schema({
name : {type : String, es_indexed : true},
description : {type : String, es_indexed : true}
});
ItemSchema.plugin(mongoosastic, {
"host": "localhost",
"port": 9200
});
var ItemModel = mongoose.model("Item", ItemSchema);
ItemModel.createMapping((err, mapping) => {
console.log('mapping created');
});
我试图用来获得所需搜索结果的搜索查询。
app.get("/search", (req, res) => {
console.log(req.query.item);
ItemModel.search({
"multi_match": {
"fields": ["multi_field"],
"query": req.query.item,
"fuzziness": "AUTO"
}
},
function (err, results) {
if (err) {
return console.log(JSON.stringify(err, null, 4));
}
return console.log(JSON.stringify(results, null, 4));
});
});
以下是存储在 mongoDB.
上的结果{ "_id" : ObjectId("627204f3ec9b3ac2cee45c75"), "name" : "water bottle", "description" : "used to store water", "__v" : 0 }
{ "_id" : ObjectId("62720516ec9b3ac2cee45c77"), "name" : "chair", "description" : "used to sit", "__v" : 0 }
{ "_id" : ObjectId("6272060aec9b3ac2cee45c84"), "name" : "Dettol hand sanitizer", "description" : "used to sanitize hands", "__v" : 0 }
{ "_id" : ObjectId("62724a5b9c2c700433afab84"), "name" : "laptop charger", "description" : "used to charge laptop", "__v" : 0 }
Elasticsearch 中的映射
{
"items" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
elasticsearch上保存的数据集 Data saved on Elasticsearch
终于对我有用了...
const item = req.params.item;
const results = await ItemModel.search({
query_string: {
query: item
}
});
if (results.body.hits.hits) {
res.json(results.body.hits.hits);
} else {
res.status(400).json({ success: false });
}