-
Notifications
You must be signed in to change notification settings - Fork 0
Comparison with PostgreSQL
martasumyk edited this page May 27, 2023
·
1 revision
- Create table
CREATE TABLE words (
word TEXT
);
- Use the COPY command to import the data from the file into the "words" table. Assuming the words_eng.txt file is in the same directory as your current location
COPY words FROM 'words_eng.txt';
- To perform searching operation run this command
SELECT * FROM documents WHERE to_tsvector('english', content) @@ to_tsquery('english', 'search_term');
-
main.py
code
Import the necessary modules:
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
Initialize your Flask application and configure the database connection:
app = Flask(__name__, template_folder=".")
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost:5432/database_name'
db = SQLAlchemy(app)
Define a model for your table:
class Word(db.Model):
__tablename__ = 'words'
word = db.Column(db.String, primary_key=True)
content_tsvector = db.Column(db.String)
Implement the full-text search functionality in your Flask route:
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
search_query = request.form['query']
results = Word.query.filter(Word.content_tsvector.match(search_query)).all()
return render_template('search_results.html', results=results)
return render_template('search.html')
if __name__ == '__main__':
app.run(debug=True)
-
search.html
code
Create the search.html
template with a search form:
<form method="POST" action="/search">
<input type="text" name="query" placeholder="Enter search query">
<button type="submit">Search</button>
</form>
Create the search_results.html
template to display the search results:
-
search_results.html
code
{% for result in results %}
<p>{{ result.word }}</p>
{% endfor %}
- Uses a specialized full-text search engine built into the PostgreSQL database.
- Provides advanced features like stemming, ranking, and search operators.
- Supports various languages and allows customization of text processing and search behavior.
- Can handle large amounts of text efficiently and has better performance.
- Makes it easy to combine full-text search with other database operations.
- Based on a custom implementation of a suffix trie data structure.
- Provides a basic and simplified approach to full-text search.
- Offers a simple and lightweight solution for searching within a limited set of words or small texts.
- Does not require an external full-text search engine or database.
- Can be suitable for scenarios where you have a specific and small set of words or limited search requirements.