Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

ующая таблица топики задачи taskstopics= db.Table('taskstopics', db.Column('taskid',…

  1. User

    ующая таблица топики задачи tasks_topics= db.Table('tasks_topics', db.Column('task_id', db.Integer, db.ForeignKey('tasks.id')), db.Column('topics_id', db.Integer, db.ForeignKey('topics.id')) )

    class Tasks(db.Model): tablename = 'tasks'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    url = db.Column(db.String(320), comment="ЧПУ задачи в адресе (транслитом)")
    task = db.Column(db.Text, comment='Условие задачи')
    answer = db.Column(db.Text, comment='Ответ')
    status = db.Column(db.Boolean, default=0, comment='Статус: опубликована/неактивна (по умолчанию нет)')
    created_t = db.Column(db.DateTime, comment='Дата и время создания')
    level = db.Column(db.SmallInteger, default=1, comment='Уровень сложности: 1,2 ... (по умолчанию 1 уровень)')
    

    связи

    topics = db.relationship('Topics', secondary=tasks_topics, back_populates="tasks")

    class Topics(db.Model): tablename = 'topics'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(140), comment="Уникальное название темы(подтемы)")
    parent = db.Column(db.Integer, default=0, comment="ID родительской темы(если есть)")
    
    # связи
    tasks = db.relationship('Tasks',
                            secondary=tasks_topics,
                            back_populates="topics")
    

    from sqlalchemy.orm import subqueryload,load_only from whoosh.analysis import StemmingAnalyzer, StandardAnalyzer

    schema = Schema(id=ID(stored=True), task=TEXT(stored=True), answer=TEXT(stored=True), topics_name = TEXT(stored=True))

    index_dir = 'tasks_index' if not os.path.exists(index_dir): os.mkdir(index_dir) ix = create_in(index_dir, schema) else: ix = open_dir(index_dir)

    with ix.writer() as writer: all_tasks = Tasks.query.options(subqueryload(Tasks.topics)).all() for task in all_tasks: topics_name = "" for top in task.topics: topics_name = topics_name+" "+top.name

        writer.add_document(id=str(task.id), task=task.task, answer=task.answer,topics_name = topics_name)
    writer.commit()
    

    Скажи: как использовать Whoosh 'tasks_index’ вместе с flask Msearch? Что мне нужно добавить в Tasks и msearch, чтобы запрос выполнялся results = Tasks.query.msearch('и перевернули').all()

  2. Assistant

    To use the 'tasks_index' index with Flask-Msearch, you need to make the following additions to your Tasks model and Flask-Msearch configuration:

    1. Add a searchable field to the Tasks model:
    class Tasks(db.Model):
        # ...
        __searchable__ = ['task', 'answer']
    
    1. Configure Flask-Msearch to use the 'tasks_index' index:
    msearch.init_app(app, index_prefix='tasks')
    
    class Tasks(db.Model):
        # ...
        __searchable__ = ['task', 'answer']
        __msearch_index__ = 'tasks_index'
    
    1. Update the index creation code to include the searchable fields:
    schema = Schema(id=ID(stored=True),
                    task=TEXT(stored=True),
                    answer=TEXT(stored=True),
                    topics_name=TEXT(stored=True))
    
    # ...
    
    writer.add_document(id=str(task.id), task=task.task, answer=task.answer, topics_name=topics_name)
    
    1. To perform a search using Flask-Msearch, you can now use the msearch method on the Tasks model:
    results = Tasks.query.msearch('и перевернули').all()
    

    Note: Make sure you have properly configured your Flask-Msearch extension and the necessary Whoosh indexing directories before running the search query.