Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
algoliasearch
Advanced tools
The algoliasearch npm package is a JavaScript client for Algolia, a hosted search API that provides a fast and accurate search experience for websites and mobile applications. The package allows developers to integrate Algolia's search capabilities into their JavaScript applications, enabling features such as full-text search, faceting, filtering, and geolocation queries.
Search
Perform a search query on an Algolia index and retrieve the results.
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
index.search('query string').then(({ hits }) => {
console.log(hits);
});
Indexing
Add or update a record in an Algolia index.
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
index.saveObject({ objectID: '1', name: 'Foo' }).then(() => {
console.log('Object indexed');
});
Configure Index Settings
Configure settings of an Algolia index to define ranking, attributes for faceting, etc.
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
index.setSettings({
searchableAttributes: ['name', 'description'],
customRanking: ['desc(popularity)']
}).then(() => {
console.log('Settings updated');
});
Manage Indices
List all indices in your Algolia application and manage them.
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
client.listIndices().then(({ items }) => {
console.log(items);
});
Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. The elasticsearch npm package is the official Elasticsearch client for Node.js. It provides similar search capabilities but is typically self-hosted, unlike Algolia which is a managed service.
Solr-node is a client library for interacting with Apache Solr, which is an open-source search platform. Similar to Algolia, it provides full-text search, but it requires self-hosting and manual scaling, whereas Algolia offers a fully managed service.
Typesense is an open-source, typo-tolerant search engine that provides a similar developer experience to Algolia. The typesense npm package allows you to integrate Typesense into your JavaScript applications. It is designed to be easy to use and deploy, offering an alternative to Algolia with a focus on simplicity and speed.
This Javascript client let you easily use the Algolia Search API in a browser, it is compatible with most browsers:
See this wikipedia page to have more details on supported browsers (we use XDomainRequest for IE8 and IE9).
Algolia Search is a search API that provides hosted full-text, numerical and faceted search. Algolia’s Search API makes it easy to deliver a great search experience in your apps & websites providing:
To setup your project, follow these steps:
algoliasearch.min.js
client.addUserKey(["search"])
. <script src="algoliasearch.min.js"></script>
<script>
client = new AlgoliaSearch('ApplicationID', 'API-Key');
...
First, index some data. For example, you can use the command line client quick start to index the 500 contacts sample.
You can then update the example/autocomplete.html
file with your ApplicationID
, API-Key
and index name
to test the autocomplete feature. This version is based on typeahead.js version 0.9.3 with a small patch to allow usage of Algolia JS client.
You can also update the example/instantsearch.html
file with your ApplicationID
, API-Key
and index name
to test an instant-search example.
All API calls will return the result in a callback that takes two arguments:
content.message
)To perform a search, you just need to initialize the index and perform a call to the search function.
You can use the following optional arguments:
page=9
"name,address"
), you can also use a string array encoding (for example ["name","address"]
). By default, all attributes are retrieved. You can also use *
to retrieve all values when an attributesToRetrieve setting is specified for your index.["name","address"]
). If an attribute has no match for the query, the raw value is returned. By default all indexed text attributes are highlighted. You can use *
if you want to highlight all textual attributes. Numerical attributes are not highlighted. A matchLevel is returned for each highlighted attribute and can contain:attributeName:nbWords
). Attributes are separated by a comma (Example: attributesToSnippet=name:10,content:10
). attributesToSnippet: ["name:10","content:10"]
). By default no snippet is computed.aroundLatLng=47.316669,5.016670
).{"_geoloc":{"lat":48.853409, "lng":2.348800}}
)insideBoundingBox=47.3165,4.9665,47.3424,5.0201
).{"_geoloc":{"lat":48.853409, "lng":2.348800}}
)attributeName
followed by operand
followed by value
. Supported operands are <
, <=
, =
, >
and >=
.
You can have multiple conditions on one attribute like for example numericFilters=price>100,price<1000
. You can also use a string array encoding (for example numericFilters: ["price>100","price<1000"]
).tags=tag1,(tag2,tag3)
means tag1 AND (tag2 OR tag3). You can also use a string array encoding, for example tagFilters: ["tag1",["tag2","tag3"]]
means tag1 AND (tag2 OR tag3).{"_tags":["tag1","tag2"]}
).attributeName:value
. For example: facetFilters=category:Book,author:John%20Doe
. You can also use a string array encoding (for example ["category:Book","author:John%20Doe"]
)."category,author"
). You can also use a JSON string array encoding (for example ["category","author"]
). Only attributes that have been added in attributesForFaceting index setting can be used in this parameter. You can also use *
to perform faceting on all attributes specified in attributesForFaceting.index = client.initIndex('contacts');
index.search('query string', function(success, content) {
for (var h in content.hits) {
console.log('Hit(' + content.hits[h].objectID + '): ' + content.hits[h].toString());
}
});
index.search('query string', function(success, content) {
for (var h in content.hits) {
console.log('Hit(' + content.hits[h].objectID + '): ' + content.hits[h].toString());
}
}, {'attributesToRetrieve': 'firstname,lastname', 'hitsPerPage': 50});
The server response will look like:
{
"hits": [
{
"firstname": "Jimmie",
"lastname": "Barninger",
"objectID": "433",
"_highlightResult": {
"firstname": {
"value": "<em>Jimmie</em>",
"matchLevel": "partial"
},
"lastname": {
"value": "Barninger",
"matchLevel": "none"
},
"company": {
"value": "California <em>Paint</em> & Wlpaper Str",
"matchLevel": "partial"
}
}
}
],
"page": 0,
"nbHits": 1,
"nbPages": 1,
"hitsPerPage": 20,
"processingTimeMS": 1,
"query": "jimmie paint",
"params": "query=jimmie+paint&atributesToRetrieve=firstname,lastname&hitsPerPage=50"
}
The javascript client is dedicated to web apps searching directly from the browser. In some use-cases, it can however be interesting to perform updates to the index directly in javascript, for example in an HTML5 mobile app. Therefore, just as for other languages, the javascript client is able to add, update or delete objects, or to modify index settings.
For more details about updating an index from javascript, have a look at the algoliasearch.js source file to see details about each function.
Note: If you use the javascript client to update the index, you need to specify https
as the protocol in the client initialization:
<script src="algoliasearch.min.js"></script>
<script>
client = new AlgoliaSearch('ApplicationID', 'API-Key', 'https');
...
FAQs
A fully-featured and blazing-fast JavaScript API client to interact with Algolia API.
The npm package algoliasearch receives a total of 1,119,193 weekly downloads. As such, algoliasearch popularity was classified as popular.
We found that algoliasearch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.