Created
March 18, 2013 16:36
-
-
Save AVVS/5188595 to your computer and use it in GitHub Desktop.
Работа с ElasticSearch из node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esc = require 'elasticsearchclient' | |
### | |
Elastic search module | |
### | |
serverOptions = | |
host: process.env.esHost || 'localhost' | |
port: 9200 | |
secure: false | |
exports.esClient = esClient = new esc serverOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Config | |
{esClient, model} = require '../conf' | |
async = require 'async' | |
#### Class | |
class DiscoverController extends require('./basicController') | |
_searchOutput: (savedData) -> | |
data = JSON.parse(savedData.join "") | |
if data.hits | |
maxScore = data.hits.max_score | |
output = data.hits.hits | |
else | |
output = [] | |
search: -> | |
query = | |
multi_match: | |
query: @context.discover_search_query || "" | |
use_dis_max: true | |
fields: ["description", "module_name^2", "owner^2", "language^1.25"] | |
options = | |
size: 100 | |
#TODO: add variable size and offset handling | |
savedData = [] | |
esClient.search('mongomodules', 'module', {query}, options) | |
.on('data', (data) => savedData.push data) | |
.on('done', => @_searchOutput savedData) | |
.on('error', (error)=> | |
return @res.json error, 500 if @req.xhr | |
@context.searchError = true | |
@index() | |
) | |
.exec() | |
module.exports = (req,res)-> | |
new DiscoverController req, res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment