solr

package
v1.5.15 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2022 License: Apache-2.0, MIT Imports: 8 Imported by: 3

README

go-solr

Build Status GoDoc Go Walker

Solr v4, required v4.4 if you want use all supported features.

Json only

No schema checking

Please go to http://wiki.apache.org/solr/ for how to write solr query.

Features

Search, Add, Update, Delete, Commit, Rollback, Optimize

Core admin, Schema REST API

Install

go get github.com/vanng822/go-solr/solr

Usage

package main
import (
	"github.com/vanng822/go-solr/solr"
	"fmt"
)

func main() {
  si, _ := solr.NewSolrInterface("http://localhost:8983/solr", "collection1")
  query := solr.NewQuery()
  query.Q("*:*")
  s := si.Search(query)
  r, _ := s.Result(nil)
  fmt.Println(r.Results.Docs)
}

Developers

export MOCK_LOGGING=1

for the mock logging

unset MOCK_LOGGING

to remove this environment variable

License

MIT

Documentation

Overview

Solr client in Go, core admin, add docs, update, delete, search and more

package main
import (
	"github.com/vanng822/go-solr/solr"
	"fmt"
)

func main() {
  si, _ := solr.NewSolrInterface("http://localhost:8983/solr", "collection1")
  query := solr.NewQuery()
  query.Q("*:*")
  s := si.Search(query)
  r, _ := s.Result(nil)
  fmt.Println(r.Results.Docs)
}

Index

Constants

View Source
const (
	VERSION = "0.5"
)

Variables

View Source
var MaximumSolrUrlLengthSupported = 2083

Solr imposes a limit on the size of a URL send to it using GET requests. Thus this library will switch to use to POST requests as the user query's grow up. If you need, you can charge this value, but be aware of the URL limit in your Solr distribution.

Functions

func HTTPGet

func HTTPGet(url string, headers [][]string, username, password string) ([]byte, error)

HTTPGet make a GET request to url, headers are optional

func HTTPPost

func HTTPPost(path string, data *[]byte, headers [][]string, username, password string) ([]byte, error)

HTTPPost make a POST request to path which also includes domain, headers are optional

func ParseDocResponse

func ParseDocResponse(docResponse map[string]interface{}, collection *Collection)

Types

type Collection

type Collection struct {
	Docs     []Document
	Start    int
	NumFound int
}

Holding the search result

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func NewConnection

func NewConnection(solrUrl, core string) (*Connection, error)

NewConnection will parse solrUrl and return a connection object, solrUrl must be a absolute url or path

func (*Connection) AddHeader

func (c *Connection) AddHeader(key, value string)

func (*Connection) Resource

func (c *Connection) Resource(source string, params *url.Values) (*[]byte, error)

func (*Connection) SetBasicAuth

func (c *Connection) SetBasicAuth(username, password string)

func (*Connection) SetCore

func (c *Connection) SetCore(core string)

Set to a new core

func (*Connection) Update

func (c *Connection) Update(data interface{}, params *url.Values) (*SolrUpdateResponse, error)

Update take optional params which can use to specify addition parameters such as commit=true

type CoreAdmin

type CoreAdmin struct {
	// contains filtered or unexported fields
}

func NewCoreAdmin

func NewCoreAdmin(solrUrl string) (*CoreAdmin, error)

solrUrl should look like this http://0.0.0.0:8983/solr[/admin/cores] ie /admin/cores will append automatically when calling Action

func (*CoreAdmin) Action

func (ca *CoreAdmin) Action(action string, params *url.Values) (*SolrResponse, error)

Call to admin/cores endpoint, additional params neccessary for this action can specified in params. No check is done for those params so check https://wiki.apache.org/solr/CoreAdmin for detail

func (*CoreAdmin) Get

func (ca *CoreAdmin) Get(path string, params *url.Values) (*SolrResponse, error)

Method for making GET-request to any relitive path to /admin/ such as /admin/cores or /admin/info/threads

func (*CoreAdmin) Reload

func (ca *CoreAdmin) Reload(core string) (*SolrResponse, error)

Reload a core, see https://wiki.apache.org/solr/CoreAdmin#RELOAD

func (*CoreAdmin) Rename

func (ca *CoreAdmin) Rename(core, other string) (*SolrResponse, error)

Rename a core See https://wiki.apache.org/solr/CoreAdmin#RENAME

func (*CoreAdmin) SetBasicAuth

func (ca *CoreAdmin) SetBasicAuth(username, password string)

