title: Text Search in Flask and Python Using flask-whooshalchemy
body: <p>I have undergone similar posting in text search using Flask-WhooshAlchemy. But none of the search provide concrete results and none of these address blueprints.</p>
  
  <p>My task is to search text using flask and python from database. My database is MySQL and I am using SQLAlchemy for it.</p>
  
  <p>My sturcture of program looks like:</p>
  
  <pre><code>BlogPost
      Admin
      __init__.py
      forms.py
      views.py
      User
       __ init__.py
      forms.py
      views.py
  __init__.py
  models.py
  </code></pre>
  
  <p><strong>init</strong>.py:</p>
  
  <pre><code>import os
  from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  import flask_whooshalchemy as whooshalchemy
  app.config['SECRET_KEY'] = 'mysecret'
  basedir = os.path.abspath(os.path.dirname(__file__))
  app.config['SQLALCHEMY_DATABASE_URI'] =      'sqlite:///'+os.path.join(basedir,'app_v29.db')
  app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  app.config['WHOOSE_BASE'] = 'sqlite:///'+os.path.join(basedir,'app_v29.db') 
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>import flask_whooshalchemy as whooshalchemy
  from BlogPost import app
  
  class Task_Add(db.Model):
      __searchable__ =['task_add']
  
      id = db.Column(db.Integer,primary_key=True)
      task_add = db.Column(db.String(64))
      def __init__(self,task_add):
          self.task_add = task_add
  
     def __repr__(self):
          return f"{self.task_add}"  
   whooshalchemy.whoosh_index(app, Task_Add)  
  
  class Invoice_Add(db.Model):
  id = db.Column(db.Integer,primary_key=True)
  invoice_add = db.Column(db.String(64))
  def __init__(self,invoice_add):
      self.invoice_add = invoice_add
  
  def __repr__(self):
      return f"{self.invoice_add}"
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>@adminDash.route('/view_task')
  def view_task():
      task_all=Task_Add.query.all()
      return render_template('admin/view_task.html',task_all=task_all)
  
  @adminDash.route('/search_task')
  def search_task():
      q=request.args.get('query')
      task_all=Task_Add.query.whoosh_search(q).all()
      return render_template('admin/view_task.html',task_all=task_all)
  </code></pre>
  
  <p>view_task.html</p>
  
  <pre><code>  &lt;div class="col-sm-3 col-sm-offset-1 blog-sidebar"&gt;
                &lt;div class="sidebar-module sidebar-module-insert"&gt;
                  &lt;form class="form-inline" method="GET" action="{{url_for('adminDash.search_task')}}"&gt;
                    &lt;div class="form-group"&gt;
                      &lt;input type="text" class="form-control" name="query" id="query"&gt;
                        &lt;button type="submit" class="btn btn-primary"&gt;Search&lt;/button&gt;
                    &lt;/div&gt;
                  &lt;/form&gt;
                &lt;/div&gt;
                &lt;/div&gt;
              &lt;!-- .card --&gt;
              &lt;section id="base-style" class="card"&gt;
                &lt;!-- .card-body --&gt;
                &lt;div class="card-body"&gt;
                  &lt;!-- .form --&gt;
                  &lt;div class="lits-group list-group-flush"&gt;
                    &lt;!-- .lits-group-item --&gt;
                    &lt;!-- .table-responsive --&gt;
                                 &lt;div class="table-responsive"&gt;
                                   &lt;!-- .table --&gt;
                                   &lt;table class="table table-sm mb-0"&gt;
                                     &lt;!-- thead --&gt;
                                     &lt;thead class="thead-"&gt;
                                       &lt;tr&gt;
                                         &lt;th&gt;&lt;font color="#0f1f94"&gt; Task&lt;/font&gt;&lt;/th&gt;
                                         &lt;th&gt;&lt;font color="#0f1f94"&gt; Delete&lt;/font&gt;&lt;/th&gt;
                                       &lt;/tr&gt;
                                     &lt;/thead&gt;
                                     &lt;!-- /thead --&gt;
                                     &lt;!-- tbody --&gt;
                                     &lt;tbody&gt;
                                       &lt;!-- tr --&gt;
                                       &lt;tr&gt;
                                         {% for task_in in task_all %}
                                         &lt;td&gt; {{task_in.task_add}} &lt;/td&gt;
                                         &lt;td&gt;
                                           &lt;form action="{{url_for('adminDash.delete_task',task_post_id=task_in.id)}}" method="POST"&gt;
                                             &lt;button type="submit" class="far fa-trash-alt"&gt;&lt;/button&gt;
                                           &lt;/form&gt;
                                         &lt;/td&gt;
                                       &lt;/tr&gt;
                                        {% endfor %}
                                   &lt;/tbody&gt;
                                 &lt;/table&gt;
                               &lt;/div&gt;
  
            &lt;!-- .lits-group-item --&gt;
                  &lt;/div&gt;
  
  
                &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>When I search a query in search button, it sends text to /search_task but </p>
  
  <pre><code>task_all=Task_Add.query.whoosh_search(q).all()
  </code></pre>
  
  <p>gives [] in the results though if query using </p>
  
  <pre><code>task_all=Task_Add.query.all()
  </code></pre>
  
  <p>it will gives query.</p>
  
  <p>Thank you to let me know where I make mistake. And how to use Flask-WhooshAlchemy in blueprints?</p>
  
  <p>Thanks</p>