solr

package module
v0.0.0-...-da739e5 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2020 License: MIT Imports: 12 Imported by: 0

README

Solr

A Solr client written in Go.

This client is geared towards supporting a web user interface that queries and filters via facets. This package provides a lot of the functionality to know what facets the user is filtering on and allow the user interface to add and remove more facets to the existing search.

Project https://github.com/hectorcorrea/solrdora is an example of a complete web application using this package.

Source code

  • solr.go: the main entry point, functions Get() and Search() are defined here.
  • getParams.go: parameter for the Get() function.
  • searchParams.go: parameter for the Search() function.
  • searchResponse.go: the object used to represent the results of a Search()
  • filterQueries.go: represent the fq values passed to Solr.
  • document.go: represents a document retrieved from Solr via Search() or Get().

Examples of use (basic)

Search for documents

q := "title:\"one two three\""

solr := solr.New("http://localhost:8983/solr/your-core")
results, err := solr.SearchText(q)

log.Printf("Documents found: %d", results.NumFound)
for i, doc := range results.Documents {
  log.Printf("%d %v", i, doc)
}

Get one Solr document by ID

q := "id:123"
fl := []string{}
options := map[string]string{}
params := NewGetParams(q, fl, options)

solr := solr.New("http://localhost:8983/solr/your-core")
doc, err := solr.Get(params)

log.Printf("%v", doc)

More examples

Search for documents customizing list of fields to retrieve, facets, and other parameters.

# In a web app qs will be req.URL.Query() where req is
# the *http.Request that you get in your HTTP handler.
qs := url.Values{
  "q":  []string{"title:\"one two\""},
  "fq": []string{"subject|Geography"},
}

options := map[string]string{
  "defType": "edismax",
}

facets := map[string]string{
  "publisher": "Publisher name",
  "subject_str": "Subject",
}

params := NewSearchParamsFromQs(qs, options, facets)
params.Fl = []string{"id", "title", "authorsAll", "_version_"}

solr := solr.New("http://localhost:8983/solr/your-core")
results, err := solr.Search(params)

log.Printf("Documents found: %d", results.NumFound)
for i, doc := range results.Documents {
  log.Printf("%d %v", i, doc)
}

A full-blow example using this library can be found in the SolrDora repo.

The GoDoc documentation for this package can be found here

Documentation

Overview

Package solr provides functions to connect to Solr and make request for getting individual documents, executing searches, updating, and deleteing documents.

This package is geared towards supporting a web user interface that queries and filters Solr via facets. As such it provides functionality to handle the typical request-response workflow of a web application. For example SearchResponse provides URLs to re-execute a search and handle pagination, likewise the Facets returned in a SearchResponse include URLs to add or remove a filter for a given facet field/value.

Most basic search usage:

s := solr.New("http://localhost/solr/some-core", false)
results := s.SearchText("hello")
log.Printf("%v", results)

Search with options:

s := solr.New("http://localhost/solr/some-core", false)
qs := url.Values{
	"q": []string{"title:\"one two\""},
}
options := map[string]interface{}{
	"defType": "edismax",
}
facets := map[string]string{
	"title_str" : "Title",
}
params := NewSearchParams(qs, options, facets)
results := s.Search(params)
log.Printf("%v", results)

Search with options from a query string (req is *http.Request from a web handler)

s := solr.New("http://localhost/solr/some-core", false)
options := map[string]interface{}{}
facets := map[string]string{}
params := NewSearchParams(req.URL.Query(), options, facets)
results := s.Search(params)
log.Printf("%v", results)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewResponseRaw

func NewResponseRaw(rawBytes []byte) (responseRaw, error)

Types

type Document

type Document struct {
	Data       map[string]interface{}
	Highlights map[string][]string
}

Represents a document retrieved from Solr.

Data is a map with the field and values for each field returned by Solr.

Highlights is only populated when the document was returned from a Search (i.e. not via Get). When populated contains the field and values that matched the search.

func NewDocument

func NewDocument() Document

Created a new Document object.

func (Document) HighlightFor

func (d Document) HighlightFor(field string) string

Returns the highlight information as a single string for a given field name.

func (Document) HighlightsFor

func (d Document) HighlightsFor(field string) []string

Returns the highlights information for a given field name.

func (Document) Id

func (d Document) Id() string

Returns the value of the Id field.

func (Document) IsHighlighted

func (d Document) IsHighlighted(field string) bool

Returns true if there is highlight information for a given field name.

func (Document) Value

func (d Document) Value(fieldName string) string

Returns the value in a field. Concatenates multi-value fields into a single string.

func (Document) ValueFloat

func (d Document) ValueFloat(fieldName string) float64

Returns the float value in a field.

func (Document) Values

func (d Document) Values(fieldName string) []string

Returns all the values in a multi-value field (mimics an array of one if the field is single value)

type FacetField

type FacetField struct {
	Field  string       // Name of the field in Solr.
	Title  string       // Display title for the field.
	Order  int          // Order of this field on the Facets
	Values []FacetValue // Values returned by Solr for this field.
}

FacetField represents a single facet field definition.

type FacetValue

