2017-02-22 74 views
0

为了完成一项非常简单的任务,我需要两三天的时间来锁定:获取搜索结果并填充模型。我多次用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) 
+1

Elasticsearch将返回结果为JSON,所以你不需要序列化/反序列化。只需像普通的json一样使用它。例如** res [“_ source”] **,这将根据您的搜索给您一个对象或数组。 – Hosar

回答

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"}] 
相关问题