为了一个很简单的任务,我挣扎了两三天锁定:获取搜索结果并填写模型。我已经多次使用 MongoDb 完成此任务,但我完全坚持使用 ElasticSearch,我确信它必须存在一些简单的方法,但我找不到北方。我已经阅读了很多,但我真的被困住了。
我可以看到搜索的结果。如果有人至少告诉我如何删除 _index、_type、_id 和 _score 并仅将 _source 作为数组返回,它可能会很有用。
据我所知,ElasticSearch 在设计时考虑到了速度,因此 _score 是使用 ElasticSearch 的部分原因。就我而言,我必须在我们的服务器中仅使用 ElasticSearch,因为它们允许在此类服务器中使用 ElasticSearch,并且 ElasticSearch 已用于 LogStash 和 Kibana。我对这样的挑战感到非常高兴,并且我一直在学习很多关于 ElasticSearch 的知识,但我确实需要一些关于“序列化/去沙尔化”_source 内容的知识才能继续前进。
我把我的整个代码放在下面。我想可能存在使用 Schema 和 mongoosastic 的线索,但我真的不知道该尝试什么。
你可以看到我创建了一个带有模式的模型,并添加了插件 mongoosastic。我想它可能存在某种方式来使用这种模型与 ElasticSearch 类似,就像我们很容易使用 MOngoDb 一样,但我不知道如何。
服务器.js
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var esController = require('./controllers/mycontroller');
mongoose.connect('mongodb://localhost:27017/greencard');
var app = express();
var router = express.Router();
router.route('/myexposedmethod')
.get(esController.myexposedmethod);
app.use('/api', router);
app.listen(3000);
包.json
{
"name": "greencard-dmz-es-oracle",
"main": "server.js",
"dependencies": {
"elasticsearch": "^12.1.3",
"express": "^4.1.1",
"express-session": "^1.6.1",
"mongoosastic": "^4.2.4",
"mongoose": "^3.8.8",
"reqclient": "^2.1.0"
}
}
我的控制器.js
var elasticsearch = require('elasticsearch');
var Promise = require('bluebird');
var Mymodel = require('../models/mymodel');
var query = {
"bool": {
"must": {
"term": { "my_prop1": "my" }
}
}
}
exports.myexposedmethod = function (req, res) {
var client = new elasticsearch.Client({
host: 'localhost:9200',
//log: 'trace'
});
function closeConnection() {
client.close();
}
function createIndex() {
return client.indices.create({
index: "myindex",
body: {
"mappings": {
"my_type": {
"properties": {
"my_prop1": { "type": "string" }
}
}
}
}
}
);
}
function addToIndex() {
return client.index({
index: 'myindex',
type: 'my_type',
body: {
my_prop1: 'my second string to be inserted'
},
refresh: true
});
}
function search() {
return client.search({
index: 'myindex',
type: 'my_type',
body: {
query: {
match: {
my_prop1: {
query: 'my'
}
}
}
}
}).then(function (resp) {
var hits = resp.hits.hits;
console.log(hits.length);
hits.forEach(function (hit) {
console.log("_source: ", hit._source);
})
//I know it is not going to work but may express what I am locking for
//var mymodel_result = JSON.stringify({
// mymodel: hits._source
//});
//the return to http://localhost:3000/api/myexposedmethod is
/*[
{
"_index": "myindex",
"_type": "my_type",
"_id": "AVpmQ4GbDU4zGDgLLeeR",
"_score": 0.16948202,
"_source": {
"my_prop1": "my first string to be inserted"
}
},
{
"_index": "myindex",
"_type": "my_type",
"_id": "AVpmXus8DU4zGDgLLeeU",
"_score": 0.16948202,
"_source": {
"my_prop1": "my second string to be inserted"
}
}
]*/
//but I want something like
//[{"my_prop1": "my first string to be inserted"},{"my_prop1": "my second string to be inserted"}]
//or
//[{"mymodel": "my first string to be inserted"},{"mymodel": "my second string to be inserted"}]
return res.json(hits);
}, function (err) {
console.trace(err.message);
});
}
Promise.resolve()
//.then(createIndex)
//.then(addToIndex)
.then(search)
.then(closeConnection)
;
};
mymodel.js 启发了我使用 MongoDb 和 mongooastic 的方式
var mongoose = require('mongoose'),
mongoosastic = require('mongoosastic'),
Schema = mongoose.Schema
var myschema = new Schema(
{ my_prop1: String }
)
myschema.plugin(mongoosastic)