️ 該客戶端已被棄用️ 從企業搜尋版本 8.3.2 開始,我們將使用者引導至新的企業搜尋節點用戶端並棄用此用戶端。
我們對該專案的開發工作將僅限於錯誤修復。所有未來的增強功能都將集中在企業搜尋節點客戶端。
謝謝你! - 鬆緊帶
第一方 Node.JS 用戶端,用於使用 Elastic App Search 建立出色的相關搜尋體驗。
若要安裝此軟體包,請執行:
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 版本時,您可以使用hostIdentifier
而不是baseUrlFn
參數來設定客戶端。 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 © 彈性
感謝所有貢獻者!