ลูกค้ารายนี้เลิกใช้แล้ว ตั้งแต่ Enterprise Search เวอร์ชัน 8.3.2 เป็นต้นไป เรากำลังนำผู้ใช้ไปยัง Enterprise Search Node Client ใหม่ และเลิกใช้งานไคลเอ็นต์นี้
ความพยายามในการพัฒนาโครงการนี้ของเราจะถูกจำกัดอยู่เพียงการแก้ไขข้อบกพร่องเท่านั้น การปรับปรุงในอนาคตทั้งหมดจะมุ่งเน้นไปที่ Enterprise Search Node Client
ขอบคุณ! - ยางยืด
ไคลเอนต์ Node.JS บุคคลที่หนึ่งสำหรับการสร้างประสบการณ์การค้นหาที่ยอดเยี่ยมและเกี่ยวข้องด้วย Elastic App Search
หากต้องการติดตั้งแพ็คเกจนี้ ให้รัน:
npm install @elastic/app-search-node
ลูกค้ารายนี้ได้รับเวอร์ชันและเผยแพร่ควบคู่ไปกับ App Search
เพื่อรับประกันความเข้ากันได้ ให้ใช้เวอร์ชันล่าสุดของไลบรารีนี้ภายในเวอร์ชันหลักของการใช้งาน App Search ที่เกี่ยวข้อง
ตัวอย่างเช่น สำหรับ App Search 7.3
ให้ใช้ 7.3
ของไลบรารีนี้หรือสูงกว่า แต่ไม่ใช่ 8.0
หากคุณใช้เวอร์ชัน SaaS ที่มีอยู่ใน Swiftype.com ของ App Search คุณควรใช้เวอร์ชัน 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 คุณสามารถใช้คีย์ประเภทใดก็ได้กับไคลเอ็นต์ แต่คีย์แต่ละประเภทมีขอบเขตที่แตกต่างกัน สำหรับข้อมูลเพิ่มเติมเกี่ยวกับคีย์ โปรดดูเอกสารประกอบ
เมื่อใช้เวอร์ชัน SaaS ที่มีอยู่ใน Swiftype.com ของ App Search คุณสามารถกำหนดค่าไคลเอ็นต์โดยใช้ 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 ) )
เราพยายามทำให้ไคลเอนต์รายนี้อัปเดตอยู่เสมอด้วยจุดสิ้นสุด API ที่มีอยู่ทั้งหมดจาก App Search
มี 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
ข้อมูลจำเพาะในโปรเจ็กต์นี้ใช้ node-replay เพื่อบันทึกการแข่งขัน
การติดตั้งใหม่ควรถูกบันทึกจากอินสแตนซ์ที่รันอยู่ของ 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 © ยืดหยุ่น
ขอขอบคุณผู้มีส่วนร่วมทุกคน!