SolrTS
A TypeScript client for Solr
Using Solr from JavaScript clients may be straightforward but Solr uses shorthand url parameters to specify your desires which tend to lead to hard to read search logic in your application quickly. We were in desperate need of a TypeScript library that:
- prevents url parameter concatenation
- facades knowledge of shorthand Solr/Lucene terminology
- leads to search logic that reads like a book
- does not have any dependencies
The library follows a prepare-execute pattern: you first specify different parts of the desired operation and then execute it. During execution the library will transform your input to an actual Solr url, post it to Solr and return Solr's response as a json object. You are on your own interpreting that response.
Examples
The examples intentionally left out error handling.
Management
configset
create;;solrConfigSetOperation.prepareUploadNameThatConfigsetShouldHaveInSolr, NameOfZipFileThatContainsConfigset;await solrConfigSetOperation.executeIpOrHostnameOfASolrNode, PortNumberWhereThatSolrNodeListensOn;
You can find more examples in the unit tests (configset.spec.ts)
create collection
;;solrCollectionOperation.numShards1.prepareCreateNameThatCollectionShouldHaveInSolr, NameOfConfigsetThatShouldBeUsedForCollection;await solrCollectionOperation.executeIpOrHostnameOfASolrNode, PortNumberWhereThatSolrNodeListensOn;
You can find more examples in the unit tests (collection.spec.ts)
alias
create;;solrAliasOperation.prepareCreateNameOfTheAliasThatYouWantYourCollectionToBeExposedUnder, ArrayWithOneOrMoreCollectionsThatShouldBeExposedUnderTheAliasname);await solrAliasOperation.executeIpOrHostnameOfASolrNode, PortNumberWhereThatSolrNodeListensOn;
You can find more examples in the unit tests (alias.spec.ts)
Index
add one or more objects
;;solrIndexOperation.in'AnExistingCollection'.prepareBulkArrayOfObjectsThatContainPropertiesThatCorrespondToTheSchema, ;await solrIndexOperation.executeIpOrHostnameOfASolrNode, PortNumberWhereThatSolrNodeListensOn;
delete one or more objects
;;solrIndexOperation.in'AnExistingCollection'.prepareBulk, ArrayOfObjectsThatContainTheIdOfDocumentsThatShouldBeDeleted;await solrIndexOperation.executeIpOrHostnameOfASolrNode, PortNumberWhereThatSolrNodeListensOn;
You can find more examples in the unit tests (index.spec.ts)
Search
search everything
;;solrSearchOperation.in'AnExistingCollection';;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
search for a term in all fields
;;solrSearchOperation.fornew LuceneQuery.term'OneOrMoreSearchTerms'.in'AnExistingCollection';const solrAnswer: any = await solrSearchOperation.executeIpOrHostnameOfASolrNode, PortNumberWhereThatSolrNodeListensOn;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
search for a term in a specific field
;;solrSearchOperation.fornew LuceneQuery.term'OneOrMoreSearchTerms'.in'SpecificField'.in'AnExistingCollection';;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
search with paging
;;solrSearchOperation.limitMaxNumberOfResults.offsetStartingFromResult.in'AnExistingCollection';;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
filter search results
filter on a value in a specific field
;;solrSearchOperation.in'AnExistingCollection'.filternew SearchFilterFieldNameYouWantToFilterOn.equalsValueThatAllDocumentsInResponseShouldHaveForThisField;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
filter on multiple values (or) in a specific field
;;solrSearchOperation.in'AnExistingCollection'.filternew SearchFilterFieldNameYouWantToFilterOn.orsArrayOfValuesOfWhichAllDocumentsInResponseShouldContainAtLeastOnOfForThisField;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
filter on a range of values in a specific field
;;solrSearchOperation.in'AnExistingCollection'.filternew SearchFilterFieldNameYouWantToFilterOn.fromFromValue.toToValue;if solrAnswer && solrAnswer.response && solrAnswer.response.docs
You can find more search examples in the unit tests (search.spec.ts)
Debug
View the queries that are being made by setting the env parameter DEBUG.
Shell:
DEBUG=solrts:* node YourScript
DOS:
set DEBUG=soltrs:* & node YourScript
Todo
- support for advanced search term logic (with lots of ands and ors)
- sort on multiple expressions
- dismax and edismax query parsers
- advanced cluster communication. This library does not yet make use of the cluster meta information that is maintained by Zookeeper. It just connects with the specified node which may need to redirect the search request to the host that has the data you're requesting