爲了完成一項非常簡單的任務,我需要兩三天的時間來鎖定:獲取搜索結果並填充模型。我多次用MongoDb完成了這項任務,但我完全堅持使用ElasticSearch,並且我確信它必須存在一些簡單的方法,但我無法找到北方。我讀了很多,但我很困難。如何使用NodeJs和ElastiSearch創建包含hits._source列表的json字符串
我可以看到搜索結果。如果有人至少告訴我如何去掉_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 inspirid在我如何使用MongoDB的與工作加mongooastic
var mongoose = require('mongoose'),
mongoosastic = require('mongoosastic'),
Schema = mongoose.Schema
var myschema = new Schema(
{ my_prop1: String }
)
myschema.plugin(mongoosastic)
Elasticsearch將返回結果爲JSON,所以你不需要序列化/反序列化。只需像普通的json一樣使用它。例如** res [「_ source」] **,這將根據您的搜索給您一個對象或數組。 – Hosar