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!
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
})
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With