Skip to content

Commit 960032f

Browse files
authored
Merge pull request #54 from arrpee/master
Add option for Realtime Get
2 parents 8e03b72 + f6632cc commit 960032f

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

solr/mock_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ func mockSuccessSelect(w http.ResponseWriter, req *http.Request) {
6969
}}`)
7070
}
7171

72+
func mockSuccessRealTimeGet(w http.ResponseWriter, req *http.Request) {
73+
logRequest(req)
74+
io.WriteString(w, `{
75+
"response":{"numFound":1,"start":0,"docs":[
76+
{
77+
"id":"123",
78+
"title":["test"],
79+
"_version_":1658405911050321920}]
80+
}}`)
81+
}
82+
83+
func mockFailRealTimeGet(w http.ResponseWriter, req *http.Request) {
84+
logRequest(req)
85+
io.WriteString(w, `{}`)
86+
}
87+
7288
func mockSuccessSpell(w http.ResponseWriter, req *http.Request) {
7389
logRequest(req)
7490
io.WriteString(w, `{
@@ -419,6 +435,8 @@ func mockMoreLikeThisError(w http.ResponseWriter, req *http.Request) {
419435

420436
func mockStartServer() {
421437
http.HandleFunc("/success/core0/select/", mockSuccessSelect)
438+
http.HandleFunc("/realtimegetsuccess/core0/get/", mockSuccessRealTimeGet)
439+
http.HandleFunc("/realtimegetfail/core0/get/", mockFailRealTimeGet)
422440
http.HandleFunc("/fail/core0/select/", mockFailSelect)
423441
http.HandleFunc("/facet_counts/core0/select/", mockSuccessSelectFacet)
424442
http.HandleFunc("/highlight/core0/select/", mockSuccessSelectHighlight)

solr/parser.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package solr
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
)
78

@@ -269,3 +270,29 @@ func (parser *MoreLikeThisParser) Parse(resp_ *[]byte) (*SolrMltResult, error) {
269270
}
270271
return sr, nil
271272
}
273+
274+
type RealTimeGetResultParser interface {
275+
Parse(*[]byte) (*SolrRealtimeGetResult, error)
276+
}
277+
278+
type RealTimeGetParser struct {
279+
}
280+
281+
// Parse function for RealTimeGetParser
282+
func (parser *RealTimeGetParser) Parse(resp_ *[]byte) (*SolrRealtimeGetResult, error) {
283+
sr := &SolrRealtimeGetResult{}
284+
jsonbuf, err := bytes2json(resp_)
285+
if err != nil {
286+
return sr, err
287+
}
288+
289+
sr.Results = new(Collection)
290+
if resp, ok := jsonbuf["response"].(map[string]interface{}); ok {
291+
ParseDocResponse(resp, sr.Results)
292+
} else {
293+
// Should only happen when params are incorrectly defined
294+
err = errors.New("Unable to parse realtime get response")
295+
}
296+
297+
return sr, err
298+
}

solr/search.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ func (s *Search) Result(parser ResultParser) (*SolrResult, error) {
7272
return parser.Parse(resp)
7373
}
7474

75+
// RealTimeGet makes queries to the RealTimeGetHandler
76+
// See https://lucene.apache.org/solr/guide/8_0/realtime-get.html
77+
// This currently supports only the id (unique-key) and fq parameters
78+
// Regardless of what actual name the unique-key field has,
79+
// it should be specified as id or ids in the params
80+
func (s *Search) RealTimeGet(parser RealTimeGetResultParser) (*SolrRealtimeGetResult, error) {
81+
// the response format is different with the id and ids params
82+
// converting id to ids lets the response be parsed by ParseDocResponse()
83+
if id := s.QueryParams().Get("id"); id != "" {
84+
s.QueryParams().Set("ids", id)
85+
s.QueryParams().Del("id")
86+
}
87+
88+
resp, err := s.Resource("get", s.QueryParams())
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
if parser == nil {
94+
parser = new(RealTimeGetParser)
95+
}
96+
97+
return parser.Parse(resp)
98+
}
99+
75100
// This method is for making query to MoreLikeThisHandler
76101
// See http://wiki.apache.org/solr/MoreLikeThisHandler
77102
func (s *Search) MoreLikeThis(parser MltResultParser) (*SolrMltResult, error) {

solr/solr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ type SolrMltResult struct {
100100
Error map[string]interface{}
101101
}
102102

103+
// SolrRealtimeGetResult is the parsed result for the RealTimeGetHandler response ie /get
104+
type SolrRealtimeGetResult struct {
105+
Results *Collection
106+
}
107+
103108
type SolrInterface struct {
104109
conn *Connection
105110
}

solr/solr_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ func TestSolrSuccessSelect(t *testing.T) {
102102
}
103103
}
104104

105+
func TestSolrRealTimeGetSuccess(t *testing.T) {
106+
si, err := NewSolrInterface("http://127.0.0.1:12345/realtimegetsuccess", "core0")
107+
if err != nil {
108+
t.Errorf("Can not instance a new solr interface, err: %s", err)
109+
}
110+
111+
q := NewQuery()
112+
q.AddParam("id", "123")
113+
s := si.Search(q)
114+
res, err := s.RealTimeGet(nil)
115+
if err != nil {
116+
t.Errorf("cannot get %s", err)
117+
}
118+
119+
if res.Results.NumFound != 1 {
120+
t.Errorf("results.numFound expected to be 1")
121+
}
122+
123+
if len(res.Results.Docs) != 1 {
124+
t.Errorf("len of results.docs should be 1")
125+
}
126+
127+
if res.Results.Docs[0].Get("id").(string) != "123" {
128+
t.Errorf("id of document should be 123")
129+
}
130+
}
131+
132+
func TestSolrRealTimeGetFail(t *testing.T) {
133+
si, err := NewSolrInterface("http://127.0.0.1:12345/realtimegetfail", "core0")
134+
if err != nil {
135+
t.Errorf("Can not instance a new solr interface, err: %s", err)
136+
}
137+
138+
q := NewQuery()
139+
q.AddParam("id", "")
140+
s := si.Search(q)
141+
_, err = s.RealTimeGet(nil)
142+
if err == nil {
143+
t.Errorf("Expected an error")
144+
}
145+
}
146+
105147
func TestSolrConnectionPostWithoutDataSucces(t *testing.T) {
106148
_, err := HTTPPost(fmt.Sprintf("%s/collection1/schema", solrUrl), nil, nil, "", "", 0)
107149
if err != nil {

0 commit comments

Comments
 (0)