Set basic auth in case solr require login

func (*CoreAdmin) Split

func (ca *CoreAdmin) Split(core string, targetCore ...string) (*SolrResponse, error)

Splits an index into two or more indexes See https://wiki.apache.org/solr/CoreAdmin#SPLIT Only targetCore is supported here. If you want to use "path" use Action method. Available in Solr4.3

func (*CoreAdmin) Status

func (ca *CoreAdmin) Status(core string) (*SolrResponse, error)

pass empty string as core if you want status of all cores. See https://wiki.apache.org/solr/CoreAdmin#STATUS

func (*CoreAdmin) Swap

func (ca *CoreAdmin) Swap(core, other string) (*SolrResponse, error)

Swap one core with other core. See https://wiki.apache.org/solr/CoreAdmin#SWAP

func (*CoreAdmin) Unload

func (ca *CoreAdmin) Unload(core string) (*SolrResponse, error)

Unload a core, see https://wiki.apache.org/solr/CoreAdmin#UNLOAD If you want to use those flag deleteIndex, deleteDataDir, deleteInstanceDir Please use Action-method with those params specified, like ca.Action("UNLOAD", params)

type Document

type Document map[string]interface{}

func (Document) Get

func (d Document) Get(k string) interface{}

Get returns value of a key

func (Document) Has

func (d Document) Has(k string) bool

Has check if a key exist in document

func (Document) Set

func (d Document) Set(k string, v interface{})

Set add a key/value to document

type ExtensiveResultParser

type ExtensiveResultParser struct {
}

func (*ExtensiveResultParser) Parse

func (parser *ExtensiveResultParser) Parse(resp_ *[]byte) (*SolrResult, error)

func (*ExtensiveResultParser) ParseError

func (parser *ExtensiveResultParser) ParseError(response *SolrResponse, sr *SolrResult)

func (*ExtensiveResultParser) ParseFacets

func (parser *ExtensiveResultParser) ParseFacets(response *SolrResponse, sr *SolrResult)

ParseJsonFacets will assign facets and build sr.jsonfacets if there is a facet_counts

func (*ExtensiveResultParser) ParseJsonFacets

func (parser *ExtensiveResultParser) ParseJsonFacets(response *SolrResponse, sr *SolrResult)

ParseJsonFacets will assign facets and build sr.jsonfacets if there is a facets

func (*ExtensiveResultParser) ParseResponse

func (parser *ExtensiveResultParser) ParseResponse(response *SolrResponse, sr *SolrResult) (err error)

ParseSolrResponse will assign result and build sr.docs if there is a response. If there is no response or grouped property in response it will return error

func (*ExtensiveResultParser) ParseResponseHeader

func (parser *ExtensiveResultParser) ParseResponseHeader(response *SolrResponse, sr *SolrResult)

type FireworkCollection

type FireworkCollection struct {
	Docs     *json.RawMessage
	Start    int
	NumFound int
}

Holding the search result

type FireworkResultParser

type FireworkResultParser struct {
}

func (*FireworkResultParser) Parse

func (parser *FireworkResultParser) Parse(resp *[]byte) (FireworkSolrResult, error)

type FireworkSolrResult

type FireworkSolrResult struct {
	Status  int                // status quick access to status
	Results FireworkCollection `json:"response"` // results parsed documents, basically response object
	QTime   int
	Params  map[string]string `json:"params"`

	ResponseHeader map[string]interface{}
	FacetCounts    map[string]interface{}

	Highlighting   map[string]interface{}
	Error          map[string]interface{}
	Grouped        map[string]interface{} // grouped for grouping result if grouping Results will be empty
	Stats          map[string]interface{}
	MoreLikeThis   map[string]interface{} // MoreLikeThis using Search (select) Component
	NextCursorMark string                 `json:"nextCursorMark"`
}

Parsed result for SearchHandler response, ie /select

type M

type M map[string]interface{}

Shortcut for map[string]interface{} Use where applicable

type MltResultParser

type MltResultParser interface {
	Parse(*[]byte) (*SolrMltResult, error)
}

type MoreLikeThisParser

type MoreLikeThisParser struct {
}

func (*MoreLikeThisParser) Parse

func (parser *MoreLikeThisParser) Parse(resp_ *[]byte) (*SolrMltResult, error)

type Query

type Query struct {
	// contains filtered or unexported fields
}

func NewQuery

func NewQuery() *Query

func (*Query) AddFacetPivot

func (q *Query) AddFacetPivot(f string)

