solr

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 13 Imported by: 0

README

solr

solr library provides a simple Apache Solr HTTP client to interact with collections in a Solr instance. The library does not care whether Solr is deployed as a standalone instance or SolrCloud. This library does not provide any feature to interact with Solr data in ZooKeeper.

It provides basic functionalities such as selections and document updates.

This library is now published as open source for anyone to use, including former PotatoBeans clients.

Usage

The go-solr module adds a way to make HTTP calls to Solr with some types already provided as structs. With this client, currently select and update (in-place and direct) are supported. Deleting documents can be done using the Solr update operation.

Solr collections by default have select and update endpoints. Select is used to retrieve data while update is used to do updates such as insert, update, and delete. This module provides functionalities for all of them without advanced functionalities such as collection management (backups, replication, and so on).

Currently only JSON payload is supported (wt=json parameter is supplied automatically).

package main

import (
	"context"
	"encoding/json"
	"github.com/potatobeansco/go-solr"
)

func main() {
	// The default Solr instances are usually configured under the /solr base URL in port 8983
	c := solr.NewClient("http://127.0.0.1:8983/solr")

	resp, err := c.SelectCtx(context.TODO(), &solr.Params{
		Q    : "*:*",
		Start: 1,
		Rows : 100,
    })
	if err != nil {
		fmt.Printf("unable to make connection to Solr: %s\n", err.Error())
		os.Exit(1)
		return
    }
	
	// Parse the returned docs as JSON
	payload := make(map[string]interface{})
	err = json.Unmarshal(&payload, resp.Body.Docs)
}

More functionalities will be supported in the future.

Documentation

Index

Constants

View Source
const (
	// DefaultUpdateHandler is the default path for document update.
	// This path by default supports JSON command, and is used to do update (adding/deleting) with JSON command
	// or in-place updates.
	DefaultUpdateHandler = "/update"
	// DefaultUpdateDocsHandler is the default path for document update without JSON command.
	// It is usually used to add a new document (see also DirectUpdate).
	DefaultUpdateDocsHandler = "/update/json/docs"
	// DefaultSelectHandler is the default path to select documents.
	DefaultSelectHandler = "/select"
)

Variables

This section is empty.

Functions

func MockSelectHandler

func MockSelectHandler(returnDocuments interface{}, numFound int) http.Handler

MockSelectHandler creates a mock /select handler which will return documents stated by returnDocuments. This should be used in conjunction with httptest.Server, to mock the solr.Client HTTP Client.

func MockUpdateHandler

func MockUpdateHandler() http.Handler

MockUpdateHandler returns a basic handler that returns empty response (successful response).

Types

type Client

type Client struct {
	HttpClient *http.Client
	// contains filtered or unexported fields
}

Client creates a Solr HTTP Client. The Solr client does not add "/solr" path to the base URL. You have to include it yourself. By default, Solr requires /solr path prefix so typically your base path would be something like "http://localhost:8983/solr" and not just "http://localhost:8983". This however still depends on how Solr is deployed.

It contains an exported HTTP client which can be replaced or mocked as usual.

func NewClient

func NewClient(baseUrl string) *Client

NewClient creates a new Solr client with HTTP client set to have 10 seconds timeout. Override Client.HttpClient as you wish.

func (*Client) DirectUpdate

func (c *Client) DirectUpdate(collection string, obj interface{}) (err error)

DirectUpdate is DirectUpdateCtx without context.

func (*Client) DirectUpdateCtx

func (c *Client) DirectUpdateCtx(ctx context.Context, collection string, obj interface{}) (err error)

DirectUpdateCtx sends payload without JSON command, using default JSON docs update handler.

func (*Client) DirectUpdateHandlerCtx

func (c *Client) DirectUpdateHandlerCtx(ctx context.Context, collection, handler string, obj interface{}) (err error)

DirectUpdateHandlerCtx directly sends payload without JSON/XML command. Handler is typically /update/json/docs path, which supports direct update without JSON command.

func (*Client) InPlaceUpdate

func (c *Client) InPlaceUpdate(collection string, obj ...InPlaceUpdatePayload) (err error)

InPlaceUpdate does in-place update (see also InPlaceUpdateCtx).

func (*Client) InPlaceUpdateCtx

func (c *Client) InPlaceUpdateCtx(ctx context.Context, collection string, obj ...InPlaceUpdatePayload) (err error)

InPlaceUpdateCtx does in-place update over numerous documents supplied by InPlaceUpdatePayload. See also InPlaceUpdateHandlerCtx.

func (*Client) InPlaceUpdateHandlerCtx

func (c *Client) InPlaceUpdateHandlerCtx(ctx context.Context, collection, handler string, obj ...InPlaceUpdatePayload) (err error)

