Exemple #1
0
    def get_search(self):
        search = Metadata.search(index=constants.METADATA_ALIAS)
        sort_by = self.args.get(
            constants.PARAM_SORT_BY,
            constants.PARAM_DEFAULT_VALUES[constants.PARAM_SORT_BY])
        sort = self.args.get(
            constants.PARAM_SORT,
            constants.PARAM_DEFAULT_VALUES[constants.PARAM_SORT])
        if sort_by != constants.SORT_BY_RELEVANCE:
            sort_by_mapping = constants.SORT_BY_MAPPING[sort_by]
            sort_mapping = constants.SORT_MAPPING[sort]
            sort_query = f'{sort_mapping}{sort_by_mapping}'
            search = search.sort(sort_query)
        search = self.setup_query(search)
        offset = self.args.get(
            constants.PARAM_OFFSET,
            constants.PARAM_DEFAULT_VALUES[constants.PARAM_OFFSET])
        limit = self.args.get(
            constants.PARAM_LIMIT,
            constants.PARAM_DEFAULT_VALUES[constants.PARAM_LIMIT])
        search = search[offset:limit + offset]
        for arg, field in constants.FILTER_ARGS.items():
            search = self.add_filters(search, arg, field)

        if self.args.get(constants.PARAM_QUERYSTRING):
            search = search.extra(
                min_score=MetadataConfig.get_solo().min_score)
        return search
Exemple #2
0
 def setup_query(self, search: Search):
     queries = []
     for field, values in MetadataConfig.get_solo().query_config.items():
         queries.append(
             Q('bool',
               should=Q(
                   'match',
                   **{field: self.args.get(constants.PARAM_QUERYSTRING)}),
               **values))
     return search.query('dis_max', queries=queries)
    def get_search(self):
        search = Metadata.search(index=constants.METADATA_ALIAS)
        # search = search.sort('-hits')
        search = self.setup_query(search)
        offset = self.args[constants.PARAM_OFFSET]
        limit = self.args[constants.PARAM_LIMIT]
        search = search[offset:limit + offset]
        for arg, field in constants.FILTER_ARGS.items():
            search = self.add_filters(search, arg, field)

        if self.args.get(constants.PARAM_QUERYSTRING):
            search = search.extra(
                min_score=MetadataConfig.get_solo().min_score)
        return search
Exemple #4
0
    def test_min_score(self):
        query = FieldSearchQuery(args={'limit': 0, 'start': 0, 'q': 'hola'})

        search = query.get_search()
        min_score = search.to_dict()['min_score']
        self.assertEqual(min_score, MetadataConfig.get_solo().min_score)