type FacetValue struct {
	Text      string // Value returned by Solr for this field.
	Count     int    // Number of documents that matched this field/value.
	Active    bool   // true if we are filtering by this facet value
	AddUrl    string // URL to filter by this value. See SetAddRemoveUrls()
	RemoveUrl string // URL to remove filter by this value. See SetAddRemoveUrls()
}

AddUrl and RemoveUrl are leaky abstraction since they are only needed in the user interface, but declaring them here simplify things a lot upstream.

type Facets

type Facets []FacetField

Facets is an array of FacetField definitions

func NewFacetsFromDefinitions

func NewFacetsFromDefinitions(definitions map[string]string) Facets

NewFacetsFromDefinitions creates a new Facets object from a map. Notice that only facetFields are created in this case (with a Field and Title, but no Values)

If the Title for a definition is in the form "N|xxx" N is used as the order of the facet in the list.

func (*Facets) ForField

func (facets *Facets) ForField(field string) (FacetField, bool)

func (Facets) SetAddRemoveUrls

func (facets Facets) SetAddRemoveUrls(baseUrl string)

Sets the internal AddUrl and RemoveUrl of the facet values for all the facets using the provided baseUrl.

AddUrl is a URL that can be used in NewSearchParamsFromQs() to create a search filtering by the field/value of the facet.

RemoveUrl is a URL that can be used in NewSearchParamsFromQs() to create a search not filtering by this field/value in the search.

type GetParams

type GetParams struct {
	Q       string            // Typically "id:xyz"
	Fl      []string          // Fields to fetch from Solr.
	Options map[string]string // Options to pass straight to Solr (e.g. defType: "edismax")
}

GetParams represents the parameters used to get a single Solr document.

func NewGetParams

func NewGetParams(q string, fl []string, options map[string]string) GetParams

Creates a new GetParams object

type SearchParams

type SearchParams struct {
	Q             string
	Fl            []string
	Rows          int
	Start         int
	FilterQueries filterQueries     // Values that will be passed as the fq parameter.
	Facets        Facets            // Facets to request from Solr.
	Options       map[string]string // Options to pass straight to Solr (e.g. defType: "edismax")
}

SearchParams represents the parameters used to issue a search in Solr.

func NewSearchParams

func NewSearchParams(q string, options map[string]string,
	facets map[string]string) SearchParams

NewSearchParams from a search string.

func NewSearchParamsFromQs

func NewSearchParamsFromQs(qs url.Values, options map[string]string,
	facets map[string]string) SearchParams

NewSearchParamsFromQs creates a SearchParams object from a query string. This method will automatically pickup several known parameters from the query string (q, rows, start, and fq).

qs typically an instance of req.URL.Query() from a web handler.

type SearchResponse

type SearchResponse struct {
	Params      SearchParams
	Q           string
	NumFound    int
	Start       int
	Rows        int
	Documents   []Document // Documents returned by Solr (including highlight information)
	Facets      Facets     // Facet information (field, title, and values)
	Url         string     // URL to execute this search
	UrlNoQ      string     // URL to execute this search without the Q parameter
	NextPageUrl string     // URL to get the next batch of results
	PrevPageUrl string     // URL to get the previous batch of results
	Raw         string
}

SearchResponse stores the results of a search, including the parameters used in the search, the resulting documents, and facets returned by the server. It also provides the URLs that could be submitted to NewSearchParamsFromQs() to re-execute the search without the Q parameter or to get the previous/next batch of results.

type Solr

type Solr struct {
	CoreUrl string
	Verbose bool
}

The main class to drive interaction with Solr.

func New

func New(coreUrl string, verbose bool) Solr

Creates a new instance of Solr. When verbose = true it will log.Printf() the HTTP requests to Solr.

func (Solr) Count

func (s Solr) Count() (int, error)

func (Solr) Delete

func (s Solr) Delete(ids []string) error

Deletes from Solr the documents with the IDs indicated.

func (Solr) DeleteAll

func (s Solr) DeleteAll() error

Deletes from Solr all the documents

func (Solr) Get

func (s Solr) Get(params GetParams) (Document, error)

Get fetches a single document from Solr.

func (Solr) Post

func (s Solr) Post(data []map[string]interface{}) error

Post issues an HTTP POST to Solr with data data of the documents indicated in the data parameter.

The parameter data is a plain Go map[string]interface{} object rather than an array of Document objects. The map key represents the field name and the map value the field value.

func (Solr) PostDoc

func (s Solr) PostDoc(doc Document) error

Updates a single document in Solr with the data in the document provided.

func (Solr) PostDocs

func (s Solr) PostDocs(docs []Document) error

Updates an array of documents in Solr.

func (Solr) PostOne

func (s Solr) PostOne(datum map[string]interface{}) error

Updates a single document in Solr. Uses plain Go map[string]interface{} object rather than a Document object. The map key is represents the field name and the map value the field value.

func (Solr) PostString

func (s Solr) PostString(data string) error

PostString issues an HTTP POST the `/update` handler to update or add one or more documents to Solr. The string is assummed to be a valid representation of one or more documents.

func (Solr) Search

func (s Solr) Search(params SearchParams) (SearchResponse, error)

Issues a search with the values indicated in the paramers.

func (Solr) SearchText

func (s Solr) SearchText(text string) (SearchResponse, error)

Issues a search for the text indicated. Uses the server's default values for all other Solr parameters.

Jump to

Keyboard shortcuts

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