InPlaceUpdateHandlerCtx does in-place update to a part of document. It follows the example payload of atomic update found here: https://solr.apache.org/guide/solr/latest/indexing-guide/partial-document-updates.html#atomic-updates

To update a part of document, you have to submit a payload containing all commands needed to modify fields in the document. Multiple documents can be updated at once. You have to use "/update" handler that supports JSON command.

func (*Client) Select

func (c *Client) Select(collection string, params *Params) (resp *Response, err error)

Select is SelectCtx without context.

func (*Client) SelectCtx

func (c *Client) SelectCtx(ctx context.Context, collection string, params *Params) (resp *Response, err error)

SelectCtx does select query using default select handler.

func (*Client) SelectHandlerCtx

func (c *Client) SelectHandlerCtx(ctx context.Context, collection, handler string, params *Params) (resp *Response, err error)

SelectHandlerCtx makes a HTTP GET call to collection/handler, sending params encoded as query string. This method is used to search for documents in Solr.

By default, the handler to search documents is /select. Search request handler with /select endpoint is available by default when using default solrconfig.xml. If other endpoint is configured, make sure you use that endpoint as handler.

func (*Client) Update

func (c *Client) Update(collection string, obj *XmlCommand) (err error)

Update does update request with XML command (see UpdateCtx).

func (*Client) UpdateCtx

func (c *Client) UpdateCtx(ctx context.Context, collection string, obj *XmlCommand) (err error)

