solrg

package module
v0.0.0-...-2ace570 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

README

Solrg

Solrg is a simple Go client for Apache Solr modeled after Solrj

Features

  • Built-in load balancing (optional) - Uses ZooKeeper state to discover and route requests
  • Simple API for the most commonly used Solr operations.

Example Usage

// Create a solr client
sc, err := solrg.NewSolrClient("localhost:9983")

// Create a collection
err = sc.CreateCollection("test", 1, 2, time.Second*180)

// Create a couple of documents
doc := solrg.NewSolrDocument("1")
doc.SetField("test_txt", []string{"test1", "test2", "test3"})
doc.SetField("test_s", []string{"test1"})

doc2 := solrg.NewSolrDocument("2")
doc2.SetField("test_txt", []string{"test3", "test4", "test5"})
doc2.SetField("test_s", []string{"test2"})

// Put them in a DocumentCollection
col := solrg.NewSolrDocumentCollection()
col.AddDoc(doc)
col.AddDoc(doc2)

// Index them
err = sc.PostDocs(&docs, "test")

// Commit changes
err = sc.Commit("test")

Alternatively, if you want to connect directly to a Solr node, you can create a client using this:

sc, err := solrg.NewDirectSolrClient("localhost:8983/solr")

Querying

params := solrg.SolrParams{
    Q:          "*:*",
    Facet:      "true",
    FacetField: []string{"test_s", "test_txt"},
}
resp, err := sc.Query("test", "select", &params, 10*time.Second)

The Solr Response is serialized to structs located in https://github.com/ezeev/solrg/blob/master/solrresp.go

For a full list of available request params, see https://github.com/ezeev/solrg/blob/master/solrparams.go. The current SolrParams struct doesn't cover every available request param by a long shot. I'll be adding more as I need them. PRs welcome.

Roadmap

  • Field collapsing
  • More admin ops (schema crud, etc..)
  • TBD...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CollectionsAPIResponse

type CollectionsAPIResponse struct {
	ResponseHeader struct {
		Status int `json:"status"`
		QTime  int `json:"QTime"`
	} `json:"responseHeader"`
	Success struct {
		One921681668983Solr struct {
			ResponseHeader struct {
				Status int `json:"status"`
				QTime  int `json:"QTime"`
			} `json:"responseHeader"`
			Core string `json:"core"`
		} `json:"192.168.1.66:8983_solr"`
		One921681667574Solr struct {
			ResponseHeader struct {
				Status int `json:"status"`
				QTime  int `json:"QTime"`
			} `json:"responseHeader"`
			Core string `json:"core"`
		} `json:"192.168.1.66:7574_solr"`
	} `json:"success"`
	Warning                        string `json:"warning"`
	OperationCreateCausedException string `json:"Operation create caused exception:"`
	Exception                      struct {
		Msg     string `json:"msg"`
		RspCode int    `json:"rspCode"`
	} `json:"exception"`
	Error struct {
		Metadata []string `json:"metadata"`
		Msg      string   `json:"msg"`
		Code     int      `json:"code"`
	} `json:"error"`
}

CollectionsAPIResponse solr collections api response struct

type FacetField

type FacetField []string

func (*FacetField) UnmarshalJSON

func (ff *FacetField) UnmarshalJSON(data []byte) error

UnmarshalJSON an override because the Solr response can return a single value or a slice depending on how many facet fields are in the request. This gurantees that the FacetField part of the response serializes to our static type (a string slice)

type FieldTypesResponse

type FieldTypesResponse struct {
	ResponseHeader struct {
		Status int `json:"status"`
		QTime  int `json:"QTime"`
	} `json:"responseHeader"`
	FieldTypes []struct {
		Name          string `json:"name"`
		Class         string `json:"class"`
		IndexAnalyzer struct {
			Tokenizer struct {
				Class string `json:"class"`
			} `json:"tokenizer"`
		} `json:"indexAnalyzer,omitempty"`
		QueryAnalyzer struct {
			Tokenizer struct {
				Class     string `json:"class"`
				Delimiter string `json:"delimiter"`
			} `json:"tokenizer"`
		} `json:"queryAnalyzer,omitempty"`
		SortMissingLast bool `json:"sortMissingLast,omitempty"`
		MultiValued     bool `json:"multiValued,omitempty"`
		Indexed         bool `json:"indexed,omitempty"`
		Stored          bool `json:"stored,omitempty"`
		Analyzer        struct {
			Tokenizer struct {
				Class string `json:"class"`
			} `json:"tokenizer"`
			Filters []struct {
				Class   string `json:"class"`
				Encoder string `json:"encoder"`
			} `json:"filters"`
		} `json:"analyzer,omitempty"`
		DocValues                 bool   `json:"docValues,omitempty"`
		Geo                       string `json:"geo,omitempty"`
		MaxDistErr                string `json:"maxDistErr,omitempty"`
		DistErrPct                string `json:"distErrPct,omitempty"`
		DistanceUnits             string `json:"distanceUnits,omitempty"`
		PositionIncrementGap      string `json:"positionIncrementGap,omitempty"`
		SubFieldSuffix            string `json:"subFieldSuffix,omitempty"`
		Dimension                 string `json:"dimension,omitempty"`
		AutoGeneratePhraseQueries string `json:"autoGeneratePhraseQueries,omitempty"`
	} `json:"fieldTypes"`
}

