javascript - vuejs using elasticsearch api methods -
i have vuejs script , need use elasticsearch api method.
// ./main.js var vue = require('vue');  vue.use(require('vue-resource'));  import es './elasticsearch.js';  new vue({      el: 'body',      methods: {         search: function() {             // should call es.search...         }     } }); and elasticsearch script:
// ./elasticsearch.js var es = require('elasticsearch');  var client = new es.client({   host: 'localhost:9200'   ,log: 'trace' });  client.search({   index: 'my_index',   type: 'my_type',   body: {     fields: {},     query: {       match: {         file_content: 'search_text'       }     }   } }).then(function (resp) {     var hits = resp.hits.hits; }, function (err) {     console.trace(err.message); }); so, in method search in main.js should call client.search , send text searched in server (_search_text_).
how bind it? or how use elasticsearch object inside vuejs method?
thanks!
your elasticsearch.js file not configured correctly module: import es './elasticsearch' won't because file not export anything.
it should more this:
// ./elasticsearch.js var es = require('elasticsearch');  var client = new es.client({   host: 'localhost:9200'   ,log: 'trace' });  function search (myindex, mytype, searchtext)   return client.search({     index: myindex,     type: mytype,     body: {       fields: {},       query: {         match: {           file_content: searchtext         }       }     }   }).then(function (resp) {       return hits = resp.hits.hits;   }, function (err) {     console.trace(err.message); });  export { search } we define function named search , export it. note inxluded return statements return promise , result function.
then in main.js can import name, , use it:
// ./main.js var vue = require('vue');  vue.use(require('vue-resource'));  import { search } './elasticsearch.js';  new vue({      el: 'body',      methods: {         search: function() {             var result = search('someindex', 'sometype', 'search text here' ).then(function(res) {   // result   })         }     } }); 
Comments
Post a Comment