UpdateCtx makes an HTTP POST call to collection/handler, sending command as XML, using default update handler. UpdateCtx accepts obj which can be any structure or objects that can be marshalled into JSON. It can also be a JsonCommand object, which is a struct containing complete possible commands to send to Solr (see update handler documentation in Solr https://solr.apache.org/guide/8_8/uploading-data-with-index-handlers.html).

When using default update handler in Solr, Solr provides /update endpoint for users to submit JsonCommand payload. You can submit a JsonCommand to add or delete documents or both. If you just want to upload a document directly without having to mess with the full JsonCommand syntax, you can just put the object you want to upload and use /update/json/docs handler, which also comes with the default Solr update request handler (configured in solrconfig.xml).

Update handler with Solr command (in XML) supports multiple add and delete operations in the same command. This is useful if you want to do add and delete simultaneously. Multiple add can still be achieved with other JSON handlers like DirectUpdate.

func (*Client) UpdateHandlerCtx

func (c *Client) UpdateHandlerCtx(ctx context.Context, collection, handler string, obj *XmlCommand) (err error)

UpdateHandlerCtx makes an HTTP POST call to collection/handler, sending obj as XML. Context is passed on to http.NewRequestWithContext.

type InPlaceUpdatePayload

type InPlaceUpdatePayload map[string]interface{}

InPlaceUpdatePayload is used to do atomic update to a document. It follows the example payload of atomic update found here: https://solr.apache.org/guide/solr/latest/indexing-guide/partial-document-updates.html#atomic-updates

Your collection must have partial update enabled. ID field must be supplied. Use NewInplaceUpdatePayload to create this payload.

func NewInPlaceUpdatePayload

func NewInPlaceUpdatePayload(idFieldName, id string) InPlaceUpdatePayload

NewInPlaceUpdatePayload constructs a new in-place update payload.

func (InPlaceUpdatePayload) Add

func (i InPlaceUpdatePayload) Add(field string, values ...interface{})

Add adds "add" command to add some values to a multivalued field.

func (InPlaceUpdatePayload) AddDistinct

func (i InPlaceUpdatePayload) AddDistinct(field string, values ...interface{})

AddDistinct adds "add-distinct" command to add some distinct values to a multivalued field.

func (InPlaceUpdatePayload) Inc

func (i InPlaceUpdatePayload) Inc(field string, value float64)

Inc adds "inc" command to increment/decrement a number.

func (InPlaceUpdatePayload) Remove

func (i InPlaceUpdatePayload) Remove(field string, values ...interface{})

Remove adds "remove" command to remove some values from multivalued field.

func (InPlaceUpdatePayload) RemoveRegex

func (i InPlaceUpdatePayload) RemoveRegex(field string, values ...interface{})

RemoveRegex adds "removeregex" command to remove some values from multivalued field with regex match.

func (InPlaceUpdatePayload) Set

func (i InPlaceUpdatePayload) Set(field string, value interface{})

Set adds "set" command to replace a value. Value can be nil to remove a value.

type Params

type Params struct {
	// If Raw is not nil and contains an element, it will be used instead of the rest of these parameters,
	// and will be returned directly.
	Raw     url.Values `schema:"-"`
	Q       string     `schema:"q,omitempty"`
	Fq      []string   `schema:"fq,omitempty"`
	Sort    string     `schema:"sort,omitempty"`
	Start   int        `schema:"start,omitempty"`
	Rows    int        `schema:"rows,omitempty"`
	DefType string     `schema:"defType,omitempty"`
	QAlt    string     `schema:"q.alt,omitempty"`
	Qf      string     `schema:"qf,omitempty"`
	// CursorMark is mutually exclusive with Start.
	CursorMark string `schema:"cursorMark,omitempty"`
	Facet      bool   `schema:"facet,omitempty"`
	FacetQuery string `schema:"facet.query,omitempty"`
	FacetField string `schema:"facet.field,omitempty"`
	// Deprecated: wt can no longer be set, and will always be JSON.
	Wt string `schema:"wt,omitempty"`
}

Params holds Solr document query parameters.

func (*Params) ToQuery

func (p *Params) ToQuery() (url.Values, error)

ToQuery returns Params encoded as query parameters. If Raw is not nil and contains an element, it will be appended to all query parameters, and will replace all existing parameters.

type Response

type Response struct {
	// Header is returned after performing an action against Solr.
	// It cannot be nil.
	Header *ResponseHeader
	// Body is returned after performing a search in the database.
	// Body contains a list of documents and metadata regarding how many documents are found.
	Body           *ResponseBody
	NextCursorMark string
}

Response is a single Solr response (without error payload).

type ResponseBody

type ResponseBody struct {
	NumFound int `json:"numFound"`
	// If this is set to true, that means the real numFound is higher,
	// and the number returned is just approximation.
	NumFoundExact bool            `json:"numFoundExact"`
	Start         int             `json:"start"`
	MaxScore      float32         `json:"maxScore"`
	Docs          json.RawMessage `json:"docs"`
}

ResponseBody represents Solr response body. It is returned only with successful select operations.

type ResponseHeader

type ResponseHeader struct {
	Rf          int                    `json:"rf"`
	ZkConnected bool                   `json:"zkConnected"`
	Status      int                    `json:"status"`
	QTime       int                    `json:"QTime"`
	Params      map[string]interface{} `json:"params"`
}

ResponseHeader contains metadata about the request.

type XmlCommand

type XmlCommand struct {
	Add      []*XmlCommandAdd    `xml:"add,omitempty"`
	Delete   []*XmlCommandDelete `xml:"delete,omitempty"`
	Commit   *XmlCommandCommit   `xml:"commit,omitempty"`
	Optimize *XmlCommandOptimize `xml:"optimize,omitempty"`
}

XmlCommand is a single XML command entry. See also https://solr.apache.org/guide/solr/latest/indexing-guide/indexing-with-update-handlers.html#json-formatted-index-updates. With Go, it is not possible to support Solr JSON command. Solr requires specifying multiple "add" and "delete" fields to add or delete multiple documents. This works in Solr as JSON command is mapped into XML. In order to support multiple "add" statement we use XML instead.

The restriction of having add and delete in the same command has been lifted. It is supported in Solr and can sometimes be useful to add and delete entries atomically.

type XmlCommandAdd

type XmlCommandAdd struct {
	XMLName xml.Name `xml:"add"`
	// Doc contains document structure required to update the document, marshaled as XML.
	// Fields have to be defined manually, per XmlCommandDocField object.
	Doc          []*XmlCommandDocField `xml:"doc>field,omitempty"`
	CommitWithin int                   `xml:"commitWithin,omitempty"`
	Overwrite    bool                  `xml:"overwrite,omitempty"`
}

XmlCommandAdd represents a Solr "add" payload.

type XmlCommandCommit

type XmlCommandCommit struct {
	XMLName        xml.Name `xml:"commit"`
	WaitSearcher   bool     `xml:"waitSearcher,attr,omitempty"`
	ExpungeDeletes bool     `xml:"expungeDeletes,attr,omitempty"`
}

XmlCommandCommit represents a Solr "commit" payload.

type XmlCommandDelete

type XmlCommandDelete struct {
	XMLName xml.Name `xml:"delete"`
	Id      string   `xml:"id,omitempty"`
	Query   string   `xml:"query,omitempty"`
}

XmlCommandDelete represents a Solr "delete" payload.

type XmlCommandDocField

type XmlCommandDocField struct {
	XMLName   xml.Name    `xml:"field"`
	FieldName string      `xml:"name,attr"`
	Value     interface{} `xml:",chardata"`
}

XmlCommandDocField represents a single "field" field inside each Solr command "doc" field.

type XmlCommandOptimize

type XmlCommandOptimize struct {
	XMLName      xml.Name `xml:"optimize"`
	WaitSearcher bool     `xml:"waitSearcher,attr,omitempty"`
	MaxSegments  bool     `xml:"maxSegments,attr,omitempty"`
}

XmlCommandOptimize represents a Solr "optimize" payload.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL