示例#1
0
class MediaSearch(BaseFacetedSearch):
    doc_types = [MediaDocument]
    fields = [
        'tags',
        'name',
    ]

    facets = [
        ('tags', TermsFacet(field='tags', size=100)),
        ('type', TermsFacet(field='type', size=20, order={'_key': 'asc'})),
        ('version', TermsFacet(field='version',
                               size=100,
                               order={'_key': 'asc'})),
        ('num_emissions',
         RangeFacet(field='num_emissions',
                    ranges=[
                        ('0', (0, 1)),
                        ('1 - 10', (1, 10)),
                        ('11 - 50', (11, 50)),
                        ('More than 50', (51, None)),
                    ])),
        ('last_emission',
         RangeFacet(
             field='last_emission',
             ranges=[
                 ('Last 7 days', (str(
                     (timezone.now() - datetime.timedelta(days=7)).date()),
                                  None)),
                 ('Last month', (str(
                     (timezone.now() - datetime.timedelta(days=30)).date()),
                                 None)),
                 ('More than a month ago',
                  (None,
                   str((timezone.now() -
                        datetime.timedelta(days=30)).date()))),
             ])),
        ('bitrate',
         RangeFacet(field='bitrate',
                    ranges=[('Low', (0, 100)), ('Medium', (100, 200)),
                            ('High', (200, None))])),
        ('samplerate',
         RangeFacet(field='samplerate',
                    ranges=[('Low', (0, 44100)), ('Medium', (44100, 48000)),
                            ('High', (48000, None))])),
        ('encoding', TermsFacet(field='encoding', size=100)),
        ('license', TermsFacet(field='license')),
        ('lyrics_language', TermsFacet(field='lyrics_language')),
        #('key', __paste__),
    ]
示例#2
0
class LabelSearch(BaseFacetedSearch):
    doc_types = [LabelDocument]
    fields = [
        'tags',
        'name',
    ]

    facets = [
        ('tags', TermsFacet(field='tags', size=100)),
        ('country', TermsFacet(field='country',
                               size=500,
                               order={'_key': 'asc'})),
        ('type', TermsFacet(field='type', size=20, order={'_key': 'asc'})),
        ('established',
         RangeFacet(field='year_start',
                    ranges=[
                        ('Before 1940\'s', (0, 1940)),
                        ('40\'s', (1940, 1950)),
                        ('50\'s', (1950, 1960)),
                        ('60\'s', (1960, 1970)),
                        ('70\'s', (1970, 1980)),
                        ('80\'s', (1980, 1990)),
                        ('90\'s', (1990, 2000)),
                        ('2000\'s', (2000, 2010)),
                        ('2010\'s', (2010, 2020)),
                        ('This Year', (2018, 2019)),
                    ])),
    ]
示例#3
0
def get_configured_facets():
    facets = None
    configuration = get_configuration()
    if configuration and hasattr(configuration, 'facets'):
        facets = configuration.facets.split()
    if facets:
        configured_facets = {}
        for facet in facets:
            if ',' in facet:
                field, interval = facet.split(',', 1)
                if ',' in interval:
                    intervals = interval.split(',')
                    ranges = []
                    for interval in intervals:
                        name, numbers = interval.split(':')
                        numbers = numbers.split('-')
                        irange = []
                        for number in numbers:
                            if number.lower() == 'none':
                                irange.append(None)
                            else:
                                try:
                                    irange.append(int(number))
                                except ValueError:
                                    continue
                        irange = tuple(irange)
                        ranges.append((name, irange))
                    configured_facets[field] = RangeFacet(field=field,
                                                          ranges=ranges)
                elif interval in DATE_INTERVALS:
                    configured_facets[field] = DateHistogramFacet(
                        field=field, interval=interval)
                else:
                    try:
                        interval = int(interval)
                        configured_facets[field] = HistogramFacet(
                            field=field, interval=interval)
                    except ValueError:
                        pass
            else:
                configured_facets[facet] = TermsFacet(field=facet + '.keyword')
    else:
        configured_facets = DEFAULT_FACETS
    return configured_facets
示例#4
0
class SouSearch(FacetedSearch):
    index = ES_INDEX_NAME  # Index to search
    fields = ['id_year_number^2', 'title^3', 'full_text']  # Fields to search

    facets = {
        'type': TermsFacet(field='type'),
        'year': RangeFacet(field='year', ranges=[
            ('1922-1929', (1922, 1930)),
            ('1930-1939', (1930, 1940)),
            ('1940-1949', (1940, 1950)),
            ('1950-1959', (1950, 1960)),
            ('1960-1969', (1960, 1970)),
            ('1970-1979', (1970, 1980)),
            ('1980-1989', (1980, 1990)),
            ('1990-1999', (1990, 2000)),
            ('2000-2009', (2000, 2010)),
            ('2010-2019', (2010, 2020)),
            ('2020-', (2020, 2030)),
            ]),
    }

    def highlight(self, search):
        return search.highlight('title', 'full_text', fragment_size=150, number_of_fragments=4)

    def search(self):
        s = super(SouSearch, self).search()
        # Don't include the actual fulltext in result; we only need the highlighted extract
        return s.source(excludes=["full_text"])

    def query(self, search, query):
        if query:
            if self.fields:
                return search.query('query_string', fields=self.fields, query=query, default_operator='and')
            else:
                return search.query('query_string', query=query, default_operator='and')
        return search
