最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - vuejs using elasticsearch api methods - Stack Overflow

programmeradmin2浏览0评论

I have a vuejs script and need to use an elasticsearch api method.

// ./main.js
var Vue = require('vue');

Vue.use(require('vue-resource'));

import ES from './elasticsearch.js';

new Vue({

    el: 'body',

    methods: {
        search: function() {
            // should call the es.search...
        }
    }
});

and the 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 the method search in main.js should call the client.search and send the text to be searched in my server (_search_text_).

How do we bind it? Or how do we use the elasticsearch object inside a vuejs method?

Thanks!

I have a vuejs script and need to use an elasticsearch api method.

// ./main.js
var Vue = require('vue');

Vue.use(require('vue-resource'));

import ES from './elasticsearch.js';

new Vue({

    el: 'body',

    methods: {
        search: function() {
            // should call the es.search...
        }
    }
});

and the 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 the method search in main.js should call the client.search and send the text to be searched in my server (_search_text_).

How do we bind it? Or how do we use the elasticsearch object inside a vuejs method?

Thanks!

Share Improve this question asked May 25, 2016 at 0:28 EvisEvis 5618 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

your elasticsearch.js file is not configured correctly as a module: import ES from './elasticsearch' won'T do anything because the file does not export anything.

it should probably look more like 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 a function named search and export it. Note That I also inxluded return statements to actually return the Promise and result from the function.

Then in main.js we can import it by that name, and use it:

// ./main.js
var Vue = require('vue');

Vue.use(require('vue-resource'));

import { search } from './elasticsearch.js';

new Vue({

    el: 'body',

    methods: {
        search: function() {
            var result = search('someindex', 'sometype', 'Search text here' ).then(function(res) {
  // do something with the result
  })
        }
    }
});

I suppose "resp.hits.hits" in your code is the search result JSON Object Array ,then you can define your vue instance like below:

// ./main.js
var Vue = require('vue');

Vue.use(require('vue-resource'));

import ES from './elasticsearch.js';

new Vue({

    el: 'body',
    data:{
       searchResults:[] //this tell vue it is an array
    }

    methods: {
        search: function() {
           var self = this;
           // call the search and get response data
           client.search(function(resp){
                self.searchResults = resp.hits.hits
           })
        }
    }

});

and in your html,you just bind DOM to searchResults,that's all.

发布评论

评论列表(0)

  1. 暂无评论