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 ¶
- func NewResponseRaw(rawBytes []byte) (responseRaw, error)
- type Document
- func (d Document) HighlightFor(field string) string
- func (d Document) HighlightsFor(field string) []string
- func (d Document) Id() string
- func (d Document) IsHighlighted(field string) bool
- func (d Document) Value(fieldName string) string
- func (d Document) ValueFloat(fieldName string) float64
- func (d Document) Values(fieldName string) []string
- type FacetField
- type FacetValue
- type Facets
- type GetParams
- type SearchParams
- type SearchResponse
- type Solr
- func (s Solr) Count() (int, error)
- func (s Solr) Delete(ids []string) error
- func (s Solr) DeleteAll() error
- func (s Solr) Get(params GetParams) (Document, error)
- func (s Solr) Post(data []map[string]interface{}) error
- func (s Solr) PostDoc(doc Document) error
- func (s Solr) PostDocs(docs []Document) error
- func (s Solr) PostOne(datum map[string]interface{}) error
- func (s Solr) PostString(data string) error
- func (s Solr) Search(params SearchParams) (SearchResponse, error)
- func (s Solr) SearchText(text string) (SearchResponse, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewResponseRaw ¶
Types ¶
type Document ¶
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 (Document) HighlightFor ¶
Returns the highlight information as a single string for a given field name.
func (Document) HighlightsFor ¶
Returns the highlights information for a given field name.
func (Document) IsHighlighted ¶
Returns true if there is highlight information for a given field name.
func (Document) Value ¶
Returns the value in a field. Concatenates multi-value fields into a single string.
func (Document) ValueFloat ¶
Returns the float value in a field.
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 ¶
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) SetAddRemoveUrls ¶
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.
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 ¶
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 ¶
The main class to drive interaction with Solr.
func New ¶
Creates a new instance of Solr. When verbose = true it will log.Printf() the HTTP requests to Solr.
func (Solr) Post ¶
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) PostOne ¶
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 ¶
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.