Skip to content

Commit

Permalink
Merge pull request #54 from arrpee/master
Browse files Browse the repository at this point in the history
Add option for Realtime Get
  • Loading branch information
vanng822 authored Feb 19, 2020
2 parents 8e03b72 + f6632cc commit 960032f
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
18 changes: 18 additions & 0 deletions solr/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ func mockSuccessSelect(w http.ResponseWriter, req *http.Request) {
}}`)
}

func mockSuccessRealTimeGet(w http.ResponseWriter, req *http.Request) {
logRequest(req)
io.WriteString(w, `{
"response":{"numFound":1,"start":0,"docs":[
{
"id":"123",
"title":["test"],
"_version_":1658405911050321920}]
}}`)
}

func mockFailRealTimeGet(w http.ResponseWriter, req *http.Request) {
logRequest(req)
io.WriteString(w, `{}`)
}

func mockSuccessSpell(w http.ResponseWriter, req *http.Request) {
logRequest(req)
io.WriteString(w, `{
Expand Down Expand Up @@ -419,6 +435,8 @@ func mockMoreLikeThisError(w http.ResponseWriter, req *http.Request) {

func mockStartServer() {
http.HandleFunc("/success/core0/select/", mockSuccessSelect)
http.HandleFunc("/realtimegetsuccess/core0/get/", mockSuccessRealTimeGet)
http.HandleFunc("/realtimegetfail/core0/get/", mockFailRealTimeGet)
http.HandleFunc("/fail/core0/select/", mockFailSelect)
http.HandleFunc("/facet_counts/core0/select/", mockSuccessSelectFacet)
http.HandleFunc("/highlight/core0/select/", mockSuccessSelectHighlight)
Expand Down
27 changes: 27 additions & 0 deletions solr/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package solr

import (
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -269,3 +270,29 @@ func (parser *MoreLikeThisParser) Parse(resp_ *[]byte) (*SolrMltResult, error) {
}
return sr, nil
}

type RealTimeGetResultParser interface {
Parse(*[]byte) (*SolrRealtimeGetResult, error)
}

type RealTimeGetParser struct {
}

// Parse function for RealTimeGetParser
func (parser *RealTimeGetParser) Parse(resp_ *[]byte) (*SolrRealtimeGetResult, error) {
sr := &SolrRealtimeGetResult{}
jsonbuf, err := bytes2json(resp_)
if err != nil {
return sr, err
}

sr.Results = new(Collection)
if resp, ok := jsonbuf["response"].(map[string]interface{}); ok {
ParseDocResponse(resp, sr.Results)
} else {
// Should only happen when params are incorrectly defined
err = errors.New("Unable to parse realtime get response")
}

return sr, err
}
25 changes: 25 additions & 0 deletions solr/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ func (s *Search) Result(parser ResultParser) (*SolrResult, error) {
return parser.Parse(resp)
}

// RealTimeGet makes queries to the RealTimeGetHandler
// See https://lucene.apache.org/solr/guide/8_0/realtime-get.html
// This currently supports only the id (unique-key) and fq parameters
// Regardless of what actual name the unique-key field has,
// it should be specified as id or ids in the params
func (s *Search) RealTimeGet(parser RealTimeGetResultParser) (*SolrRealtimeGetResult, error) {
// the response format is different with the id and ids params
// converting id to ids lets the response be parsed by ParseDocResponse()
if id := s.QueryParams().Get("id"); id != "" {
s.QueryParams().Set("ids", id)
s.QueryParams().Del("id")
}

resp, err := s.Resource("get", s.QueryParams())
if err != nil {
return nil, err
}

if parser == nil {
parser = new(RealTimeGetParser)
}

return parser.Parse(resp)
}

// This method is for making query to MoreLikeThisHandler
// See http://wiki.apache.org/solr/MoreLikeThisHandler
func (s *Search) MoreLikeThis(parser MltResultParser) (*SolrMltResult, error) {
Expand Down
5 changes: 5 additions & 0 deletions solr/solr.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ type SolrMltResult struct {
Error map[string]interface{}
}

// SolrRealtimeGetResult is the parsed result for the RealTimeGetHandler response ie /get
type SolrRealtimeGetResult struct {
Results *Collection
}

type SolrInterface struct {
conn *Connection
}
Expand Down
42 changes: 42 additions & 0 deletions solr/solr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,48 @@ func TestSolrSuccessSelect(t *testing.T) {
}
}

func TestSolrRealTimeGetSuccess(t *testing.T) {
si, err := NewSolrInterface("http://127.0.0.1:12345/realtimegetsuccess", "core0")
if err != nil {
t.Errorf("Can not instance a new solr interface, err: %s", err)
}

q := NewQuery()
q.AddParam("id", "123")
s := si.Search(q)
res, err := s.RealTimeGet(nil)
if err != nil {
t.Errorf("cannot get %s", err)
}

if res.Results.NumFound != 1 {
t.Errorf("results.numFound expected to be 1")
}

if len(res.Results.Docs) != 1 {
t.Errorf("len of results.docs should be 1")
}

if res.Results.Docs[0].Get("id").(string) != "123" {
t.Errorf("id of document should be 123")
}
}

func TestSolrRealTimeGetFail(t *testing.T) {
si, err := NewSolrInterface("http://127.0.0.1:12345/realtimegetfail", "core0")
if err != nil {
t.Errorf("Can not instance a new solr interface, err: %s", err)
}

q := NewQuery()
q.AddParam("id", "")
s := si.Search(q)
_, err = s.RealTimeGet(nil)
if err == nil {
t.Errorf("Expected an error")
}
}

func TestSolrConnectionPostWithoutDataSucces(t *testing.T) {
_, err := HTTPPost(fmt.Sprintf("%s/collection1/schema", solrUrl), nil, nil, "", "", 0)
if err != nil {
Expand Down

0 comments on commit 960032f

Please sign in to comment.