You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2015/11/25 00:38:27 UTC
[05/13] lucy git commit: More custom Go for Searcher.
More custom Go for Searcher.
Hand-roll bindings for FetchDoc, fetchDocVec and topDocs to allow for
returning a tuple including an `error`.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/d483dd07
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/d483dd07
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/d483dd07
Branch: refs/heads/master
Commit: d483dd076c36f3e8c29d80de5ad6e401e5531ca0
Parents: e35becd
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Tue Nov 10 20:04:44 2015 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Fri Nov 20 16:43:37 2015 -0800
----------------------------------------------------------------------
go/build.go | 3 +++
go/lucy/index_test.go | 2 +-
go/lucy/search.go | 31 +++++++++++++++++++++++++++++++
go/lucy/search_test.go | 16 ++++++++++++----
4 files changed, 47 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/d483dd07/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index 5981016..2b94cc5 100644
--- a/go/build.go
+++ b/go/build.go
@@ -209,7 +209,10 @@ func specClasses(parcel *cfc.Parcel) {
searcherBinding := cfc.NewGoClass(parcel, "Lucy::Search::Searcher")
searcherBinding.SpecMethod("Hits",
"Hits(query interface{}, offset uint32, numWanted uint32, sortSpec SortSpec) (Hits, error)")
+ searcherBinding.SpecMethod("Top_Docs", "topDocs(Query, uint32, SortSpec) (TopDocs, error)")
searcherBinding.SpecMethod("Close", "Close() error")
+ searcherBinding.SpecMethod("Fetch_Doc", "FetchDoc(int32) (HitDoc, error)")
+ searcherBinding.SpecMethod("Fetch_Doc_Vec", "fetchDocVec(int32) (DocVector, error)")
searcherBinding.SpecMethod("", "ReadDoc(int32, interface{}) error")
searcherBinding.Register()
http://git-wip-us.apache.org/repos/asf/lucy/blob/d483dd07/go/lucy/index_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/index_test.go b/go/lucy/index_test.go
index 4faab4e..5fb4d8b 100644
--- a/go/lucy/index_test.go
+++ b/go/lucy/index_test.go
@@ -334,7 +334,7 @@ func TestDocVectorMisc(t *testing.T) {
indexer.AddDoc(&testDoc{Content: "foo bar baz"})
indexer.Commit()
searcher, _ := OpenIndexSearcher(folder)
- dv := searcher.fetchDocVec(1)
+ dv, _ := searcher.fetchDocVec(1)
fieldBuf := dv.fieldBuf("content");
if fieldBuf == nil {
t.Errorf("fieldBuf returned nil")
http://git-wip-us.apache.org/repos/asf/lucy/blob/d483dd07/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index 44f5054..147af63 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -88,6 +88,24 @@ func (s *SearcherIMP) ReadDoc(docID int32, doc interface{}) error {
}
}
+func (s *SearcherIMP) FetchDoc(docID int32) (doc HitDoc, err error) {
+ err = clownfish.TrapErr(func() {
+ self := (*C.lucy_Searcher)(clownfish.Unwrap(s, "s"))
+ docC := C.LUCY_Searcher_Fetch_Doc(self, C.int32_t(docID))
+ doc = WRAPHitDoc(unsafe.Pointer(docC))
+ })
+ return doc, err
+}
+
+func (s *SearcherIMP) fetchDocVec(docID int32) (dv DocVector, err error) {
+ err = clownfish.TrapErr(func() {
+ self := (*C.lucy_Searcher)(clownfish.Unwrap(s, "s"))
+ dvC := C.LUCY_Searcher_Fetch_Doc_Vec(self, C.int32_t(docID))
+ dv = WRAPDocVector(unsafe.Pointer(dvC))
+ })
+ return dv, err
+}
+
func (s *SearcherIMP) Close() error {
return clownfish.TrapErr(func() {
self := (*C.lucy_Searcher)(clownfish.Unwrap(s, "s"))
@@ -109,6 +127,19 @@ func (s *SearcherIMP) Hits(query interface{}, offset uint32, numWanted uint32,
return hits, err
}
+func (s *SearcherIMP) topDocs(query Query, numWanted uint32,
+ sortSpec SortSpec) (topDocs TopDocs, err error) {
+ err = clownfish.TrapErr(func() {
+ self := (*C.lucy_Searcher)(clownfish.Unwrap(s, "s"))
+ sortSpecC := (*C.lucy_SortSpec)(clownfish.UnwrapNullable(sortSpec))
+ queryC := (*C.lucy_Query)(clownfish.Unwrap(query, "query"))
+ topDocsC := C.LUCY_Searcher_Top_Docs(self, queryC,
+ C.uint32_t(numWanted), sortSpecC)
+ topDocs = WRAPTopDocs(unsafe.Pointer(topDocsC))
+ })
+ return topDocs, err
+}
+
type setScorer interface {
SetScore(float32)
}
http://git-wip-us.apache.org/repos/asf/lucy/blob/d483dd07/go/lucy/search_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/search_test.go b/go/lucy/search_test.go
index fcd3c33..50d7fae 100644
--- a/go/lucy/search_test.go
+++ b/go/lucy/search_test.go
@@ -418,7 +418,7 @@ type simpleTestDoc struct {
func TestHitsBasics(t *testing.T) {
index := createTestIndex("a", "b")
searcher, _ := OpenIndexSearcher(index)
- topDocs := searcher.topDocs(NewTermQuery("content", "a"), 10, nil)
+ topDocs, _ := searcher.topDocs(NewTermQuery("content", "a"), 10, nil)
hits := NewHits(searcher, topDocs, 0)
if got := hits.TotalHits(); got != topDocs.getTotalHits() {
t.Errorf("TotalHits is off: %d", got)
@@ -604,8 +604,13 @@ func TestIndexSearcherMisc(t *testing.T) {
if _, ok := searcher.GetReader().(PolyReader); !ok {
t.Error("GetReader")
}
- if _, ok := searcher.fetchDocVec(4).(DocVector); !ok {
- t.Error("DocVector")
+ doc, err := searcher.FetchDoc(4)
+ if _, ok := doc.(Doc); !ok || err != nil {
+ t.Error("FetchDoc: %v", err)
+ }
+ docVec, err := searcher.fetchDocVec(4)
+ if _, ok := docVec.(DocVector); !ok || err != nil {
+ t.Error("FetchDocVec: %v", err)
}
}
@@ -640,7 +645,10 @@ func TestIndexSearcherHits(t *testing.T) {
func TestIndexSearcherTopDocs(t *testing.T) {
index := createTestIndex("a", "b")
searcher, _ := OpenIndexSearcher(index)
- topDocs := searcher.topDocs(NewTermQuery("content", "b"), 10, nil)
+ topDocs, err := searcher.topDocs(NewTermQuery("content", "b"), 10, nil)
+ if err != nil {
+ t.Errorf("topDocs: %v", err)
+ }
matchDocs := topDocs.GetMatchDocs()
if docID := matchDocs[0].getDocID(); docID != 2 {
t.Errorf("TopDocs expected 2, got %d", docID)