flask-msearch icon indicating copy to clipboard operation
flask-msearch copied to clipboard

AND and OR searches not giving any result

Open chaudharynirupma opened this issue 3 years ago • 1 comments

keyword = "first_name:Akhil AND country:london"

results = Post.query.msearch(keyword,limit=20)

there is a row in my db with first_name=Akhil and country=london
It gives rollback and not showing any result 
Please help me with this
thanks in advance

chaudharynirupma avatar Apr 05 '21 10:04 chaudharynirupma

Any index has been created? This demo works well

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tempfile import mkdtemp

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_msearch import Search

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['MSEARCH_INDEX_NAME'] = mkdtemp()
app.config['MSEARCH_BACKEND'] = 'whoosh'
app.config['MSEARCH_PRIMARY_KEY'] = 'id'
app.config['MSEARCH_ENABLE'] = True

db = SQLAlchemy(app)
search = Search(app, db=db)


class Post(db.Model):
    __tablename__ = 'post'
    __searchable__ = ['first_name', 'country']

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String)
    country = db.Column(db.String)


def create_data(session):
    post = Post(first_name="Akhil", country="london")
    session.add(post)
    session.commit()


if __name__ == '__main__':
    db.create_all()
    create_data(session=db.session)

    keyword = "first_name:Akhil AND country:london"
    results = Post.query.msearch(keyword, limit=20)
    print(results)
    print(results.all())

honmaple avatar Apr 08 '21 02:04 honmaple