def test_search_with_date(self):
        OfficerInfoDocType(meta={
            'id': '1'
        },
                           full_name='full name',
                           badge='123',
                           url='url').save()
        CrDocType(meta={
            'id': '1'
        }, crid='1234', incident_date='2017-12-27').save()
        self.refresh_index()

        workers = {
            'OFFICER': OfficerWorker(),
            'DATE > CR': DateCRWorker(),
        }
        response = SearchManager(workers=workers).search('fu na 2017-12-27')
        expect(response).to.eq({
            'OFFICER': [{
                'id': '1',
                'url': 'url',
                'badge': '123',
                'full_name': u'full name'
            }],
            'DATE > CR': [{
                'id': '1',
                'crid': '1234',
                'incident_date': '2017-12-27'
            }]
        })
    def test_suggest_sample(self):
        taglessOfficerDoc = OfficerInfoDocType(
            meta={'id': '1'},
            full_name='this should not be returned',
            badge='123',
            url='url')
        taglessOfficerDoc.save()

        officerDoc = OfficerInfoDocType(meta={'id': '2'},
                                        full_name='full name',
                                        badge='123',
                                        url='url',
                                        tags=['sample'])
        officerDoc.save()

        self.refresh_index()

        response = SearchManager(workers={
            'OFFICER': OfficerWorker(),
        }).suggest_sample()

        expect(response).to.eq({
            'OFFICER': [{
                'id': '2',
                'url': 'url',
                'badge': '123',
                'full_name': u'full name',
                'tags': ['sample']
            }]
        })
    def test_prioritizing_autocomplete_and_keyword_on_badge_and_historic_badges(self):
        OfficerInfoDocType(
            full_name='Badge-Matched Guy',
            badge='00123',
            badge_keyword='00123',
        ).save()
        OfficerInfoDocType(
            full_name='Historic-Badge-Matched Guy',
            historic_badges=['00123'],
            historic_badges_keyword=['00123']
        ).save()
        OfficerInfoDocType(
            full_name='Partial Badge-Matched Guy',
            badge='100123',
            badge_keyword='100123'
        ).save()
        OfficerInfoDocType(
            full_name='Partial Historic-Badge-Matched Guy',
            historic_badges=['100123'],
            historic_badges_keyword=['100123']
        ).save()
        OfficerInfoDocType(
            full_name='Unmatched Guy',
            badge='12345',
            badge_keyword='12345'
        ).save()

        self.refresh_index()
        response = OfficerWorker().search('00123')

        expect(response.hits.total).to.equal(4)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('Badge-Matched Guy')
        expect(response.hits.hits[1]['_source']['full_name']).to.eq('Historic-Badge-Matched Guy')
        expect(response.hits.hits[2]['_source']['full_name']).to.eq('Partial Badge-Matched Guy')
        expect(response.hits.hits[3]['_source']['full_name']).to.eq('Partial Historic-Badge-Matched Guy')
    def test_search_officer_badge(self):
        OfficerInfoDocType(full_name='John Doe', badge='100123').save()

        self.refresh_index()

        response = OfficerWorker().search('100')

        expect(response.hits.total).to.equal(1)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('John Doe')
    def test_search_by_officer_id(self):
        doc = OfficerInfoDocType(full_name='some dude', badge='123', meta={'_id': '456'})
        doc.save()
        doc2 = OfficerInfoDocType(full_name='another guy', badge='789', meta={'_id': '012'})
        doc2.save()

        self.refresh_index()

        response = OfficerWorker().search('456')

        expect(response.hits.total).to.be.equal(1)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('some dude')
    def test_search_prioritizing_tags(self):
        doc = OfficerInfoDocType(
            full_name='some dude', badge='123', allegation_count=10)
        doc.save()
        doc = OfficerInfoDocType(
            full_name='another guy', badge='456', allegation_count=10, tags='somersault')
        doc.save()

        self.refresh_index()

        response = OfficerWorker().search('some')

        expect(response.hits.total).to.equal(2)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('another guy')
        expect(response.hits.hits[1]['_source']['full_name']).to.eq('some dude')
    def test_search_prioritizing_allegation_count(self):
        doc = OfficerInfoDocType(
            full_name='full name', badge='123', allegation_count=10)
        doc.save()
        doc = OfficerInfoDocType(
            full_name='funny naga', badge='456', allegation_count=20)
        doc.save()

        self.refresh_index()

        response = OfficerWorker().search('fu na')

        expect(response.hits.total).to.equal(2)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('funny naga')
        expect(response.hits.hits[1]['_source']['full_name']).to.eq('full name')
Exemple #8
0
class SearchMobileV2ViewSet(SearchViewSet):
    lookup_field = 'text'

    formatters = {
        'DATE > CR': CRFormatter,
        'DATE > TRR': TRRFormatter,
        'DATE > OFFICERS': OfficerV2Formatter,
        'OFFICER': OfficerV2Formatter,
        'CR': CRFormatter,
        'TRR': TRRFormatter,
        'INVESTIGATOR > CR': CRFormatter,
    }

    workers = {
        'DATE > CR': DateCRWorker(),
        'DATE > TRR': DateTRRWorker(),
        'OFFICER': OfficerWorker(),
        'CR': CRWorker(),
        'TRR': TRRWorker(),
        'DATE > OFFICERS': DateOfficerWorker(),
        'INVESTIGATOR > CR': InvestigatorCRWorker(),
    }

    recent_items_queries = [
        {
            'query_param': 'officer_ids',
            'query': OfficerMobileQuery,
            'serializer': OfficerSerializer,
        },
        {
            'query_param': 'crids',
            'query': CrMobileQuery,
            'serializer': AllegationSerializer,
        },
        {
            'query_param': 'trr_ids',
            'query': TrrMobileQuery,
            'serializer': TRRSerializer,
        },
    ]
Exemple #9
0
from search.date_util import find_dates_from_string
from search.workers import (
    DateWorker,
    OfficerWorker,
    UnitWorker,
    CommunityWorker,
    NeighborhoodsWorker
)
from .formatters import SimpleFormatter


DEFAULT_SEARCH_WORKERS = {
    'OFFICER': OfficerWorker(),
    'UNIT': UnitWorker(),
    'COMMUNITY': CommunityWorker(),
    'NEIGHBORHOOD': NeighborhoodsWorker()
}


class SearchManager(object):
    def __init__(self, formatters=None, workers=None, hooks=None):
        self.formatters = formatters or {}
        self.workers = workers or DEFAULT_SEARCH_WORKERS
        self.hooks = hooks or []

    def search(self, term, content_type=None, limit=10):
        response = {}

        _workers = {content_type: self.workers[content_type]} if content_type else self.workers
        search_with_dates = any([isinstance(worker, DateWorker) for worker in _workers.values()])
        dates = [date.strftime('%Y-%m-%d') for date in find_dates_from_string(term)] if search_with_dates else []