f (Facet) https://wiki.apache.org/solr/SimpleFacetParameters#facet.pivot Example: category

func (*Query) AddFacetQuery

func (q *Query) AddFacetQuery(fq string)

fq (FacetQuery) https://wiki.apache.org/solr/SimpleFacetParameters#facet.query_:_Arbitrary_Query_Faceting Example: price:[* TO 500]

func (*Query) AddJsonFacet

func (q *Query) AddJsonFacet(jf string)

jf (Json facet) https://cwiki.apache.org/confluence/display/solr/JSON+Request+API#JSONRequestAPI-FacetExample Example: {avg_price:"avg(price)"}

func (*Query) AddParam

func (q *Query) AddParam(k, v string)

func (*Query) BoostFunctions

func (q *Query) BoostFunctions(bf string)

bf (Boost Functions) parameter http://wiki.apache.org/solr/DisMaxQParserPlugin Example: ord(popularity)^0.5 recip(rord(price),1,1000,1000)^0.3 Check this http://wiki.apache.org/solr/FunctionQuery for available functions

func (*Query) BoostQuery

func (q *Query) BoostQuery(bq string)

bq (Boost Query) parameter http://wiki.apache.org/solr/DisMaxQParserPlugin

func (*Query) DefType

func (q *Query) DefType(defType string)

defType http://wiki.apache.org/solr/CommonQueryParameters Example: dismax

func (*Query) FieldList

func (q *Query) FieldList(fl string)

fl (Field List ) parameter http://wiki.apache.org/solr/CommonQueryParameters Example: id,name,decsription

func (*Query) FilterQuery

func (q *Query) FilterQuery(fq string)

fq (Filter Query) http://wiki.apache.org/solr/CommonQueryParameters Example: popularity:[10 TO *]

func (*Query) Geofilt

func (q *Query) Geofilt(latitude, longitude float64, sfield string, distance float64)

geofilt - The distance filter http://wiki.apache.org/solr/SpatialSearch Output example: fq={!geofilt pt=45.15,-93.85 sfield=store d=5}

func (*Query) GetParam

func (q *Query) GetParam(k string) string

func (*Query) Q

func (qq *Query) Q(q string)

q parameter http://wiki.apache.org/solr/CommonQueryParameters Example: id:100

func (*Query) QueryFields

func (q *Query) QueryFields(qf string)

qf (Query Fields) parameter http://wiki.apache.org/solr/DisMaxQParserPlugin Example: features^20.0+text^0.3

func (*Query) RemoveParam

func (q *Query) RemoveParam(k string)

func (*Query) Rows

func (q *Query) Rows(rows int)

Rows sets value for rows which means set the limit for how many rows to return

func (*Query) SetFacetMinCount

func (q *Query) SetFacetMinCount(mc int)

mc (Facet min count) https://cwiki.apache.org/confluence/display/solr/Faceting#Faceting-Thefacet.mincountParameter Example: 5

func (*Query) SetFacetPivotMinCount

func (q *Query) SetFacetPivotMinCount(mc int)

mc (Facet pivot min count) https://wiki.apache.org/solr/SimpleFacetParameters#facet.pivot Example: 5

func (*Query) SetParam

func (q *Query) SetParam(k, v string)

func (*Query) Sort

func (q *Query) Sort(sort string)

sort parameter http://wiki.apache.org/solr/CommonQueryParameters Example: geodist() asc

func (*Query) Start

func (q *Query) Start(start int)

Start sets start value which is the offset of result

func (*Query) String

func (q *Query) String() string

type ResultParser

type ResultParser interface {
	Parse(resp *[]byte) (*SolrResult, error)
}

ResultParser is interface for parsing result from response. The idea here is that application have possibility to parse. Or defined own parser with internal data structure to suite application's need

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

func NewSchema

func NewSchema(solrUrl, core string) (*Schema, error)

NewSchema will parse solrUrl and return a schema object, solrUrl must be a absolute url or path

func (*Schema) All

func (s *Schema) All() (*SolrResponse, error)

Return entire schema, require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) DynamicFields

func (s *Schema) DynamicFields(fl string, showDefaults bool) (*SolrResponse, error)

see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) DynamicFieldsName

func (s *Schema) DynamicFieldsName(name string, showDefaults bool) (*SolrResponse, error)

see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) Fields

func (s *Schema) Fields(fl string, includeDynamic, showDefaults bool) (*SolrResponse, error)

see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) FieldsName