type FilterQuery

type FilterQuery []string

func (*FilterQuery) UnmarshalJSON

func (ff *FilterQuery) UnmarshalJSON(data []byte) error

type LiveNodes

type LiveNodes struct {
	Nodes      []string
	LastUpdate time.Time
}

LiveNodes struct to hold slice of live nodes and when the last time live nodes were updated

type SolrClient

type SolrClient struct {
	Connection *zk.Conn
	// contains filtered or unexported fields
}

SolrClient Solr Client struct

func NewDirectSolrClient

func NewDirectSolrClient(solrUrl string) (*SolrClient, error)

func NewSolrClient

func NewSolrClient(zksString string) (*SolrClient, error)

NewSolrClient returns a new instance of a Solr Client

func (*SolrClient) Commit

func (sc *SolrClient) Commit(collectionName string) error

Commit executes a Solr commit command

func (*SolrClient) Connect

func (sc *SolrClient) Connect(zksString string) error

Connect Connects the SolrClient instance

func (*SolrClient) CreateCollection

func (sc *SolrClient) CreateCollection(name string, numShards int, replicationFactor int, timeout time.Duration) error

CreateCollection creates a Solr collection

func (*SolrClient) DeleteByQuery

func (sc *SolrClient) DeleteByQuery(collectionName string, query string) error

DeleteByQuery deletes documents matching a Solr query

func (*SolrClient) DeleteCollection

func (sc *SolrClient) DeleteCollection(name string) error

DeleteCollection deletes a Solr collection

func (*SolrClient) FieldTypes

func (sc *SolrClient) FieldTypes(collection string) (*FieldTypesResponse, error)

func (*SolrClient) LBNodeAddress

func (sc *SolrClient) LBNodeAddress() string

LBNodeAddress Returns a node address using simple round robin LB of available nodes

func (*SolrClient) LiveSolrNodes

func (sc *SolrClient) LiveSolrNodes() (*LiveNodes, error)

LiveSolrNodes returns a slice of urls to live Solr nodes

func (*SolrClient) PostDocs

func (sc *SolrClient) PostDocs(docs *SolrDocumentCollection, targetCollection string) error

PostDocs indexes a SolrDocumentCollection

func (*SolrClient) PostStructs

func (sc *SolrClient) PostStructs(data []interface{}, targetCollection string) error

func (*SolrClient) Query

func (sc *SolrClient) Query(collection string, reqHandler string, params *SolrParams, timeout time.Duration) (*SolrSearchResponse, error)

Search executes a Solr search

type SolrCollectionExistsError

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

func (*SolrCollectionExistsError) Error

func (e *SolrCollectionExistsError) Error() string

type SolrDocument

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

SolrDocument struct the holds fields and provides methods for manipulating them

func NewSolrDocument

func NewSolrDocument(id string) SolrDocument

NewSolrDocument creates a new instance of a SolrDocument

func (*SolrDocument) Exists

func (sd *SolrDocument) Exists(name string) bool

Exists returns a bool. True = the field exists. False = the field does not exist.

func (*SolrDocument) GetField

func (sd *SolrDocument) GetField(name string) ([]string, error)

GetField returns a field from the document if it exists

func (*SolrDocument) ID

func (sd *SolrDocument) ID() string

ID returns the Id of the document

func (*SolrDocument) SetField

func (sd *SolrDocument) SetField(name string, values []string)

SetField sets the value for a field in the SolrDocument

func (*SolrDocument) SetID

func (sd *SolrDocument) SetID(id string)

SetID sets the Id of the document

func (*SolrDocument) SolrJSON

func (sd *SolrDocument) SolrJSON() (string, error)

