Exemplo n.º 1
0
    def solr_results(self, query=None, batch=True, b_size=10, b_start=0):

        searchable_text = self.request.form.get('SearchableText', '')
        if searchable_text:
            query = make_query(searchable_text)
        else:
            query = u'*:*'

        filters = self.solr_filters()
        if 'trashed' not in self.request.form:
            filters.append(u'trashed:false')

        params = {
            'fl': [
                'UID', 'Title', 'getIcon', 'portal_type', 'path',
                'containing_dossier', 'id', 'created', 'modified',
                'review_state', 'bumblebee_checksum',
            ],
            'hl': 'on',
            'hl.fl': 'SearchableText',
            'hl.snippets': 3,
        }

        solr = getUtility(ISolrSearch)
        resp = solr.search(
            query=query, filters=filters, start=b_start, rows=b_size,
            sort=self.solr_sort(), **params)
        results = OGSolrContentListing(resp)

        if batch:
            results = Batch(results, b_size, b_start)
        return results
Exemplo n.º 2
0
    def search(self, query, limit=20):
        """Search in solr (if the solr feature is enabled) but then convert
        solr results to brains and continue working with them.

        Brains are obtained using path/rid which is expected to be performant
        enough. Search results are usually limited to 10.
        """
        if not is_solr_feature_enabled():
            for each in super(SolrObjPathSource, self).search(query,
                                                              limit=limit):
                yield each
            return

        results = self.solr.search(
            query=make_query(query),
            filters=self.make_solr_filters(self.selectable_filter.criteria),
            rows=limit,
            fl=['path'],
        )

        from opengever.base.solr import OGSolrContentListing  # XXX: FIXME!
        for solr_doc in OGSolrContentListing(results):
            brain = self._getBrainByToken(solr_doc.getPath())
            if brain:
                yield self.getTermByBrain(brain, real_value=False)
Exemplo n.º 3
0
    def reply(self):
        search_term = self.request.form.get('q', None)
        limit = int(self.request.form.get('limit', 10))
        portal_path = '/'.join(api.portal.get().getPhysicalPath())
        path = '{}/{}'.format(
            portal_path,
            self.request.form.get('path', '/').lstrip('/'),
        ).rstrip('/')

        if not search_term:
            return []

        if api.portal.get_registry_record('use_solr',
                                          interface=ISearchSettings,
                                          default=False):
            view = getMultiAdapter((self.context, self.request),
                                   name=u'livesearch_reply')
            view.search_term = search_term
            view.limit = limit
            view.path = path

            return [{
                'title': entry.Title(),
                'filename': entry.filename,
                '@id': entry.getURL(),
                '@type': entry.portal_type,
            } for entry in OGSolrContentListing(view.results())]

        else:
            del self.request.form['q']

            # Strip VHM path because plone.restapi SearchHandler will add it
            vhm_physical_path = '/'.join(
                self.request.get('VirtualRootPhysicalPath', ''))
            if vhm_physical_path:
                if path.startswith(vhm_physical_path):
                    path = path[len(vhm_physical_path):]
                    if not path:
                        path = '/'

            self.request.form.update({
                'SearchableText':
                search_term + '*',
                'sort_limit':
                limit,
                'path':
                path,
                'metadata_fields': ['Title', 'filename', 'id', 'portal_type']
            })

            results = super(GeverLiveSearchGet, self).reply()
            return [{
                'title': entry['title'],
                'filename': entry['filename'],
                '@id': entry['@id'],
                '@type': entry['@type'],
            } for entry in results['items'][:limit]]
Exemplo n.º 4
0
    def reply(self):
        if not api.portal.get_registry_record(
                'use_solr', interface=ISearchSettings):
            raise BadRequest('Solr is not enabled on this GEVER installation.')

        query, filters, start, rows, sort, params = self.make_solr_query()

        requested_fields = params.get('fl')
        if requested_fields:
            requested_fields = (
                set(requested_fields.split(',')) - BLACKLISTED_ATTRIBUTES)
        else:
            requested_fields = DEFAULT_FIELDS

        solr = getUtility(ISolrSearch)
        solr_fields = set(solr.manager.schema.fields.keys())
        requested_solr_fields = set([])
        for field in requested_fields:
            if field in FIELD_MAPPING:
                field = FIELD_MAPPING[field][0]
            requested_solr_fields.add(field)
        params['fl'] = ','.join(
            (requested_solr_fields | REQUIRED_FIELDS) & solr_fields)

        resp = solr.search(
            query=query, filters=filters, start=start, rows=rows, sort=sort,
            **params)

        docs = OGSolrContentListing(resp)
        items = []
        for doc in docs:
            item = {}
            for field in requested_fields:
                # Do not allow access to private attributes
                if field.startswith("_"):
                    continue
                accessor = FIELD_MAPPING.get(field, (None, field))[1]
                value = getattr(doc, accessor, None)
                if callable(value):
                    value = value()
                item[field] = json_compatible(value)
            items.append(item)

        res = {
            "@id": "{}?{}".format(
                self.request['ACTUAL_URL'], self.request['QUERY_STRING']),
            "items": items,
            "items_total": resp.num_found,
            "start": start,
            "rows": rows,
        }

        if 'facet_counts' in resp.body:
            res['facet_counts'] = resp.body['facet_counts']
        return res
