Add ReadDoc to Searcher Go bindings. Support multiple document types when fetching document data via a Searcher.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/853cf863 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/853cf863 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/853cf863 Branch: refs/heads/master Commit: 853cf863a11be2d6543c90ad5830504081747c0f Parents: 34ffe42 Author: Marvin Humphrey <[email protected]> Authored: Fri Sep 25 19:59:28 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Mon Sep 28 12:54:39 2015 -0700 ---------------------------------------------------------------------- go/build.go | 1 + go/lucy/search.go | 20 ++++++++++++++++++++ go/lucy/search_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/853cf863/go/build.go ---------------------------------------------------------------------- diff --git a/go/build.go b/go/build.go index 15fbd21..e250aeb 100644 --- a/go/build.go +++ b/go/build.go @@ -152,6 +152,7 @@ func specClasses(parcel *cfc.Parcel) { searcherBinding.SpecMethod("Hits", "Hits(query interface{}, offset uint32, numWanted uint32, sortSpec SortSpec) (Hits, error)") searcherBinding.SpecMethod("Close", "Close() error") + searcherBinding.SpecMethod("", "ReadDoc(int32, interface{}) error") searcherBinding.Register() hitsBinding := cfc.NewGoClass(parcel, "Lucy::Search::Hits") http://git-wip-us.apache.org/repos/asf/lucy/blob/853cf863/go/lucy/search.go ---------------------------------------------------------------------- diff --git a/go/lucy/search.go b/go/lucy/search.go index ed1b5e4..514c2eb 100644 --- a/go/lucy/search.go +++ b/go/lucy/search.go @@ -32,6 +32,7 @@ package lucy #include "Lucy/Search/SortSpec.h" #include "Lucy/Search/TopDocs.h" #include "Lucy/Document/HitDoc.h" +#include "Lucy/Index/IndexReader.h" #include "LucyX/Search/MockMatcher.h" #include "Clownfish/Blob.h" #include "Clownfish/Hash.h" @@ -76,6 +77,25 @@ func (obj *IndexSearcherIMP) Hits(query interface{}, offset uint32, numWanted ui return doHits(obj, query, offset, numWanted, sortSpec) } +// Read data into the supplied doc. +func (s *SearcherIMP) ReadDoc(docID int32, doc interface{}) error { + self := (*C.lucy_Searcher)(clownfish.Unwrap(s, "s")) + class := C.cfish_Obj_get_class((*C.cfish_Obj)(unsafe.Pointer(self))) + if class == C.LUCY_INDEXSEARCHER { + ixReader := C.LUCY_IxSearcher_Get_Reader((*C.lucy_IndexSearcher)(unsafe.Pointer(self))) + cfStr := (*C.cfish_String)(clownfish.GoToClownfish("Lucy::Index::DocReader", unsafe.Pointer(C.CFISH_STRING), false)) + defer C.cfish_decref(unsafe.Pointer(cfStr)) + docReader := C.LUCY_IxReader_Fetch(ixReader, cfStr) + if docReader == nil { + return clownfish.NewErr("No DocReader available") + } + docReaderGo := clownfish.WRAPAny(unsafe.Pointer(C.cfish_incref(unsafe.Pointer(docReader)))).(DocReader) + return fetchDocFromDocReader(docReaderGo, docID, doc) + } else { + return clownfish.NewErr("Support for ReadDoc not implemented") + } +} + func doClose(obj Searcher) error { self := ((*C.lucy_Searcher)(unsafe.Pointer(obj.TOPTR()))) return clownfish.TrapErr(func() { http://git-wip-us.apache.org/repos/asf/lucy/blob/853cf863/go/lucy/search_test.go ---------------------------------------------------------------------- diff --git a/go/lucy/search_test.go b/go/lucy/search_test.go index b281477..d0668ae 100644 --- a/go/lucy/search_test.go +++ b/go/lucy/search_test.go @@ -623,6 +623,36 @@ func TestIndexSearcherTopDocs(t *testing.T) { } } +func TestIndexSearcherReadDoc(t *testing.T) { + index := createTestIndex("a", "b") + searcher, _ := OpenIndexSearcher(index) + docDoc := NewHitDoc(0, -1.0) + docStruct := &simpleTestDoc{} + docMap := make(map[string]interface{}) + var err error + err = searcher.ReadDoc(2, docDoc) + if err != nil { + t.Errorf("ReadDoc failed with HitDoc: %v", err) + } + err = searcher.ReadDoc(2, docStruct) + if err != nil { + t.Errorf("ReadDoc failed with struct: %v", err) + } + err = searcher.ReadDoc(2, docMap) + if err != nil { + t.Errorf("ReadDoc failed with map: %v", err) + } + if docDoc.Extract("content").(string) != "b" { + t.Error("Next with Doc object yielded bad data") + } + if docStruct.Content != "b" { + t.Error("Next with struct yielded bad data") + } + if docMap["content"].(string) != "b" { + t.Error("Next with map yielded bad data") + } +} + func TestMatchDocBasics(t *testing.T) { matchDoc := NewMatchDoc(0, 1.0, nil) matchDoc.SetDocID(42)