示例#5
0
class PlaylistSearch(BaseFacetedSearch):
    doc_types = [PlaylistDocument]
    fields = [
        'tags',
        'name',
    ]

    facets = [
        ('tags', TermsFacet(field='tags', size=240)),
        ('type', TermsFacet(field='type', size=100, order={'_key': 'asc'})),
        ('status', TermsFacet(field='status', size=100, order={'_key':
                                                               'asc'})),
        ('weather', TermsFacet(field='weather',
                               size=100,
                               order={'_key': 'asc'})),
        ('seasons', TermsFacet(field='seasons',
                               size=100,
                               order={'_key': 'asc'})),
        ('daypart_days',
         TermsFacet(field='daypart_days', size=100, order={'_key': 'asc'})),
        ('daypart_slots',
         TermsFacet(field='daypart_slots', size=100, order={'_key': 'asc'})),
        ('target_duration', TermsFacet(field='target_duration', size=100)),
        ('num_emissions',
         RangeFacet(field='num_emissions',
                    ranges=[
                        ('0', (0, 1)),
                        ('1 - 10', (1, 10)),
                        ('11 - 50', (11, 50)),
                        ('More than 50', (50, None)),
                    ])),
        (
            'last_emission',
            RangeFacet(
                field='last_emission',
                ranges=[
                    # ('old', (None, '2016-01-01')),
                    # ('2016', ('2016-01-01', '2016-12-31')),
                    # ('2018', ('2018-01-01', '2018-12-31')),
                    # ('recent', ('2019-01-01', None)),
                    ('Last 7 days', (str(
                        (timezone.now() - datetime.timedelta(days=7)).date()),
                                     None)),
                    ('Last month', (str(
                        (timezone.now() - datetime.timedelta(days=30)).date()),
                                    None)),
                    ('More than a month ago',
                     (None,
                      str((timezone.now() -
                           datetime.timedelta(days=30)).date()))),
                    ('More than year month ago',
                     (None,
                      str((timezone.now() -
                           datetime.timedelta(days=365)).date()))),
                    # ('Exclude 48 hours', (
                    #     None,
                    #     str((timezone.now() - datetime.timedelta(hours=48)).date())
                    # )),
                ])),
        (
            'next_emission',
            RangeFacet(
                field='next_emission',
                ranges=[
                    # ('old', (None, '2016-01-01')),
                    # ('2016', ('2016-01-01', '2016-12-31')),
                    # ('2018', ('2018-01-01', '2018-12-31')),
                    # ('recent', ('2019-01-01', None)),
                    ('Next 7 days', (None,
                                     str((timezone.now() +
                                          datetime.timedelta(days=7)).date()))
                     ),
                    # ('Exclude 48 hours', (
                    #     str((timezone.now() + datetime.timedelta(hours=48)).date()),
                    #     None
                    # )),
                ])),
        #('last_emission', DateHistogramFacet(field='last_emission', interval='month')),
        ('flags', TermsFacet(field='state_flags', order={'_key': 'asc'})),
    ]
class TestFacetedFilterField(object):
    test_field_name = "faceted_filter_field"

    @pytest.mark.parametrize(
        ', '.join(['facets', 'context', 'aggs_query']),
        [(None, ['date'], {}),
         ({
             'status':
             TermsFacet(field='status'),
             'date':
             DateHistogramFacet(field='date', interval='year'),
             'range':
             RangeFacet(field='height',
                        ranges=[("few", (None, 2)), ("lots", (2, None))])
         }, ['unknown'], {}),
         ({
             'status': TermsFacet(field='status')
         }, ['status'], {
             'aggs': {
                 '_filter_status': {
                     'aggs': {
                         'status': {
                             'terms': {
                                 'field': 'status'
                             }
                         }
                     },
                     'filter': {
                         'match_all': {}
                     }
                 }
             }
         }),
         ({
             'date': DateHistogramFacet(field='date', interval='year')
         }, ['date'], {
             'aggs': {
                 '_filter_date': {
                     'aggs': {
                         'date': {
                             'date_histogram': {
                                 'field': 'date',
                                 'interval': 'year',
                                 'min_doc_count': 0
                             }
                         }
                     },
                     'filter': {
                         'match_all': {}
                     }
                 }
             },
         }),
         ({
             'range':
             RangeFacet(field='height',
                        ranges=[("few", (None, 2)), ("lots", (2, None))])
         }, ['range'], {
             'aggs': {
                 '_filter_range': {
                     'aggs': {
                         'range': {
                             'range': {
                                 'field':
                                 'height',
                                 'keyed':
                                 False,
                                 'ranges': [{
                                     'key': 'few',
                                     'to': 2
                                 }, {
                                     'key': 'lots',
                                     'from': 2
                                 }]
                             }
                         }
                     },
                     'filter': {
                         'match_all': {}
                     }
                 }
             }
         })])
    def test_queryset(self, facets, context, aggs_query, es_dsl_queryset):
        fld = FacetedFilterField(facets=facets,
                                 field_name=self.test_field_name)

        valid_query = {}
        valid_query.update(aggs_query)

        qs = fld.prepare_queryset(es_dsl_queryset, context)
        ret = qs.to_dict()
        assert ret == valid_query