我为了一项非常简单的任务而奋斗了两三天:获取搜索结果并填写模型。我已经用MongoDb做了很多次这个任务,但我完全坚持使用ElasticSearch,我确信它必须存在一些简单的方法,但我找不到北方。我已经阅读了很多,但我真的被卡住了。
我可以看到搜索结果。如果有人至少告诉我如何带走_index,_type,_id和_score并仅将_source作为数组返回它可能会有用。
据我所知,据我所知,ElasticSearch的设计考虑了速度,因此_score是使用ElasticSearch的部分原因。在我的情况下,我必须在我们的服务器中只使用ElasticSearch,因为它们允许在这样的服务器中使用ElasticSearch,而ElasticSearch已经用于LogStash和Kibana。我对这样的挑战感到非常高兴,并且我已经学习了很多关于ElasticSearch的知识,但我确实需要一些北方的"序列化/解决方案" _source内容按顺序前进。
我把我的整个代码放在下面。我想可能存在使用Schema和mongoosastic的线索,但我真的没有想法要尝试什么。
你可以看到我用模式创建了一个模型,并添加了mongoosastic插件。我想这可能存在某种方式使用ElasticSearch这样的模型,就像我们轻松使用MOngoDb一样,但我不知道如何。
Server.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);
的package.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"
}
}
mycontroller.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)
答案 0 :(得分:0)
您只需将resp.hits.hits
数组映射到符合您喜欢的数组。
var newArray = resp.hits.hits.map(function(hit) {
return hit._source;
});
newArray
将包含resp.hits.hits数组中的所有_source字段,因此它将如下所示:
[{"my_prop1": "my first string to be inserted"},{"my_prop1": "my second string to be inserted"}]