SolrJSON returns the json representation of a solr document for indexing

type SolrDocumentCollection

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

SolrDocumentCollection holds a collection of SolrDocuments

func NewSolrDocumentCollection

func NewSolrDocumentCollection() SolrDocumentCollection

NewSolrDocumentCollection returns a new instance of a SolrDocumentCollection

func (*SolrDocumentCollection) AddDoc

func (sdc *SolrDocumentCollection) AddDoc(doc SolrDocument) error

AddDoc adds a document to the collection

func (*SolrDocumentCollection) DeleteDoc

func (sdc *SolrDocumentCollection) DeleteDoc(id string)

DeleteDoc removes a doc by id

func (*SolrDocumentCollection) GetDoc

func (sdc *SolrDocumentCollection) GetDoc(id string) SolrDocument

GetDoc returns a doc by id

func (*SolrDocumentCollection) NumDocs

func (sdc *SolrDocumentCollection) NumDocs() int

NumDocs returns the number of docs in the collection

func (*SolrDocumentCollection) SolrJSON

func (sdc *SolrDocumentCollection) SolrJSON() (string, error)

SolrJSON returns a json string representation of the doc collection ready for Solr

type SolrFacetField

type SolrFacetField struct {
	Name  string `json:"name"`
	Type  string `json:"type"`
	Value int    `json:"value"`
}

SolrFacetField holds data for facet fields from a Solr response. NOTE: you must use &json.nl=arrntv on your Solr queries for this to work

type SolrParams

type SolrParams struct {
	Q          string      `json:"q" url:"q,omitempty"`
	DefType    string      `json:"defType" url:"defType,omitempty"`
	FacetField FacetField  `json:"facet.field" url:"facet.field,omitempty"`
	JSONNl     string      `json:"json.nl" url:"json.nl,omitempty"`
	Qf         string      `json:"qf" url:"qf,omitempty"`
	Fl         string      `json:"fl" url:"fl,omitempty"`
	Rows       string      `json:"rows" url:"rows,omitempty"`
	Facet      string      `json:"facet" url:"facet,omitempty"`
	Bq         string      `json:"bq" url:"bq,omitempty"`
	Fq         FilterQuery `json:"fq" url:"fq,omitempty"`
	Sort       string      `json:"sort" url:"sort,omitempty"`
	Start      string      `json:"start" url:"start,omitempty"`
}

SolrParams hold information for a Solr request

type SolrSearchDocument

type SolrSearchDocument map[string]interface{}

SolrSearchDocument holds fields of a returned document and provides helper methods for accessing values

func (SolrSearchDocument) Float64

func (sd SolrSearchDocument) Float64(fieldName string) (float64, error)

Float64 returns a float64 field

func (SolrSearchDocument) HasField

func (sd SolrSearchDocument) HasField(fieldName string) bool

HasField returns true if the document has a specified field

func (SolrSearchDocument) Int64

func (sd SolrSearchDocument) Int64(fieldName string) (int64, error)

Int64 returns a int64 field or casts a float64 field to an int

func (SolrSearchDocument) Slice

func (sd SolrSearchDocument) Slice(fieldName string) ([]interface{}, error)

Slice returns a slice (array) field

func (SolrSearchDocument) String

func (sd SolrSearchDocument) String(fieldName string) string

String returns a string representation of a field

func (SolrSearchDocument) StringSlice

func (sd SolrSearchDocument) StringSlice(fieldName string) ([]string, error)

StringSlice returns a string slice (array) field

type SolrSearchResponse

type SolrSearchResponse struct {
	ResponseHeader struct {
		ZkConnected bool       `json:"zkConnected"`
		Status      int        `json:"status"`
		QTime       int        `json:"QTime"`
		Params      SolrParams `json:"params"`
	} `json:"responseHeader"`
	Response struct {
		NumFound int                  `json:"numFound"`
		Start    int                  `json:"start"`
		MaxScore float64              `json:"maxScore"`
		Docs     []SolrSearchDocument `json:"docs"`
	} `json:"response"`
	FacetCounts struct {
		FacetQueries struct {
		} `json:"facet_queries"`
		FacetFields map[string][]SolrFacetField `json:"facet_fields"`
		FacetRanges struct {
		} `json:"facet_ranges"`
		FacetIntervals struct {
		} `json:"facet_intervals"`
		FacetHeatmaps struct {
		} `json:"facet_heatmaps"`
	} `json:"facet_counts"`
}

SolrSearchResponse holds information about a Solr search response

Jump to

Keyboard shortcuts

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