angular.js和elasticsearch建立链接

angular.js和elasticsearch建立链接

GitHub例子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="https://cdn.bootcss.com/angular.js/1.3.2/angular.min.js"></script>
    <script src="https://cdn.bootcss.com/elasticsearch/2.4.1/elasticsearch.angular.min.js"></script>
</head>
<body ng-app="ExampleApp">

<div class="container" ng-controller="ExampleController">
    <p>hello</p>
</div>

<script>
    // App module
    //
    // The app module will contain all of the components the app needs (directives,
    // controllers, services, etc.). Since it will be using the components within
    // the elasticsearch module, define it a dependency.
    var ExampleApp = angular.module('ExampleApp', ['elasticsearch']);
    // Service
    //
    // esFactory() creates a configured client instance. Turn that instance
    // into a service so that it can be required by other parts of the application
    ExampleApp.service('client', function (esFactory) {
        return esFactory({
            host: 'http://db.irocn.com:8864',
            // apiVersion: '1.3', //可以忽略不写
            // log: 'trace'//可以忽略不写
        });
    });
    // Controller
    //
    // It requires the "client" service, and fetches information about the server,
    // it adds either an error or info about the server to $scope.
    //
    // It also requires the esFactory to that it can check for a specific type of
    // error which might come back from the client
    ExampleApp.controller('ExampleController', function ($scope, client, esFactory) {
        // 搜索文档
        var name = "陈大勇";
        client.search({
            index: 'filecoin_users',
            size: 50,
            body: {
                query: {
                    match: {
                        username : name
                    }
                }
            }
        }).then(function (resp) {
            console.log(resp);
            // $("#message").append("search data total num : " + resp.hits.total + "<br\>");
        });
    });
</script>
</body>
</html>

Jquery和elasticsearch建立连接

<!DOCTYPE html>
<html lang="UTF-8">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/elasticsearch/14.1.0/elasticsearch.jquery.js"></script>
</head>
<body>
<div id="message">

</div>
</body>

<script>
    var client = new $.es.Client({
        //连接的服务器
        hosts: 'http://db.irocn.com:8864'
    });

    // 获取状态,参数可选,可以只传递一个回调
    client.cluster.health(function (err, resp) {
        if (err) {
            $("#message").append(err.message + "<br\>");
            console.error(err.message);
        } else {
            console.dir(resp);
            $("#message").append("server status: " + resp.status + "<br\>");
        }
    });
    
    var data = {
        title: 'test!',
        content: 'It all started when...',
        date: '2018-03-11'
    };
    
    // // 建立索引, 添加数据
    // client.index({
    //     index: 'blog',
    //     type: 'post',
    //     id: 1,
    //     body: data
    // }, function (err, resp) {
    //     console.dir(resp);
    //     console.dir(err);
    //     $("#message").append("add data result: " + resp.result + "<br\>");
    // });
    
    // 搜索文档
    client.search({
        index: 'filecoin_users',
        size: 50,
        body: {
            query: {
                match: {
                    username : '杨大勇'
                }
            }
        }
    }).then(function (resp) {
        console.log(resp);
        // $("#message").append("search data total num : " + resp.hits.total + "<br\>");
    });

</script>

</html>

猜你喜欢

转载自blog.csdn.net/weixin_42651014/article/details/85289419