Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returns nothing no mater what keyword #5

Closed
zjyfdu opened this issue Dec 4, 2017 · 9 comments
Closed

returns nothing no mater what keyword #5

zjyfdu opened this issue Dec 4, 2017 · 9 comments

Comments

@zjyfdu
Copy link

zjyfdu commented Dec 4, 2017

Hi, I just modified my code following the instruction of QuickStart and Config,
like this

@course.route("/search")
def w_search():
    keyword = request.args.get('q')
    results = Course.query.msearch(keyword,fields=['title']).first()
    return redirect(url_for('course.classes', id=results.id))

However it returns nothing no mater what keywords
did I miss some other configurations?
actually, I know nothing about the role of Create_index

Thanks a Lot For Your Help!!!

>>> Course.query.msearch('asdf',fields=['title']).all()
[]
>>> Course.query.filter_by(title='asdf').all()
[<Course u'asdf'>]
@honmaple
Copy link
Owner

honmaple commented Dec 4, 2017

You should use search.create_index(Course) if course data is exists before you use flask_msearch,or whoosh can't find any data.

after search.init_app(app),flask_maple will auto update model index and you don't have to create or update index unless set MSEARCH_ENABLE = False

@zjyfdu
Copy link
Author

zjyfdu commented Dec 4, 2017

I figured it out! Thanks again!!

@zjyfdu zjyfdu closed this as completed Dec 4, 2017
@AdamGold
Copy link

AdamGold commented Jun 12, 2019

@honmaple The index doesn't automatically update for me. I have a test which creates a new user and then searches for it:

extra_user = User.create_from_credentials(**details)
db_instance.session.add(extra_user)
db_instance.session.commit()

assert User.query.msearch("name", ["username"]).all()

It doesn't work. When I add search.create_index() before the assertion, it works. MSEARCH_ENABLE is set to True.

@honmaple
Copy link
Owner

@AdamGold flask-maple use signal models_committed to update index automatically.
Please check if the config SQLALCHEMY_TRACK_MODIFICATIONS is True

@AdamGold
Copy link

AdamGold commented Jun 13, 2019

@honmaple Will give it a shot. Another question - when I delete a row in the table with the flask_login proxy:

db_instance.session.delete(flask_login.current_user)

I get the following error:

  File "/venv/lib/python3.6/site-packages/flask_msearch/backends.py", line 163, in _index_signal
    self.create_one_index(instance, delete=True)
  File "/venv/lib/python3.6/site-packages/flask_msearch/whoosh_backend.py", line 163, in create_one_index
    ix = self._index(table)
  File "/venv/lib/python3.6/site-packages/flask_msearch/whoosh_backend.py", line 142, in _index
    name = model.__table__.name
AttributeError: type object 'LocalProxy' has no attribute '__table__'

@honmaple
Copy link
Owner

@AdamGold current_user isn't model instance, you should use current_user._get_current_object()

@yomajo
Copy link

yomajo commented Aug 1, 2022

Not getting results either.
sqlite database contains products with keyword tec:
image

on route I have:

@search_bp.route('/', methods=['GET', 'POST'])
def search():
    # available at localhost/search
    if request.method == 'POST':
        q = request.form.get('q', None)
        print(f'\nquery: {q}\n')
        
        results_raw = Products.query.msearch(q, fields=['name'], limit=10)
        print(results_raw)

        results = results_raw.all()
        print(results)

        return render_template('results.html', results=results, query=q)
        
    return render_template('search.html')

In terminal this yields:

query: tec

SELECT products.id AS products_id, products.name AS products_name, products.qty AS products_qty, products.brand_name AS products_brand_name 
FROM products 
WHERE 0 = 1
[]

Documentation has filter(...) on query object.
results = Post.query.msearch(keyword,fields=['title'],limit=20).filter(...)

How should I use it?

Branch for reference: yomajo/Flask-Experiments@7b46868

@honmaple
Copy link
Owner

honmaple commented Aug 2, 2022

@yomajo The default analyzer can't get keyword ech

>>> from whoosh.analysis import StemmingAnalyzer
>>> ana = StemmingAnalyzer()
>>> [token.text for token in ana("Ron-tech")]
['ron', 'tech']
>>> [token.text for token in ana("Opentech")]
['opentech']

You should use other analyzer or custom analyzer

from whoosh.analysis import Tokenizer
from whoosh.analysis.acore import Token

class CustomTokenizer(Tokenizer):
    def __call__(self, value, positions=False, start_pos=0, **kwargs):
        token = Token(positions, **kwargs)
        pos = start_pos
        for pos in range(0, len(value) - 2):
            token.text = value[pos:pos + 3]
            if positions:
                token.pos = pos
            yield token


def CustomAnalyzer():
    return CustomTokenizer()


ana = CustomAnalyzer()
print([token.text for token in ana("Ron-tech")])
# ['Ron', 'on-', 'n-t', '-te', 'tec', 'ech']
print([token.text for token in ana("Opentech")])
# ['Ope', 'pen', 'ent', 'nte', 'tec', 'ech']

https://whoosh.readthedocs.io/en/latest/analysis.html

@yomajo
Copy link

yomajo commented Aug 2, 2022

Thanks for reply @honmaple! I'm new to full-text search space, so please be adaptive on my idiocy.

If I understand correctly, analyzer returns tokens and if token (or their variable sum) is not a whole query, then search fails?

Example in your last example, say query was ch, while non of tokens have exact token:

print([token.text for token in ana("Opentech")])
# ['Ope', 'pen', 'ent', 'nte', 'tec', 'ech']

Then what added benefit is there over using:
results = <Model>.query.filter(<Model>.<column>.ilike(f'%{q}%')).all() - where q is a query.

which is case insensitive search for substrings in Model column. Is it performance? Or am I misunderstanding tokenization on whoosh part?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants