️ Dieser Client ist veraltet️ Ab Enterprise Search Version 8.3.2 leiten wir Benutzer zum neuen Enterprise Search Node Client weiter und verwerfen diesen Client.
Unser Entwicklungsaufwand für dieses Projekt wird sich auf Fehlerbehebungen beschränken. Alle zukünftigen Verbesserungen werden sich auf den Enterprise Search Node Client konzentrieren.
Danke schön! - Elastisch
Ein Node.JS-Erstanbieter-Client zum Erstellen hervorragender, relevanter Sucherlebnisse mit Elastic App Search.
Um dieses Paket zu installieren, führen Sie Folgendes aus:
npm install @elastic/app-search-node
Dieser Client wird zusammen mit App Search versioniert und veröffentlicht.
Um die Kompatibilität zu gewährleisten, verwenden Sie die neueste Version dieser Bibliothek innerhalb der Hauptversion der entsprechenden App Search-Implementierung.
Verwenden Sie beispielsweise für App Search 7.3
7.3
dieser Bibliothek oder höher, jedoch nicht 8.0
.
Wenn Sie die auf Swiftype.com verfügbare SaaS-Version von App Search verwenden, sollten Sie die Version 7.5.x des Clients verwenden.
Bei der Verwendung dieses Clients wird davon ausgegangen, dass Sie bereits über eine Instanz von Elastic App Search verfügen, die ausgeführt wird.
Der Client wird mithilfe der Parameter baseUrlFn
und apiKey
konfiguriert.
const apiKey = 'private-mu75psc5egt9ppzuycnc2mc3'
const baseUrlFn = ( ) => 'http://localhost:3002/api/as/v1/'
const client = new AppSearchClient ( undefined , apiKey , baseUrlFn )
Notiz:
Der [apiKey]
authentifiziert Anfragen an die API. Sie können jeden Schlüsseltyp mit dem Client verwenden, jeder hat jedoch einen anderen Gültigkeitsbereich. Weitere Informationen zu Schlüsseln finden Sie in der Dokumentation.
Wenn Sie die auf Swiftype.com verfügbare SaaS-Version von App Search verwenden, können Sie den Client mit Ihrem hostIdentifier
anstelle des baseUrlFn
-Parameters konfigurieren. Den hostIdentifier
finden Sie im Menü „Anmeldeinformationen“.
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 ) )
Beachten Sie, dass diese API keinen Indizierungsfehler auslöst. Fehler werden im Antworttext pro Dokument eingefügt:
[
{ "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 ) )
Erstellen eines Suchschlüssels, der nur das Titelfeld zurückgibt.
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 ) )
Wir versuchen, diesen Client mit allen verfügbaren API-Endpunkten von App Search auf dem neuesten Stand zu halten.
Es gibt einige APIs, die möglicherweise noch nicht verfügbar sind. Für diese APIs verwenden Sie bitte den Low-Level-Client, um eine Verbindung zu einem beliebigen App Search-Endpunkt herzustellen.
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
Die Spezifikationen in diesem Projekt verwenden Node-Replay zum Erfassen von Fixtures.
Neue Geräte sollten von einer laufenden Instanz von App Search erfasst werden.
Um neue Fixtures zu erfassen, führen Sie einen Befehl wie den folgenden aus:
nvm use
HOST_IDENTIFIER=host-c5s2mj API_KEY=private-b94wtaoaym2ovdk5dohj3hrz REPLAY=record npm run test -- -g 'should create a meta engine'
Um das ein wenig aufzuschlüsseln...
HOST_IDENTIFIER
– Verwenden Sie dies, um den in Tests verwendeten gefälschten Wert mit einem tatsächlich gültigen Wert für Ihre App Search-Instanz zu überschreiben, von dem aus aufgezeichnet werden sollAPI_KEY
– Verwenden Sie dies, um den in Tests verwendeten gefälschten Wert mit einem tatsächlich gültigen Wert für Ihre App Search-Instanz zu überschreiben, von dem aus aufgezeichnet werden sollREPLAY=record
– Weist Replay an, eine neue Antwort aufzuzeichnen, falls noch keine vorhanden istnpm run test
– Führen Sie die Tests aus-- -g 'should create a meta engine'
– Beschränken Sie die Tests darauf, NUR den neuen Test auszuführen, den Sie erstellt haben, z. B. 'sollte eine Meta-Engine erstellen'Dadurch wird ein neues Gerät erstellt. Stellen Sie sicher, dass Sie dieses Gerät manuell bearbeiten, um die in diesem Gerät aufgezeichnete Host-ID und den API-Schlüssel durch die von den Tests verwendeten Werte zu ersetzen.
Sie müssen außerdem sicherstellen, dass sich Fixture entsprechend dem verwendeten Host im korrekt benannten Verzeichnis unter fixtures
befindet.
Sie wissen, ob etwas nicht stimmt, da dies zu einem Fehler führt, wenn Sie npm run test
mit einem Fehler wie dem folgenden ausführen:
Error: POST https://host-c5s2mj.api.swiftype.com:443/api/as/v1/engines refused: not recording and no network access
Wenn etwas nicht wie erwartet funktioniert, öffnen Sie bitte ein Problem.
Am besten lesen Sie die Dokumentation.
Sie können sich die Diskussionsforen der Elastic App Search-Community ansehen.
Wir freuen uns über Mitwirkende am Projekt. Bevor Sie beginnen, ein paar Anmerkungen ...
Apache 2.0 © Elastic
Vielen Dank an alle Mitwirkenden!