solr-ez

A library to make and execute apache solr queries to a custom URL

Usage no npm install needed!

<script type="module">
  import solrEz from 'https://cdn.skypack.dev/solr-ez';
</script>

README

Ez Solr

This is a library to make and execute Apache Solr queries to a custom public URL

Installing

npm install --save solr-ez

Usage

Lets pretend that we have an Apache Solr instance in running on localhost:8983, and the url for select queries is http://localhost:8983/solr/dev/select

Constructing the class

Initializing with a url string

const Solr = require("solr-ez");
// The first argument specifies where to find the server,
// and the second argument is the path to the select endpoint
const client = new Solr("http://localhost:8983", "solr/dev/select");

Initializing with a server Object

const Solr = require("solr-ez");
const server = {
    // This is the host the server is running on, in this case it is localhost
    host: "localhost",
    // The port to connect to, by default it is 8983
    port: 8983,
    // Whether to use http or https, anything else will fallback to http
    protocol: "http"
}
// Here one again we defined the server in the first agument
// and the path to the select endpoint in the second
const client = new Solr(server, "solr/dev/select");

Making a query

Getting the Query Object

const query = client.newQuery();

Building the query

There are 3 function that are part of the Query class and that can be used to build the final query

Setting up the actual query

For this we will use the setQuery function. This function takes one argument with an object, where the keys are the fields in the solr index, and the values are the query you wish to execute on those index fields.

Lets say we have a field called heading in our Solr index, and we want to have Solr return all the documents which contain the word duck anywhere in the heading and also where a field called type is equal to poultry

query.setQuery({
    "heading": "*duck*",
    "type": "poultry",
})
Setting up index filter

For this we will use the setFilter funtion, this function take one argument with an array of field list arguments

Lets say we want to list only heading and type, but heading should be displayed as head in our return data

query.setFilter([
    "head:heading",
    "type",
])
Setting up the size of the result set

For this we will need to use the setSize function, this function takes one argument with an object with 2 optional keys:

In this example we will request the first 20 documents starting from the 2nd one

query.setSize({
    start: 2,
    rows: 20
})
Sorting the resultSet

For this we will use the setSort function, this function takes one argument with an array of valid sort strings, which will be a comma separated list later

Let's say we want to sort the result by the heading field in ascending order

query.setSort([
    "heading asc"
])
Grouping the resultset

For this we will use the setGrouping function, it takes one argument with an object that may contain two fields:

  • field : Field to group values by
  • limit : How many results should be shown in each group, Solr's default is 1

Lets say we want to group by field heading and show 15 results in each group.

query.setGroup({
    field: "heading",
    limit: 15
});
Getting the full URL that the GET request will be sent to
const string = query.getFullUrl()

Finally we can execute the query

For this we will use the execute function, This function uses the getFullUrl() function internally, so no need to pass any arguments. It returns a promise with the result data of the query.

query.execute()
    .then((data) => {
        console.log(data);
    })