class SearchContainer(ReadonlyContainer): __name__ = 'search' def __init__(self): self.clear() def clear(self): self.tag_container = SearchByTagContainer(self) self.catalog = Catalog() self.catalog['tags'] = KeywordIndex('tags', ITagged) self.catalog['name'] = TextIndex('display_name', IDisplayName, True) self.catalog['__all'] = TextIndex('tokens', ITokenized, True) self.ids = IntIds() def index_object(self, obj): real_obj = follow_symlinks(obj) try: self.catalog.index_doc(self.ids.register(real_obj), real_obj) except NotYet: log.msg("cannot index object %s because it's not yet committed" % obj, system='search') def _index_object(self, obj): real_obj = follow_symlinks(obj) self.catalog.index_doc(self.ids.register(real_obj), real_obj) def unindex_object(self, obj): try: self.catalog.unindex_doc(self.ids.register(obj)) except NotYet: log.msg("cannot unindex object %s because it's not yet committed" % obj, system='search') def search(self, **kwargs): # HACK, we should be able to setup a persistent utility provideUtility(self.ids, IIntIds) return list(self.catalog.searchResults(**kwargs)) def search_goog(self, query): # hack, zope catalog treats ':' specially return self.search(__all=query.replace(':', '_')) @property def _items(self): return {'by-tag': self.tag_container}
def setupCatalog(test, optCount=10, unoptCount=10, halfCount=10): intids = IntIds() component.provideUtility(intids, IIntIds) component.provideAdapter(SimpleKeyReference) cat = Catalog() cat['opt_name'] = FieldIndex('name', IOptimizedClass) cat['opt_value'] = FieldIndex('value', IOptimizedClass) cat['half_name'] = FieldIndex('name', IHalfOptimizedClass) cat['half_valueOpt'] = FieldIndex('value', IHalfOptimizedClass) cat['all_opt'] = AllIndex(IOptimizedClass) cat['all_unopt'] = AllIndex(IUnOptimizedClass) cat['all_half'] = AllIndex(IHalfOptimizedClass) for i in range(optCount): o = OptimizedClass() o.value = i o.name = unicode(i) id = intids.register(o) cat.index_doc(id, o) for i in range(unoptCount): o = UnOptimizedClass() o.value = i o.name = unicode(i) id = intids.register(o) cat.index_doc(id, o) for i in range(halfCount): o = HalfOptimizedClass() o.valueOpt = i o.valueNoOpt = i o.name = unicode(i) id = intids.register(o) cat.index_doc(id, o) component.provideUtility(cat, ICatalog, name='foo-catalog')
def setupCatalog(test): intids = IntIds() component.provideUtility(intids, IIntIds) component.provideAdapter(SimpleKeyReference) cat = Catalog() cat['org_name'] = FieldIndex('name', IOrganization) cat['proj_name'] = FieldIndex('name', IProject) cat['proj_descr'] = FieldIndex('description', IProject) cat['student_name'] = FieldIndex('name', IStudent) cat['student_country'] = FieldIndex('country', IStudent) cat['mentor_name'] = FieldIndex('name', IMentor) cat['all_students'] = AllIndex(IStudent) cat['all_mentors'] = AllIndex(IMentor) cat['all_projects'] = AllIndex(IProject) cat['all_orgs'] = AllIndex(IOrganization) m1 = Mentor(u"John Doe") id = intids.register(m1) cat.index_doc(id, m1) p1 = Project(u"Save the world", u'test') id = intids.register(p1) cat.index_doc(id, p1) s1 = Student(u"Charith", u"Sri Lanka") id = intids.register(s1) cat.index_doc(id, s1) s2 = Student(u"Jane", u"USA") id = intids.register(s2) cat.index_doc(id, s2) s3 = Student(u"Ann", u"Hungary") id = intids.register(s3) cat.index_doc(id, s3) s4 = Student(u"Stewart", u"Hungary", m1) id = intids.register(s4) cat.index_doc(id, s4) o1 = Organization(u"Zope.org") id = intids.register(o1) cat.index_doc(id, o1) # cat2 = zc.relation.catalog.Catalog(dumpRelation, loadRelation) # cat2.addValueIndex(IProjectRelation['project'], dumpObj, loadObj, btree=BTrees.family32.OO) # cat2.addValueIndex(IProjectRelation['mentor'], dumpObj, loadObj, btree=BTrees.family32.OO) # cat2.addDefaultQueryFactory(zc.relation.queryfactory.TransposingTransitive('project','mentor')) # # rel = ProjectRelation(m1, p1) # cat2.index(rel) # component.provideUtility(cat2, zc.relation.interfaces.ICatalog, name='rel-catalog') component.provideUtility(cat, ICatalog, name='foo-catalog')
catalog = Catalog() ztapi.provideUtility(ICatalog, catalog, 'catalog1') from zc.catalog.catalogindex import SetIndex catalog['f1'] = SetIndex('f1', IContent) catalog['f2'] = FieldIndex('f2', IContent) print catalog d2 = datetime.now() print d1, d2 content = [ Content(1, ['a', 'b', 'c'], 1), Content(2, ['a'], 1), Content(3, ['b'], 1), Content(4, ['c', 'd'], 2), Content(5, ['b', 'c'], 2), Content(6, ['a', 'c'], 2), Content(7, [d1.strftime('%m-%b-%d'), d2.strftime('%m-%b-%d'), 'a'], 2) ] print content for entry in content: catalog.index_doc(intid.register(entry), entry) f1 = ('catalog1', 'f1') from hurry.query.set import AnyOf print displayQuery(AnyOf(f1, ['a','c'])) print "*"*8 print displayQuery(AnyOf(f1, [d1.strftime('%m-%b-%d')]))