Почему каждый раз приходится индексировать?
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ZODB.FileStorage import FileStorage
from ZODB.DB import DB
import persistent
import datetime, time
import random
import transaction
from zcatalog import catalog
from zcatalog import indexes
#
# Create catalog
cat = catalog.Catalog()
#
#
# ZODB storage
storage = FileStorage('Data.fs')
db = DB(storage)
connection = db.open()
root = connection.root()
#
#
#
names=[u'Василий', u'Иван', u'Николай', u'Герман', u'Тихон', u'Автоандил',
u'Илья', u'Пётр', u'Адольф', u'Рустем', u'Георгий', u'Гоча', u'Парамонт'
]
families=[u'Иванов', u'Петров', u'Сидоров', u'Пидоров', u'Какашенко', u'Пупкин',
u'Джугашвилли', u'Цукерман', u'Моцарт', u'Линкольн', u'Троцкий',]
second_names=[u'Иванович', u'Израилевич', u'Бонапартович', u'Маоцзедунович', u'Петрович',
u'Алексанрович']
#
#
# Create indexes
name_index = indexes.TextIndex(field_name="name")
family_index = indexes.TextIndex(field_name="family")
second_name_index=indexes.TextIndex(field_name="second_name")
person_index=indexes.FieldIndex(field_name="author")
date_index=indexes.FieldIndex(field_name="date")
#
cat["name"] = name_index
cat["family"] = family_index
cat["second_name"] = second_name_index
cat["author"] = person_index
cat['date']=date_index
#
#
#
#
class Person(persistent.Persistent):
def __init__(self, name, family, second_name):
self.name=name
self.family=family
self.second_name=second_name
class Anket(persistent.Persistent):
def __init__(self, author, date):
self.author=author
self.date=date
#
#
# function to create a test BD
def create_limon_ankets(count):
prefix='anket'
start_year=1950
end_year=datetime.date.today().year
for i in range(count):
rand_month=random.randrange(1,13)
rand_day=random.randrange(1,29)
rand_year=random.randrange(start_year, end_year+1)
rand_date=datetime.date(rand_year, rand_month, rand_day)
rand_name=random.choice(names)
rand_family=random.choice(families)
rand_second_name=random.choice(second_names)
rand_person=Person(rand_name, rand_family, rand_second_name)
#
# indexing
cat.index_doc(rand_person)
anket=Anket(rand_person, rand_date)
cat.index_doc(anket)
#
# saving data
root['%s_%s' % (prefix, i)]= anket
transaction.commit()
# BD is filled yet
#create_limon_ankets(10000)
#
#
#
# WHY???? Без вызова этой какахи не работает!!!!
def create_index():
for i in root:
instance=root[i]
cat.index_doc(instance)
cat.index_doc(instance.author)
#
#
create_index()
#
#
ss = cat.searchResults(family=u"Иванов OR Петров", name=u'Герман')
sss = cat.searchResults(date=[datetime.date(2011,11,06), datetime.date(2011,12,06)])