Ejemplo n.º 1
0
    def search(self, query, highlight=False, **kwargs):
        self.open()
        if isinstance(query, basestring):
            prepared_query = {
                'query': {
                    'query_string': { 'query': clean_query_string(query) }
                }
            }

        else:
            prepared_query = query

        if 'project' in kwargs:
            project = kwargs.pop('project')
            prepared_query['filter'] = {
                "term": { 'project': project }
            }

        if highlight:
            prepared_query['highlight'] = {
                'number_of_fragments': 3,
                'fragment_size': 150,
                'no_match_size': 150,
                'fields': {'text': {}, 'label': {}, 'abstract': {}},
                'pre_tags': ['<span class="highlighted">'],
                'post_tags': ['</span>']
            }

        #pprint.pprint(prepared_query)
        return self.open().search(prepared_query, index=self.name, **kwargs)
Ejemplo n.º 2
0
    def filter_search(self, request, search, view):
        q = request.query_params.get('q', None)
        if not q:
            return search

        # search = search.highlight('_all').highlight_options(**{
        #     'pre_tags': ['&lt;strong&gt;'],
        #     'post_tags': ['&lt;/strong&gt;']
        # })

        return search.query('match', display_title={
            'query': clean_query_string(q),
            'operator': 'and',
            'fuzziness': '0.3'
        })
Ejemplo n.º 3
0
    def filter_search(self, request, search, view):
        q = request.query_params.get('q', None)
        if not q:
            return search

        # search = search.highlight('_all').highlight_options(**{
        #     'pre_tags': ['&lt;strong&gt;'],
        #     'post_tags': ['&lt;/strong&gt;']
        # })

        return search.query('match',
                            display_title={
                                'query': clean_query_string(q),
                                'operator': 'and',
                                'fuzziness': '0.3'
                            })
Ejemplo n.º 4
0
    def filter_queryset(self, request, queryset, view):
        query = BASE_QUERY.copy()
        filters = []
        params = request.QUERY_PARAMS

        if hasattr(request, 'project') or 'project' in params:
            project = params.get('project', request.project.slug)
            filters.append({'term': {'serialized.project.url':
                                     '/projects/{}/'.format(project)}})

        term = clean_query_string(params.get('autocomplete').lower()).split()

        query['query']['filtered']['query'] = {
            'bool': { 'must': [], 'should': [] }
        }

        must = query['query']['filtered']['query']['bool']['must']
        should = query['query']['filtered']['query']['bool']['should']

        if len(term) > 1:
            must.append({
                'match': {
                    'display_title': {
                        'query': ' '.join(term[:-1]),
                        'operator': 'and',
                        'fuzziness': '0.2'
                    }
                }
            })

        must.append({
            'prefix': {
                'display_title': {
                    'value': term[-1]
                }
            }
        })

        should.append({
            'match_phrase': {
                'display_title': params.get('autocomplete').lower()
            }
        })

        should.append({
            'match': {
                'display_title': {
                    'query': ' '.join(term),
                    'operator': 'and'
                }
            }
        })

        query['fields'] = ['display_title', 'display_url']

        if filters:
            query['query']['filtered']['filter'] = { 'and': filters }

        if params.get('highlight'):
            query['highlight'] = {
                'fields': {'display_title': {'number_of_fragments': 0}},
                'pre_tags': ['&lt;strong&gt;'],
                'post_tags': ['&lt;/strong&gt;']
            }

        if hasattr(view, 'model') and view.model is not None:
            return en_index.search_model(view.model, query)
        else:
            # Should boost notes most of all
            return en_index.search(query)
Ejemplo n.º 5
0
    def filter_queryset(self, request, queryset, view):
        query = BASE_QUERY.copy()
        filters = []
        params = request.QUERY_PARAMS

        if hasattr(request, 'project') or 'project' in params:
            project = params.get('project', request.project.slug)
            filters.append({
                'term': {
                    'serialized.project.url': '/projects/{}/'.format(project)
                }
            })

        term = clean_query_string(params.get('autocomplete').lower()).split()

        query['query']['filtered']['query'] = {
            'bool': {
                'must': [],
                'should': []
            }
        }

        must = query['query']['filtered']['query']['bool']['must']
        should = query['query']['filtered']['query']['bool']['should']

        if len(term) > 1:
            must.append({
                'match': {
                    'display_title': {
                        'query': ' '.join(term[:-1]),
                        'operator': 'and',
                        'fuzziness': '0.2'
                    }
                }
            })

        must.append({'prefix': {'display_title': {'value': term[-1]}}})

        should.append({
            'match_phrase': {
                'display_title': params.get('autocomplete').lower()
            }
        })

        should.append({
            'match': {
                'display_title': {
                    'query': ' '.join(term),
                    'operator': 'and'
                }
            }
        })

        query['fields'] = ['display_title', 'display_url']

        if filters:
            query['query']['filtered']['filter'] = {'and': filters}

        if params.get('highlight'):
            query['highlight'] = {
                'fields': {
                    'display_title': {
                        'number_of_fragments': 0
                    }
                },
                'pre_tags': ['&lt;strong&gt;'],
                'post_tags': ['&lt;/strong&gt;']
            }

        if hasattr(view, 'model') and view.model is not None:
            return en_index.search_model(view.model, query)
        else:
            # Should boost notes most of all
            return en_index.search(query)