sf9v.github.io

Have a lot of fun!

Solr-Go 0.2 released!

Posted at — Feb 10, 2021

Contents

Introduction

Solr-Go started as a rudimentary client for interacting with Solr in Go. It was built for maximum flexibility, which sacrificed the usability.

It didn’t have a convenient way of building requests; And there are separate clients for each Solr API, which means you’ll need two clients if you want to interact with the Query and Schema API.

Below is an example of querying in version 0.1.x.

import (
    "context"

    solrquery "github.com/sf9v/solr-go/query"
)

... 
// Create a client
queryClient := solrquery.NewClient("localhost", 8983)

// Build the query manually using map of interfaces
query := map[string]interface{}{
    "query": "{!lucene df=name v=iPod}",
}

ctx := context.Background()
collection := "techproducts"

// send the query
queryResp, err := queryClient.Query(ctx, collection, query)
...

As we can see from the example above, the query is built manually. Building queries manually can be very tedious and error-prone.

What’s new?

In this release, we tried to improve the overall usability of the library by leveraging the builder pattern which resulted in a more convenient API.

It is now very easy to build complex queries. An additional improvement is that, we only have a single client, which means you only need one client to interact with all Solr APIs (query, update, collections, config, etc.).

Below is an example of querying in version 0.2.

import (
    "github.com/sf9v/solr-go"
)

...

// Create a client
baseURL := "http://solr.example.com"
collection := "techproducts"
ctx := context.Background()
client := solr.NewJSONClient(baseURL)

// Create a query
query := solr.NewQuery().
    QueryParser(
        // dismax query parser
        solr.NewDisMaxQueryParser().
            Query("'solr rocks'"),
    ).
    Queries(solr.M{
        "query_filters": []solr.M{
            {
                "#size_tag": solr.M{
                    "field": solr.M{
                        "f":     "size",
                        "query": "XL",
                    },
                },
            },
            {
                "#color_tag": solr.M{
                    "field": solr.M{
                        "f":     "color",
                        "query": "Red",
                    },
                },
            },
        },
    }).
    Facets(
        // facets
        solr.NewTermsFacet("categories").
            Field("cat").Limit(10),
        solr.NewQueryFacet("high_popularity").
            Query("popularity:[8 TO 10]"),
    ).
    Sort("score").
    Offset(1).
    Limit(10).
    Filters("inStock:true").
    Fields("name", "price")

// Send the query
queryResponse, err := client.Query(ctx, collection, query)
...

Other changes

Thanks!

We hoped that you’ll find this project useful as much as we did. As always, any suggestions and ideas on how to improve the project is welcome! Please feel free to open an issue or make a pull request.