func (s *Schema) FieldsName(name string, includeDynamic, showDefaults bool) (*SolrResponse, error)

see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) Fieldtypes

func (s *Schema) Fieldtypes(showDefaults bool) (*SolrResponse, error)

see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) FieldtypesName

func (s *Schema) FieldtypesName(name string, showDefaults bool) (*SolrResponse, error)

see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) Get

func (s *Schema) Get(path string, params *url.Values) (*SolrResponse, error)

See Get requests in https://wiki.apache.org/solr/SchemaRESTAPI for detail

func (*Schema) Name

func (s *Schema) Name() (*SolrResponse, error)

Return name of schema, require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) Post

func (s *Schema) Post(path string, data interface{}) (*SolrUpdateResponse, error)

For modify schema, require Solr4.4, currently one can add fields and copy fields. Example: s.Post("fields", data) for adding new fields. See https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) SetBasicAuth

func (s *Schema) SetBasicAuth(username, password string)

func (*Schema) SetCore

func (s *Schema) SetCore(core string)

Set to a new core

func (*Schema) Uniquekey

func (s *Schema) Uniquekey() (*SolrResponse, error)

Require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI

func (*Schema) Version

func (s *Schema) Version() (*SolrResponse, error)

Require Solr4.3, see https://wiki.apache.org/solr/SchemaRESTAPI

type Search struct {
	Debug string
	// contains filtered or unexported fields
}

func NewSearch

func NewSearch(c *Connection, q *Query) *Search

NewSearch takes c and q as optional

func (*Search) MoreLikeThis

func (s *Search) MoreLikeThis(parser MltResultParser) (*SolrMltResult, error)

This method is for making query to MoreLikeThisHandler See http://wiki.apache.org/solr/MoreLikeThisHandler

func (*Search) QueryParams

func (s *Search) QueryParams() *url.Values

Return query params including debug and indent if Debug is set

func (*Search) QueryString

func (s *Search) QueryString() string

QueryString return a query string of all queries except wt=json

func (*Search) Resource

func (s *Search) Resource(resource string, params *url.Values) (*[]byte, error)

Wrapper for connection.Resource which will add wt=json automatically One can use this to query to /solr/{CORE}/{RESOURCE} example /solr/collection1/select This can be useful when you use an search component that is not supported in this package

func (*Search) Result

func (s *Search) Result(parser ResultParser) (*SolrResult, error)

Result will create a StandardResultParser if no parser specified. parser must be an implementation of ResultParser interface

func (*Search) SetQuery

func (s *Search) SetQuery(q *Query)

SetQuery will replace old query with new query q

func (*Search) SpellCheck

func (s *Search) SpellCheck(parser ResultParser) (*SolrResult, error)

This method is for making query to SpellCheckHandler See https://wiki.apache.org/solr/SpellCheckComponent

type SolrInterface

type SolrInterface struct {
	// contains filtered or unexported fields
}

func NewSolrInterface

func NewSolrInterface(solrUrl, core string) (*SolrInterface, error)

Return a new instance of SolrInterface

func (*SolrInterface) Add

func (si *SolrInterface) Add(docs []Document, chunk_size int, params *url.Values) (*SolrUpdateResponse, error)

Add will insert documents in batch of chunk_size. success is false as long as one chunk failed. The result in SolrUpdateResponse is summery of response from all chunks with key chunk_%d

func (*SolrInterface) Commit

func (si *SolrInterface) Commit() (*SolrUpdateResponse, error)

Commit the changes since the last commit

func (*SolrInterface) CoreAdmin

func (si *SolrInterface) CoreAdmin() (*CoreAdmin, error)

Return new instance of CoreAdmin with provided solrUrl and basic auth

func (*SolrInterface) Delete

func (si *SolrInterface) Delete(data map[string]interface{}, params *url.Values) (*SolrUpdateResponse, error)

Delete take data of type map and optional params which can use to specify addition parameters such as commit=true . Only one delete statement is supported, ie data can be { "id":"ID" } . If you want to delete more docs use { "query":"QUERY" } . Extra params can specify in params or in data such as { "query":"QUERY", "commitWithin":"500" }

func (*SolrInterface) DeleteAll

func (si *SolrInterface) DeleteAll() (*SolrUpdateResponse, error)

DeleteAll will remove all documents and commit

func (*SolrInterface) Optimize

func (si *SolrInterface) Optimize(params *url.Values) (*SolrUpdateResponse, error)

func (*SolrInterface) Ping

