매우 간단한 작업으로 2 ~ 3 일 동안 잠글 수 있습니다. 검색 결과를 얻고 모델을 작성하십시오. 나는 MongoDb로이 작업을 여러 번 해왔지만 ElasticSearch로 완성되었으므로 몇 가지 쉬운 방법이 있어야한다고 확신하지만 북쪽을 찾을 수는 없다. 나는 주변을 많이 읽었지만 나는 정말로 붙어있다.NodeJs 및 ElastiSearch를 사용하여 hits._source 목록을 사용하여 json 문자열을 만드는 방법
검색 결과를 볼 수 있습니다. 누군가 적어도 _index, _type, _id 및 _score를 제거하고 _source 만 배열로 반환하는 방법을 알려주는 경우 유용 할 수 있습니다.
ElasticSearch는 속도를 염두에두고 설계되었으므로 _score가 ElasticSearch를 사용하는 이유 중 일부라는 것을 알고 있습니다. 필자는 서버에서 ElasticSearch 만 사용할 수 있고 ElasticSearch는 이미 LogStash 및 Kibana에서 사용 중이므로 ElasticSearch 만 사용해야합니다. 나는 이러한 도전에 매우 만족하고 ElasticSearch에 대해 많은 것을 배우지 만 앞으로는 _source 콘텐츠를 "serialize/desearile"하는 북쪽이 필요합니다.
나는 전체 코드를 작성했다. 나는 Schema와 mongoosastic를 사용하는 단서가 있을지도 모른다라고 생각한다. 그러나 나는 무엇을 시용해야하는지 정말로 아이디어가 없다.
스키마가있는 모델을 만들었으며 플러그인을 추가했습니다. mongoosastic. 우리가 MOngoDb로 쉽게 할 수있는 것과 비슷한 ElasticSearch와 같은 모델을 사용하는 방법이 있을지 모르지만 어떻게해야할지 모르겠다.
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를 함께 작동하도록 사용하는 방법에 inspirid 플러스 mongooastic
var mongoose = require('mongoose'),
mongoosastic = require('mongoosastic'),
Schema = mongoose.Schema
var myschema = new Schema(
{ my_prop1: String }
)
myschema.plugin(mongoosastic)
Elasticsearch는 JSON으로 결과를 반환합니다 serialize/deserialize. 정상적인 json처럼 사용하십시오. 예 : ** res [ "_ source"] **, 검색 결과에 따라 객체 나 배열을 줄 것입니다. – Hosar