node-azure-search
A JavaScript client library for the Azure Search service, which works from either from Node.js or the browser. The module is browserify compatible.
This module calls the Azure Search REST API. The documentation for the API is available here.
Installation
Use npm:
$ npm install azure-search
Usage
If using from node:
var AzureSearch = require('azure-search');
var client = AzureSearch({
url: "https://XXX.search.windows.net",
key: "YYY",
version: "2016-09-01",
headers: {
"x-ms-azs-return-searchid": "true",
"Access-Control-Expose-Headers": "x-ms-azs-searchid"
}
});
If using in the browser:
<html>
<head>
<script src="azure-search.min.js"></script>
</head>
<body>
<script>
var client = AzureSearch({
url: "https://XXX.search.windows.net",
key:"YYYY"
});
</script>
</body>
</html>
Note that from the browser, you must have the corsOptions
set in the index schema, and only search
, suggest
, lookup
and count
will work.
A client object can then be used to create, update, list, get and delete indexes:
var schema = {
name: 'myindex',
fields:
[ { name: 'id',
type: 'Edm.String',
searchable: false,
filterable: true,
retrievable: true,
sortable: true,
facetable: true,
key: true },
{ name: 'description',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
key: false } ],
scoringProfiles: [],
defaultScoringProfile: null,
corsOptions: null };
client.createIndex(schema, function(err, schema){
});
client.updateIndex('myindex', schema, function(err){
});
client.getIndex('myindex', function(err, schema){
});
client.listIndexes(function(err, schemas){
});
client.getIndexStats('myindex', function(err, stats){
});
var data = {
'text': 'Text to analyze',
'analyzer': 'standard'
}
client.testAnalyzer('myindex', data, function (err, tokens) {
}
client.deleteIndex('myindex', function(err){
});
You can also add documents to the index, and search it:
var doc1 = {
"id": "document1",
"description": "this is the description of my document"
}
client.addDocuments('myindex', [doc1], function(err, results){
});
client.lookup('myindex', 'document1', function(){
});
client.count('myindex', function(err, count){
});
client.search('myindex', {search: "document", top: 10, facets: ["facet1", "facet2"]}, function(err, results){
});
client.suggest('myindex', {search: "doc"}, function(err, results){
});
You can get, create, update and delete data sources:
var options = {
name : "blob-datasource",
type : "azureblob",
credentials : { connectionString : "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy" },
container : { name : "mycontainer", query : "" }
}
client.createDataSource(options, function(err, data){
});
client.updateDataSource(options, function(err, data){
});
client.deleteDataSource("blob-datasource", function(err, data){
});
client.getDataSource("dataSourceName", function(err, data) {
});
You can also create, update, list, get, delete, run and reset indexers:
var schema = {
name: 'myindexer',
description: 'Anything',
dataSourceName: 'myDSName',
targetIndexName: 'myIndexName',
schedule: {
interval: 'PT15M',
startTime: '2016-06-01T00:00:00Z'
},
parameters: {
'maxFailedItems' : 10,
'maxFailedItemsPerBatch' : 5,
'base64EncodeKeys': false,
'batchSize': 500
}};
client.createIndexer(schema, function(err, schema){
});
client.updateIndexer('myindexer', schema, function(err){
});
client.getIndexer('myindexer', function(err, schema){
});
client.listIndexers(function(err, schemas){
});
client.getIndexerStatus('myindexer', function(err, status){
});
client.deleteIndexer('myindexer', function(err){
});
client.runIndexer('myindexer', function(err){
});
client.resetIndexer('myindexer', function(err){
});
It is also possible to work with the (currently in preview) synonym maps:
var client = require('azure-search')({
url: 'https://xxx.search.windows.net',
key: 'your key goes here',
version: '2016-09-01-Preview'
})
var schema = {
name: 'mysynonmap',
format: 'solr',
synonyms: 'a=>b\nb=>c',
}
client.createSynonymMap(schema, function(err, data) {
});
client.updateOrCreateSynonymMap('mysynonmap', schema, function(err, data) {
});
client.getSynonymMap('mysynonmap', function(err, data) {
});
client.listSynonymMaps(function (err, maps) {
})
client.deleteSynonymMap('mysynonmap', function (err) {
});
Accessing the Raw Response
The raw response body is always returned as the 3rd argument in the callback.
i.e.
client.search('myindex', {search: "document", top: 10}, function(err, results, raw){
});
Using Promises
To use promises, invoke azureSearch
as a function instead of a constructor.
i.e.
var azureSearch = require('azure-search');
azureSearch({
url: "https://XXX.search.windows.net",
key: "YYY"
})
.then(client => client.listIndexes())
.then(console.log, console.error)
If you need access to the raw response body, use callback syntax instead.
Contributing
Contributions are very welcome.
To download the dependencies:
> npm install
To build the minified JavaScript:
> npm run build
To run the tests:
> npm run test
Please note that you will have to update your clientConfiguration
and storageConnectionString
variables in order to run the tests.
License
MIT