func (si *SolrInterface) Ping() (status string, qtime int, err error)

Return 'status' and QTime from solr, if everything is fine status should have value 'OK' QTime will have value -1 if can not determine

func (*SolrInterface) Rollback

func (si *SolrInterface) Rollback() (*SolrUpdateResponse, error)

Rollback rollbacks all add/deletes made to the index since the last commit. This should use with caution. See https://wiki.apache.org/solr/UpdateXmlMessages#A.22rollback.22

func (*SolrInterface) Schema

func (si *SolrInterface) Schema() (*Schema, error)

Return new instance of Schema with provided solrUrl and basic auth

func (*SolrInterface) Search

func (si *SolrInterface) Search(q *Query) *Search

Return a new instace of Search, q is optional and one can set it later

func (*SolrInterface) SetBasicAuth

func (si *SolrInterface) SetBasicAuth(username, password string)

SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password. See http://golang.org/pkg/net/http/#Request.SetBasicAuth

func (*SolrInterface) SetCore

func (si *SolrInterface) SetCore(core string)

Set to new core, this is just wrapper to Connection.SetCore which mean it will affect all places that use this Connection instance

func (*SolrInterface) Update

func (si *SolrInterface) Update(data interface{}, params *url.Values) (*SolrUpdateResponse, error)

Update take data of type interface{} and optional params which can use to specify addition parameters such as commit=true

type SolrMltResult

type SolrMltResult struct {
	Status         int         // status quick access to status
	Results        *Collection // results parsed documents, basically response object
	Match          *Collection // Documents for match section
	ResponseHeader map[string]interface{}
	Error          map[string]interface{}
}

Parsed result for MoreLikeThisHandler response, ie /mlt

type SolrResponse

type SolrResponse struct {
	Status   int
	Response map[string]interface{}
}

type SolrResult

type SolrResult struct {
	Status         int         // status quick access to status
	Results        *Collection // results parsed documents, basically response object
	QTime          int
	ResponseHeader map[string]interface{}
	Facets         map[string]interface{}
	JsonFacets     map[string]interface{}
	FacetCounts    map[string]interface{}
	Highlighting   map[string]interface{}
	Error          map[string]interface{}
	Grouped        map[string]interface{} // grouped for grouping result if grouping Results will be empty
	Stats          map[string]interface{}
	MoreLikeThis   map[string]interface{} // MoreLikeThis using Search (select) Component
	SpellCheck     map[string]interface{} // SpellCheck using SpellCheck (spell) Component
	NextCursorMark string
}

Parsed result for SearchHandler response, ie /select

type SolrUpdateResponse

type SolrUpdateResponse struct {
	Success bool
	Result  map[string]interface{}
}

type StandardResultParser

type StandardResultParser struct {
}

func (*StandardResultParser) Parse

func (parser *StandardResultParser) Parse(resp_ *[]byte) (*SolrResult, error)

func (*StandardResultParser) ParseError

func (parser *StandardResultParser) ParseError(response *SolrResponse, sr *SolrResult)

func (*StandardResultParser) ParseFacetCounts

func (parser *StandardResultParser) ParseFacetCounts(response *SolrResponse, sr *SolrResult)

ParseFacetCounts will assign facet_counts to sr if there is one. No modification done here

func (*StandardResultParser) ParseHighlighting

func (parser *StandardResultParser) ParseHighlighting(response *SolrResponse, sr *SolrResult)

ParseHighlighting will assign highlighting to sr if there is one. No modification done here

func (*StandardResultParser) ParseMoreLikeThis

func (parser *StandardResultParser) ParseMoreLikeThis(response *SolrResponse, sr *SolrResult)

Parse moreLikeThis if there is in response

func (*StandardResultParser) ParseResponse

func (parser *StandardResultParser) ParseResponse(response *SolrResponse, sr *SolrResult) (err error)

ParseSolrResponse will assign result and build sr.docs if there is a response. If there is no response or grouped property in response it will return error

func (*StandardResultParser) ParseResponseHeader

func (parser *StandardResultParser) ParseResponseHeader(response *SolrResponse, sr *SolrResult)

func (*StandardResultParser) ParseSpellCheck

func (parser *StandardResultParser) ParseSpellCheck(response *SolrResponse, sr *SolrResult)

Parse moreLikeThis if there is in response

func (*StandardResultParser) ParseStats

func (parser *StandardResultParser) ParseStats(response *SolrResponse, sr *SolrResult)

Parse stats if there is in response

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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