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/09/28 21:52:26 UTC

[10/14] lucy git commit: Customize and test Collector Go bindings.

Customize and test Collector Go bindings.

*   Custom binding for SortCollector's PopMatchDocs.
*   Tests for BitCollector, OffsetCollector, SortCollector


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/40a1eecb
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/40a1eecb
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/40a1eecb

Branch: refs/heads/master
Commit: 40a1eecbec1b212d6d2b7742bfeba7d546467d15
Parents: d850571
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Fri Sep 11 19:21:05 2015 -0700
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Tue Sep 15 15:54:51 2015 -0700

----------------------------------------------------------------------
 go/build.go            |  4 ++++
 go/lucy/search.go      | 14 ++++++++++++++
 go/lucy/search_test.go | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/40a1eecb/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index 11615aa..15fbd21 100644
--- a/go/build.go
+++ b/go/build.go
@@ -202,6 +202,10 @@ func specClasses(parcel *cfc.Parcel) {
 	sortSpecBinding.SetSuppressCtor(true)
 	sortSpecBinding.SpecMethod("Get_Rules", "GetRules() []SortRule")
 	sortSpecBinding.Register()
+
+	sortCollBinding := cfc.NewGoClass(parcel, "Lucy::Search::Collector::SortCollector")
+	sortCollBinding.SpecMethod("Pop_Match_Docs", "PopMatchDocs() []MatchDoc")
+	sortCollBinding.Register()
 }
 
 func build() {

http://git-wip-us.apache.org/repos/asf/lucy/blob/40a1eecb/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index 2723265..b47b340 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -18,6 +18,7 @@ package lucy
 
 /*
 #include "Lucy/Search/Collector.h"
+#include "Lucy/Search/Collector/SortCollector.h"
 #include "Lucy/Search/Hits.h"
 #include "Lucy/Search/IndexSearcher.h"
 #include "Lucy/Search/Query.h"
@@ -326,3 +327,16 @@ func newMockMatcher(docIDs []int32, scores []float32) MockMatcher {
 	matcher := C.lucy_MockMatcher_new(docIDsCF, blob)
 	return WRAPMockMatcher(unsafe.Pointer(matcher))
 }
+
+func (sc *SortCollectorIMP) PopMatchDocs() []MatchDoc {
+	self := (*C.lucy_SortCollector)(clownfish.Unwrap(sc, "sc"))
+	matchDocsC := C.LUCY_SortColl_Pop_Match_Docs(self)
+	defer C.cfish_decref(unsafe.Pointer(matchDocsC))
+	length := int(C.CFISH_Vec_Get_Size(matchDocsC))
+	slice := make([]MatchDoc, length)
+	for i := 0; i < length; i++ {
+		elem := C.cfish_incref(unsafe.Pointer(C.CFISH_Vec_Fetch(matchDocsC, C.size_t(i))))
+		slice[i] = WRAPMatchDoc(unsafe.Pointer(elem))
+	}
+	return slice
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/40a1eecb/go/lucy/search_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/search_test.go b/go/lucy/search_test.go
index 6d0c42d..d084f53 100644
--- a/go/lucy/search_test.go
+++ b/go/lucy/search_test.go
@@ -529,3 +529,41 @@ func TestSpanBasics(t *testing.T) {
 		t.Errorf("Set/Get weight: %f", got)
 	}
 }
+
+func TestBitCollectorBasics(t *testing.T) {
+	index := createTestIndex("a", "b", "c", "a")
+	searcher, _ := OpenIndexSearcher(index)
+	bitVec := NewBitVector(5)
+	collector := NewBitCollector(bitVec)
+	searcher.Collect(NewTermQuery("content", "a"), collector)
+	expected := []bool{false, true, false, false, true, false, false, false}
+	if got := bitVec.ToArray(); !reflect.DeepEqual(got,expected) {
+		t.Errorf("Unexpected result set: %v", got)
+	}
+}
+
+func TestOffsetCollectorBasics(t *testing.T) {
+	index := createTestIndex("a", "b", "c")
+	searcher, _ := OpenIndexSearcher(index)
+	bitVec := NewBitVector(64)
+	bitColl := NewBitCollector(bitVec)
+	offsetColl := NewOffsetCollector(bitColl, 40)
+	searcher.Collect(NewTermQuery("content", "b"), offsetColl)
+	if got := bitVec.NextHit(0); got != 42 {
+		t.Errorf("Unexpected docID: %d", got)
+	}
+}
+
+func TestSortCollectorBasics(t *testing.T) {
+	index := createTestIndex("a", "b", "c", "a")
+	searcher, _ := OpenIndexSearcher(index)
+	collector := NewSortCollector(nil, nil, 1)
+	searcher.Collect(NewTermQuery("content", "a"), collector)
+	if totalHits := collector.GetTotalHits(); totalHits != 2 {
+		t.Errorf("Unexpected TotalHits: %d", totalHits)
+	}
+	matchDocs := collector.PopMatchDocs()
+	if docID := matchDocs[0].GetDocID(); docID != 1 {
+		t.Errorf("Weird MatchDoc: %d", docID)
+	}
+}