Exemplo n.º 5
0
    def render_results(self, resp):
        ts = getToolByName(self.context, 'translation_service')
        portal_url = getToolByName(self.context, 'portal_url')()
        portalProperties = getToolByName(self.context, 'portal_properties')
        siteProperties = getattr(portalProperties, 'site_properties', None)
        useViewAction = []
        if siteProperties is not None:
            useViewAction = siteProperties.getProperty(
                'typesUseViewActionInListings', [])

        results = OGSolrContentListing(resp)
        self.result_items = []
        self.show_more = {}

        self.legend = ts.translate(legend_livesearch, context=self.request)
        self.nothing_found = ts.translate(label_no_results_found,
                                          context=self.request)
        self.advanced_search_url = portal_url + '/advanced_search?SearchableText=%s' % url_quote_plus(
            self.search_term)
        self.advanced_search_label = ts.translate(label_advanced_search,
                                                  context=self.request)

        for result in results:
            item_url = result.getURL()
            if result.portal_type in useViewAction:
                item_url += '/view'

            item_url = item_url + u'?SearchableText=%s' % url_quote_plus(
                self.search_term)
            title = safe_unicode(result.Title())
            css_klass = get_mimetype_icon_klass(result.doc)
            description = safe_unicode(result.Description()) or u''

            self.result_items.append({
                'url': item_url,
                'title': title,
                'css_klass': css_klass,
                'description': description
            })

        if results.actual_result_count > self.limit:
            # add a more... row
            searchquery = u'@@search?SearchableText=%s&path=%s' % (
                url_quote_plus(self.search_term), self.path)
            title = ts.translate(label_show_all, context=self.request)
            self.show_more = {'url': searchquery, 'title': title}

        return self.template()
Exemplo n.º 6
0
 def test_contentlisting_returns_og_types(self):
     body = """{
         "responseHeader": {"status": 0},
         "response": {
             "numFound": 1,
             "start": 0,
             "docs": [{
                 "UID": "85bed8c49f6d4f8b841693c6a7c6cff1",
                 "Title": "My Item"
             }]
         }
     }"""
     resp = SolrResponse(body=body, status=200)
     listing = OGSolrContentListing(resp)
     self.assertTrue(isinstance(listing[0], OGSolrContentListingObject))
     self.assertTrue(isinstance(listing[0].doc, OGSolrDocument))
Exemplo n.º 7
0
    def render_results(self, resp):
        output = []

        def write(s):
            output.append(safe_unicode(s))

        ts = getToolByName(self.context, 'translation_service')
        portal_url = getToolByName(self.context, 'portal_url')()
        portalProperties = getToolByName(self.context, 'portal_properties')
        siteProperties = getattr(portalProperties, 'site_properties', None)
        useViewAction = []
        if siteProperties is not None:
            useViewAction = siteProperties.getProperty(
                'typesUseViewActionInListings', [])

        results = OGSolrContentListing(resp)
        if not results:
            write('''<fieldset class="livesearchContainer">''')
            write('''<legend id="livesearchLegend">%s</legend>''' %
                  ts.translate(legend_livesearch, context=self.request))
            write('''<div class="LSIEFix">''')
            write('''<div id="LSNothingFound">%s</div>''' %
                  ts.translate(label_no_results_found, context=self.request))
            write('''<div class="LSRow">''')
            write('<a href="%s" style="font-weight:normal">%s</a>' %
                  (portal_url + '/advanced_search?SearchableText=%s' %
                   url_quote_plus(self.search_term),
                   ts.translate(label_advanced_search, context=self.request)))
            write('''</div>''')
            write('''</div>''')
            write('''</fieldset>''')
        else:
            write('''<fieldset class="livesearchContainer">''')
            write('''<legend id="livesearchLegend">%s</legend>''' %
                  ts.translate(legend_livesearch, context=self.request))
            write('''<div class="LSIEFix">''')
            write('''<ul class="LSTable">''')
            for result in results:

                itemUrl = result.getURL()
                if result.portal_type in useViewAction:
                    itemUrl += '/view'

                itemUrl = itemUrl + u'?SearchableText=%s' % url_quote_plus(
                    self.search_term)

                write('''<li class="LSRow">''')
                full_title = safe_unicode(result.Title())
                if len(full_title) > MAX_TITLE:
                    display_title = ''.join((full_title[:MAX_TITLE], '...'))
                else:
                    display_title = full_title

                full_title = full_title.replace('"', '&quot;')

                css_klass = get_mimetype_icon_klass(result.doc)

                write('''<a href="%s" title="%s" class="%s">%s</a>''' %
                      (itemUrl, full_title, css_klass, display_title))
                display_description = safe_unicode(result.Description()) or u''
                if len(display_description) > MAX_DESCRIPTION:
                    display_description = ''.join(
                        (display_description[:MAX_DESCRIPTION], '...'))

                # need to quote it, to avoid injection of html containing javascript and other evil stuff
                display_description = html_quote(display_description)
                write('''<div class="LSDescr">%s</div>''' %
                      (display_description))
                write('''</li>''')
                full_title, display_title, display_description = None, None, None

            write('''<li class="LSRow">''')
            write('<a href="%s" style="font-weight:normal">%s</a>' %
                  (portal_url + '/advanced_search?SearchableText=%s' %
                   url_quote_plus(self.search_term),
                   ts.translate(label_advanced_search, context=self.request)))
            write('''</li>''')

            if resp.num_found > self.limit:
                # add a more... row
                write('''<li class="LSRow">''')
                searchquery = u'@@search?SearchableText=%s&path=%s' % (
                    url_quote_plus(self.search_term), self.path)
                write(u'<a href="%s" style="font-weight:normal">%s</a>' %
                      (searchquery,
                       ts.translate(label_show_all, context=self.request)))
                write('''</li>''')

            write('''</ul>''')
            write('''</div>''')
            write('''</fieldset>''')
        return '\n'.join(output).encode('utf-8')