-
Notifications
You must be signed in to change notification settings - Fork 661
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from datasciencebr/cuducos-full-text-search
Use full text search to query reimbursements
- Loading branch information
Showing
11 changed files
with
126 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.contrib.postgres.search import SearchVector | ||
|
||
from jarbas.core.models import Reimbursement | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def handle(self, *args, **options): | ||
total = Reimbursement.objects.count() | ||
print('Creating search vector for {} reimbursements…'.format(total)) | ||
print('This takes several minutes/hours.') | ||
|
||
search_vector = \ | ||
SearchVector('congressperson_name', config='portuguese', weight='A') + \ | ||
SearchVector('supplier', config='portuguese', weight='A') + \ | ||
SearchVector('cnpj_cpf', config='portuguese', weight='A') + \ | ||
SearchVector('party', config='portuguese', weight='A') + \ | ||
SearchVector('state', config='portuguese', weight='B') + \ | ||
SearchVector('receipt_text', config='portuguese', weight='B') + \ | ||
SearchVector('passenger', config='portuguese', weight='C') + \ | ||
SearchVector('leg_of_the_trip', config='portuguese', weight='C') + \ | ||
SearchVector('subquota_description', config='portuguese', weight='D') + \ | ||
SearchVector('subquota_group_description', config='portuguese', weight='D') | ||
|
||
Reimbursement.objects.update(search_vector=search_vector) |
26 changes: 26 additions & 0 deletions
26
jarbas/core/migrations/0039_add_search_vector_to_reimbursement.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.4 on 2017-09-27 20:19 | ||
from __future__ import unicode_literals | ||
|
||
import django.contrib.postgres.search | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0038_auto_20170728_1748'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='historicalreimbursement', | ||
name='search_vector', | ||
field=django.contrib.postgres.search.SearchVectorField(null=True), | ||
), | ||
migrations.AddField( | ||
model_name='reimbursement', | ||
name='search_vector', | ||
field=django.contrib.postgres.search.SearchVectorField(null=True), | ||
), | ||
] |
20 changes: 20 additions & 0 deletions
20
jarbas/core/migrations/0040_create_gin_index_with_search_vector.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.4 on 2017-09-28 02:37 | ||
from __future__ import unicode_literals | ||
|
||
import django.contrib.postgres.indexes | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0039_add_search_vector_to_reimbursement'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddIndex( | ||
model_name='reimbursement', | ||
index=django.contrib.postgres.indexes.GinIndex(fields=['search_vector'], name='core_reimbu_search__ba9b2f_gin'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
|
||
from jarbas.core.management.commands.searchvector import Command | ||
from jarbas.core.models import Reimbursement | ||
|
||
|
||
class TestCommandHandler(TestCase): | ||
|
||
@patch.object(Reimbursement.objects, 'update') | ||
@patch('jarbas.core.management.commands.searchvector.print') | ||
def test_handler(self, print_, update): | ||
command = Command() | ||
command.handle() | ||
self.assertEqual(2, print_.call_count) | ||
self.assertEqual(1, update.call_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters