️ このクライアントは廃止されました️ Enterprise Search バージョン 8.3.2 の時点で、ユーザーは新しい Enterprise Search Node Client に誘導され、このクライアントは非推奨になります。
このプロジェクトでの開発作業はバグ修正に限定されます。今後のすべての機能強化は、Enterprise Search Node Client に焦点を当てます。
ありがとう! - 伸縮性のある
Elastic App Search で優れた関連性の高い検索エクスペリエンスを構築するためのファーストパーティ Node.JS クライアント。
このパッケージをインストールするには、次を実行します。
npm install @elastic/app-search-node
このクライアントはバージョン管理され、App Search とともにリリースされます。
互換性を保証するには、対応する App Search 実装のメジャー バージョン内でこのライブラリの最新バージョンを使用してください。
たとえば、App Search 7.3
の場合は、このライブラリの7.3
以降を使用します8.0
は使用しません。
App Search の swiftype.com で入手可能な SaaS バージョンを使用している場合は、クライアントのバージョン 7.5.x を使用する必要があります。
このクライアントを使用する場合は、Elastic App Search のインスタンスがすでに稼働していることを前提としています。
クライアントは、 baseUrlFn
パラメーターとapiKey
パラメーターを使用して構成されます。
const apiKey = 'private-mu75psc5egt9ppzuycnc2mc3'
const baseUrlFn = ( ) => 'http://localhost:3002/api/as/v1/'
const client = new AppSearchClient ( undefined , apiKey , baseUrlFn )
注記:
[apiKey]
は API へのリクエストを認証します。クライアントでは任意のキー タイプを使用できますが、それぞれの有効範囲が異なります。キーの詳細については、ドキュメントを参照してください。
App Search の swiftype.com で入手可能な SaaS バージョンを使用する場合、 baseUrlFn
パラメーターの代わりにhostIdentifier
を使用してクライアントを構成できます。 hostIdentifier
、[資格情報] メニュー内にあります。
const AppSearchClient = require ( '@elastic/app-search-node' )
const hostIdentifier = 'host-c5s2mj'
const apiKey = 'private-mu75psc5egt9ppzuycnc2mc3'
const client = new AppSearchClient ( hostIdentifier , apiKey )
const engineName = 'favorite-videos'
const documents = [
{
id : 'INscMGmhmX4' ,
url : 'https://www.youtube.com/watch?v=INscMGmhmX4' ,
title : 'The Original Grumpy Cat' ,
body : 'A wonderful video of a magnificent cat.'
} ,
{
id : 'JNDFojsd02' ,
url : 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' ,
title : 'Another Grumpy Cat' ,
body : 'A great video of another cool cat.'
}
]
client
. indexDocuments ( engineName , documents )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error ) )
この API はインデックス作成エラーをスローしないことに注意してください。エラーはドキュメントごとに応答本文にインライン化されます。
[
{ "id" : " park_rocky-mountain " , "errors" : [] },
{
"id" : " park_saguaro " ,
"errors" : [ " Invalid field value: Value 'foo' cannot be parsed as a float " ]
}
]
const engineName = 'favorite-videos'
const documents = [
{
id : 'INscMGmhmX4' ,
title : 'Updated title'
}
]
client
. updateDocuments ( engineName , documents )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error ) )
const engineName = 'favorite-videos'
const documentIds = [ 'INscMGmhmX4' , 'JNDFojsd02' ]
client
. getDocuments ( engineName , documentIds )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
// Without paging
client
. listDocuments ( engineName )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
// With paging
client
. listDocuments ( engineName , { page : { size : 10 , current : 1 } } )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const documentIds = [ 'INscMGmhmX4' , 'JNDFojsd02' ]
client
. destroyDocuments ( engineName , documentIds )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
client
. listEngines ( { page : { size : 10 , current : 1 } } )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
client
. getEngine ( engineName )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
client
. createEngine ( engineName , { language : 'en' } )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
client
. destroyEngine ( engineName )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const query = 'cat'
const searchFields = { title : { } }
const resultFields = { title : { raw : { } } }
const options = { search_fields : searchFields , result_fields : resultFields }
client
. search ( engineName , query , options )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const searches = [
{ query : 'cat' , options : {
search_fields : { title : { } } ,
result_fields : { title : { raw : { } } }
} } ,
{ query : 'grumpy' , options : { } }
]
client
. multiSearch ( engineName , searches )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const options = {
size : 3 ,
types : {
documents : {
fields : [ 'title' ]
}
}
}
client
. querySuggestion ( engineName , 'cat' , options )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
client
. listCurations ( engineName )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
// Pagination details are optional
const paginationDetails = {
page : {
current : 2 ,
size : 10
}
}
client
. listCurations ( engineName , paginationDetails )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'
client
. getCuration ( engineName , curationId )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const newCuration = {
queries : [ 'cat blop' ] ,
promoted : [ 'Jdas78932' ] ,
hidden : [ 'INscMGmhmX4' , 'JNDFojsd02' ]
}
client
. createCuration ( engineName , newCuration )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'
// "queries" is required, either "promoted" or "hidden" is required.
// Values sent for all fields will overwrite existing values.
const newDetails = {
queries : [ 'cat blop' ] ,
promoted : [ 'Jdas78932' , 'JFayf782' ]
}
client
. updateCuration ( engineName , curationId , newDetails )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'
client
. destroyCuration ( engineName , curationId )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
client
. getSchema ( engineName )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'favorite-videos'
const schema = {
views : 'number' ,
created_at : 'date'
}
client
. updateSchema ( engineName , schema )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
タイトルフィールドのみを返す検索キーを作成します。
const publicSearchKey = 'search-xxxxxxxxxxxxxxxxxxxxxxxx'
// This name must match the name of the key above from your App Search dashboard
const publicSearchKeyName = 'search-key'
const enforcedOptions = {
result_fields : { title : { raw : { } } } ,
filters : { world_heritage_site : 'true' }
}
// Optional. See https://github.com/auth0/node-jsonwebtoken#usage for all options
const signOptions = {
expiresIn : '5 minutes'
}
const signedSearchKey = AppSearchClient . createSignedSearchKey (
publicSearchKey ,
publicSearchKeyName ,
enforcedOptions ,
signOptions
)
const baseUrlFn = ( ) => 'http://localhost:3002/api/as/v1/'
const client = new AppSearchClient ( undefined , signedSearchKey , baseUrlFn )
client . search ( 'sample-engine' , 'everglade' )
const engineName = 'my-meta-engine'
client
. createMetaEngine ( engineName , [ 'source-engine-1' , 'source-engine-2' ] )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'my-meta-engine'
client
. addMetaEngineSources ( engineName , [ 'source-engine-3' ] )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'my-meta-engine'
client
. deleteMetaEngineSources ( engineName , [ 'source-engine-3' ] )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
const engineName = 'my-meta-engine'
client
. createEngine ( engineName , {
type : 'meta' ,
source_engines : [ 'source-engine-1' , 'source-engine-2' ]
} )
. then ( response => console . log ( response ) )
. catch ( error => console . log ( error . errorMessages ) )
App Search から利用できるすべての API エンドポイントを使用して、このクライアントを最新の状態に保つよう努めます。
まだ利用できない可能性のある API がいくつかあります。これらの API の場合は、低レベル クライアントを使用して接続し、App Search エンドポイントにアクセスしてください。
const engineName = 'favorite-videos'
const options = {
query : 'cats'
}
const Client = require ( '@elastic/app-search-node/lib/client' )
const client = new Client ( 'private-mu75psc5egt9ppzuycnc2mc3' , 'http://localhost:3002/api/as/v1/' )
client . post ( `engines/ ${ encodeURIComponent ( engineName ) } /search` , options ) . then ( console . log )
npm test
このプロジェクトの仕様では、ノードリプレイを使用してフィクスチャをキャプチャします。
新しいフィクスチャは、App Search の実行中のインスタンスからキャプチャする必要があります。
新しいフィクスチャをキャプチャするには、次のようなコマンドを実行します。
nvm use
HOST_IDENTIFIER=host-c5s2mj API_KEY=private-b94wtaoaym2ovdk5dohj3hrz REPLAY=record npm run test -- -g 'should create a meta engine'
それを少し分解すると...
HOST_IDENTIFIER
- これを使用して、テストで使用される偽の値を、App Search インスタンスの記録元となる実際の有効な値でオーバーライドします。API_KEY
- これを使用して、テストで使用される偽の値を、App Search インスタンスの記録元となる実際の有効な値でオーバーライドします。REPLAY=record
- 新しい応答が存在しない場合は、新しい応答を記録するように再生に指示します。npm run test
- テストを実行します。-- -g 'should create a meta engine'
- 作成した新しいテストのみを実行するようにテストを制限します。たとえば、'メタ エンジンを作成する必要があります'これにより、新しいフィクスチャが作成されます。必ずそのフィクスチャを手動で編集して、そのフィクスチャに記録されているホスト識別子と API キーをテストで使用される値に置き換えてください。
また、使用されたホストに応じて、フィクスチャがfixtures
の下の正しい名前のディレクトリに配置されていることを確認する必要もあります。
npm run test
実行すると次のようなエラーが表示されるため、何かが正しくないことがわかります。
Error: POST https://host-c5s2mj.api.swiftype.com:443/api/as/v1/engines refused: not recording and no network access
何かが期待どおりに動作しない場合は、問題を開いてください。
最善の策は、ドキュメントを読むことです。
Elastic App Search コミュニティのディスカッション フォーラムをチェックアウトできます。
プロジェクトへの貢献者を歓迎します。始める前に、いくつか注意事項があります...
Apache 2.0 © エラスティック
貢献者の皆様、ありがとうございました!