开发者

SQLAlchemy Search Algorithms

开发者 https://www.devze.com 2023-03-22 19:52 出处:网络
I am working on a pyramid web application that uses SQLAlchemy. I have a database of 7000 US universities, and an input box on the front end to input a university. The input box uses jQuery UI\'s auto

I am working on a pyramid web application that uses SQLAlchemy.

I have a database of 7000 US universities, and an input box on the front end to input a university. The input box uses jQuery UI's autocomplete, which later calls my pyramid app with AJAX to get a list of options.

The way I am fetching results based on the term are as follows: session.query(University).filter(University.name.like('%' + term + '%')

This works pretty decently, however, I would like to support the following - when a user enters UCLA, I would like to find University of California - Los Angeles. For FSU - Florida Status University, and so forth.

Other than iterating my entire list of universities (~8000), and manually deciding if the na开发者_运维百科me matches, is there a way to specify a matching function to sqlalchemy's query/filter?

Thanks!


SQLAlchemy is essentially a front-end to SQL. If your query isn't something that can be easily expressed as SQL, it's unlikely you're going to see any benefit from trying to twist it in that direction relative to just iterating manually - it still has to pull out a whole row and compare it, whether you do it or it does it.

However, if when you search for X, you also want to search for a Y which can be derived from X, look up the Y first, then query for both using the OR operator.

ie. Don't do:

get all rows, and for each row, see if it matches X or Y

instead do:

get all rows that match X plus all rows that match Y.

0

精彩评论

暂无评论...
验证码 换一张
取 消