2016-11-01 3 views
0

私はユーザーの検索クエリをelasticsearchインデックスに追加してオートコンプリートを試みています。ユーザークエリのインデックス

私はsearchServiceでこのようなESのためのJS APIを使用しています:

this.indexQuery = function (searchTerms) { 
    var userQueries = es_return.hits.hits; 
    esClient.index({ 
     index: 'query-index1', 
     type: 'autocomplete', 
     body: { 
     fields: ['suggestions'] 
     } 
    }).then(function(es_return) { 
     userQueries.push(searchTerms); 
    }, function(error) { 
     console.log(error); 
    }); 
    };//eof indexQuery 

これは、ユーザーを追加する必要がありますが、右、検索のための提出の「インデックス」にクエリを入力しましたか?私<form><button>要素で

、私は私のコントローラでng-submit="vm.search(); vm.indexQuery()"

とindexQueryを持って次のようになります。

vm.indexQuery = function(searchTerms) { 
    $http.post('localhost:9200/query-index1/autocomplete', { 
     params: { 
     suggestions: vm.searchTerms 
     } 
    }).then (function(response){ 
     console.log("success"); 
    }, function(error) { 
     console.log("error"); 
    }); 
    }; 
}; 

しかし、それだけで働いていません。どこが間違っていますか?

+0

'ng-keydown =" indexQuery($ event) "' - 'vm.indexQuery'の' searchTerms'パラメタに何も渡されていないように見えます。あなたの 'ng-model'入力が何であっても – plong0

+0

このリンクを参照することができますhttps://github.com/vhvinod/AngularJS-ElasticSearch –

+0

@ plong0、okありがとう – user3125823

答えて

0

一度理解が深まったら、実際は信じられないほど簡単でした。私がしなければならなかったのは、search()の最後に別の関数を呼び出すことでした。その関数は、searchServiceの関数を呼び出してユーザのクエリをインデックス化していました。私はまたesClient.index()関数に2つのパラメータ、opType: "index"refresh: trueを加えなければなりませんでした。その後、ケーキだった:)!

this.indexQuery = function (searchTerms) { 
esClient.index({ 
    index: 'query-index1', 
    type: 'autocomplete', 
    opType: 'index', 
    refresh: true, 
    body: { 
    fields: ['suggestions'] 
    } 
}).then(function(es_return) { 
    console.log('success'); 
}, function(error) { 
    console.log('error'); 
}); 

}

HTHは誰にでも役立ちます。

関連する問題