func OpenIndexer(args *OpenIndexerArgs) (obj Indexer, err error) { var schemaC *C.lucy_Schema = nil if args.Schema != nil { schemaC = (*C.lucy_Schema)(unsafe.Pointer(args.Schema.TOPTR())) } switch args.Index.(type) { case string: default: panic("TODO: support Folder") } ixLoc := clownfish.NewString(args.Index.(string)) var managerC *C.lucy_IndexManager = nil if args.Manager != nil { managerC = (*C.lucy_IndexManager)(unsafe.Pointer(args.Manager.TOPTR())) } var flags int32 if args.Create { flags = flags | int32(C.lucy_Indexer_CREATE) } if args.Truncate { flags = flags | int32(C.lucy_Indexer_TRUNCATE) } err = clownfish.TrapErr(func() { cfObj := C.lucy_Indexer_new(schemaC, (*C.cfish_Obj)(unsafe.Pointer(ixLoc.TOPTR())), managerC, C.int32_t(flags)) obj = WRAPIndexer(unsafe.Pointer(cfObj)) }) return obj, err }
func (obj *SchemaIMP) SpecField(field string, fieldType FieldType) { self := ((*C.lucy_Schema)(unsafe.Pointer(obj.TOPTR()))) fieldCF := clownfish.NewString(field) C.LUCY_Schema_Spec_Field(self, (*C.cfish_String)(unsafe.Pointer(fieldCF.TOPTR())), (*C.lucy_FieldType)(unsafe.Pointer(fieldType.TOPTR()))) }
func OpenIndexSearcher(index interface{}) (obj IndexSearcher, err error) { var indexC *C.cfish_Obj switch index.(type) { case string: ixLoc := clownfish.NewString(index.(string)) indexC = (*C.cfish_Obj)(unsafe.Pointer(ixLoc.TOPTR())) default: panic("TODO: support Folder") } err = clownfish.TrapErr(func() { cfObj := C.lucy_IxSearcher_new(indexC) obj = WRAPIndexSearcher(unsafe.Pointer(cfObj)) }) return obj, err }
func (obj *IndexerIMP) AddDoc(doc interface{}) error { self := ((*C.lucy_Indexer)(unsafe.Pointer(obj.TOPTR()))) stockDoc := C.LUCY_Indexer_Get_Stock_Doc(self) docFields := (*C.cfish_Hash)(C.LUCY_Doc_Get_Fields(stockDoc)) C.CFISH_Hash_Clear(docFields) // TODO: Support map as doc in addition to struct as doc. // Get reflection value and type for the supplied struct. var docValue reflect.Value if reflect.ValueOf(doc).Kind() == reflect.Ptr { temp := reflect.ValueOf(doc).Elem() if temp.Kind() == reflect.Struct { docValue = temp } } if docValue == (reflect.Value{}) { mess := fmt.Sprintf("Doc not struct pointer: %v", reflect.TypeOf(doc)) return clownfish.NewErr(mess) } docType := docValue.Type() for i := 0; i < docValue.NumField(); i++ { field := docType.Field(i).Name value := docValue.Field(i).String() fieldC := obj.findFieldC(field) valueC := clownfish.NewString(value) C.CFISH_Hash_Store(docFields, (*C.cfish_String)(unsafe.Pointer(fieldC)), C.cfish_inc_refcount(unsafe.Pointer(valueC.TOPTR()))) } // TODO create an additional method AddDocWithBoost which allows the // client to supply `boost`. boost := 1.0 err := clownfish.TrapErr(func() { C.LUCY_Indexer_Add_Doc(self, stockDoc, C.float(boost)) }) return err }
func doHits(obj Searcher, query interface{}, offset uint32, numWanted uint32, sortSpec SortSpec) (hits Hits, err error) { self := ((*C.lucy_Searcher)(unsafe.Pointer(obj.TOPTR()))) var sortSpecC *C.lucy_SortSpec if sortSpec != nil { sortSpecC = (*C.lucy_SortSpec)(unsafe.Pointer(sortSpec.TOPTR())) } switch query.(type) { case string: queryStringC := clownfish.NewString(query.(string)) err = clownfish.TrapErr(func() { hitsC := C.LUCY_Searcher_Hits(self, (*C.cfish_Obj)(unsafe.Pointer(queryStringC.TOPTR())), C.uint32_t(offset), C.uint32_t(numWanted), sortSpecC) hits = WRAPHits(unsafe.Pointer(hitsC)) }) default: panic("TODO: support Query objects") } return hits, err }