elasticsearch 上的自动完成/提前输入 angularjs 引导程序 [英] autocomplete/typeahead angularjs bootstrap on elasticsearch

查看:21
本文介绍了elasticsearch 上的自动完成/提前输入 angularjs 引导程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可行的解决方案,以便在 elasticsearch 服务器上使用 angularjs&bootstrap 实现自动完成/提前输入.

I was looking for a working solution to get autocomplete/typeahead with angularjs&bootstrap on elasticsearch server.

推荐答案

这是一个可行的解决方案,不是一个问题,但我想分享它希望它会有所帮助:

This is a working solution, not a question, but I want to share it hope it will help:

调用自动完成功能的html代码:

the html code to call the autocomplete function :

 <input required type="text"
   popover-trigger="focus"
   placeholder="recherche globale"
   class="form-control"
   ng-model="simplequeryInput"
   ng-model-onblur focus-on="focusMe"
   ng-click="searchSimple=true" ng-keyup="$event.keyCode == 13 ? submitSimple() : null"
   typeahead="item for item in autocomplete($viewValue) | limitTo:15 "
   typeahead-on-select="simplequeryInput=$model"
 />

  • 包含elasticsearch (v2.4.0) 脚本可用这里

    我的弹性搜索服务

    interfaceApp.service('elasticQuery', function ($rootScope,esFactory) {
      return esFactory({ host: $rootScope.elastic_host}); //'localhost:9200'
    });
    

  • angularjs 代码查询 elasticsearch :

  • angularjs code querying elasticsearch :

    'use strict';
    var searchModules = angular.module('searchModules', ['ngRoute','ngDialog']);
    searchModules.controller('searchCtrl', function (ngDialog,$scope, $http,$rootScope, elasticQuery) {
      ...
      $scope.autocomplete = function(val) {
        var keywords = [];
        keywords.push(val);
        // THIS RETURN IS VERY IMPORTANT 
        return elasticQuery.search({
          index: 'YOUR_INDEX_NAME',
          size: 15,
          body: {
            "fields" : ["T_FAMILY","T_GENUS","T_SCIENTIFICNAME"], // the fields you need
            "query" : {
            "bool" : {
              "must" : [
                {
                  "query_string" : {
                    "query" : "T_FAMILY:"+val // i want only source where FAMILY == val
                  }
                }
              ]
            }
            }
          }
        }).then(function (response) {
          for (var i in response.hits.hits) {
            var fields = (response.hits.hits[i]).fields;
            var tmpObject = fields["T_FAMILY"] +" " + fields["T_GENUS"] + " ( "+fields["T_SCIENTIFICNAME"] + " )";
            keywords.push(tmpObject);
          }
        return keywords;
        });
      }
    });
    

  • 希望能帮到你

    这篇关于elasticsearch 上的自动完成/提前输入 angularjs 引导程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