Problem/Motivation

When using views to submit a search, we need an optional option to choose if we want to search in attached files too or not.

Proposed resolution

Support for views is done in D7.

Remaining tasks

Port this issue to D8

Original report by [dolu]

Hello,

I would like to know if an option can be configured to let the user choose if he wants to include search into files in the results.

Thank you!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Ludo.R’s picture

Issue summary: View changes
izus’s picture

i think this may be part of 'tools' that are used to execute the search, like search api pages or views, maybe search_api_attachments can provide a views filter for this...i'm just dropping ideas, if you have some architected solution please share it to know if we can have this in. or even better, if we can see some code :)

Thanks

Ludo.R’s picture

Well, thank you for your answer.

I guess that a (exposed) Views filter would do the trick, but this is a just a thought.
If I need to investigate further on (and implement) that feature, hopefully I'll provide some code to review.

Thanks!

Ludo.R’s picture

Hi,

I need to implement this feature.

Could you provide me some basic information on how should I proceed?

Building a Views plugin/handler is ok, but how should I talk with SearchAPI to tell him not to return file results, or return file results only?

Thanks!

izus’s picture

hi,
i think that what is need here is a views exposed filter.
i'm not sure, but i think it's possible to have this feature without diving in search api. Only diving in views:
actually in the view configuration we can configure which fields we want to search throw.
the idea is to do some 'live' reconfiguration of that before sending the request. some hook_views_query_alter probably

Ludo.R’s picture

Ok, then this solution would be specific to search_api_views (which seems to be the default search results display as it is shipped with search_api).
But then probably a solution would be needed for search_api_page. And in that case one can't use hook_views_query_alter to filter out results.

So I guess that a generic function should be created that would filter out results by hooking into Search API. Then a views filter for search_api_views and a hook_form_alter for search_api_page.

How does it sound to you?

If you can give me some tips on how to implement the Search API filter function, I would be happy to implement it as well as the views plugin.

Otherwise, I'll implement your first thought in my custom site_helper module (I can share code though is someone wants).

izus’s picture

i'm ok for both solutions

supporting views and search_pages is ok too (there aren't many tools other that those i think)
to let the user choose the behaviour before submitting the form, the first solution seems natural for me; The tool (views, search_page) should provide the filter and honor it.

the second solution will require the tool to provide the filter and search_api to honor the filter. why not after all if well documented.

for the search_api filter, i'm not very familiar with it, but if i should develop one, i would start from one of those filters implemented by search_api itself. #drupal-search-api chan server can help too i guess.

for both solutions, seeing some code here and adding this feature is much appreciated.

Thanks

Ludo.R’s picture

My approach for now, is to override the Search API Views's fulltext search field, which contains configuration to include/exclude fields.
So it would be quite easy to reconfigure those fields at runtime.

However I wonder if this solution is generic enough.
It sounds weird but one could have a search view without the fulltext search field.

So I'm asking myself if there should be a new filter, on its own, but then I need to find a new way to alter the query.

What do you think?

izus’s picture

you're right! only fulltext may be restrictive
i'm really not very familiar with search_api deep integration, try asking the serach_api guys in #drupal-search-api irc chan or in a support request issue.
as said on #7 we will still need a views filter and form_alter filter options if we want the user to be able to switch from searching only in files/ files and content/ only content. i don't know if a search_api filter can expose those.

Ludo.R’s picture

I'm stuck with the views plugin declaration.

Right now I'm implementing hook_views_plugin():

/**
 * Implements hook_views_plugins().
 */
function search_api_attachments_views_plugins() {
  return array(
    'access' => array( // I'm note sure about this...
      'search_attachments' => array(
        'title' => t('Filter attachments search results'),
        'handler' => 'search_api_attachments_views_handler_attachments_filter',
        'path' => drupal_get_path('module', 'search_api_attachments') . '/includes',
        'parent' => 'views_handler_filter',
      ),
    ),
  );
}

But I this doesn't appear as a views filter, since I don't how to tell this should be a filter.

This is from search_api_views.views.inc:

$table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilter';

This lets me believe that I cant' add a filter, it can only be overriden.

Well, as you can understand, I need some directions here to go further.

izus’s picture

hi,
try finding an existing module that adds a views filter and adapt from it. that's what i will do in your place. per example here is an article that seems to have a good example http://precessionmedia.com/blog/how-create-custom-filter-handler-views

Ludo.R’s picture

Here is a first try.

It works as is, but still needs to be improved at the UI level: finding the right options (include/exclude, both?), labels, exposed options...

Ludo.R’s picture

Sorry, 2 files were missing.

Don't use #12 patch! (it simply won't work).

Ludo.R’s picture

Status: Active » Needs review
izus’s picture

Status: Needs review » Needs work

hi,
i can't seem to test the feature here.
i can see the filter in the list of filters in views ui but can't configure it or test it

izus’s picture

Status: Needs work » Needs review
FileSize
4.06 KB

ok
i found out why i couldn't have it working.
it's because of the table name length in hook_views_data_alter
i renamed it saa_exclude_attachments

also moved the filter to views/includes folder and reworked a little some methods in the filter.

please test it and add anything it seems needed for you.

i personally tested, and it seams ok for views. we can focus on this patch and after that add another one for search pages.

Grimreaper’s picture

Status: Needs review » Reviewed & tested by the community

Hello,

I have tested the patch from comment 16 and it works fine.

Just two remarks:
- Maybe putting the include file in includes/views instead of views/includes. As you want.
- coding standards: Implementation of hook_views_api(). => Implements

Thanks for the patches.

izus’s picture

Title: Exposed option to include search in files or not » Exposed option to include search in files or not for views and search_api_page
Issue summary: View changes
Status: Reviewed & tested by the community » Needs work

changed the summary
butting back to needs work (for suggestions in #17)
will merge the views patch when it's ready and wait for a patch for search_api_page :)

Grimreaper’s picture

Status: Needs work » Needs review
FileSize
5.93 KB

Ok,

Here is the patch with the modifications and some coding styles fix.

  • izus committed 2d79334 on 7.x-1.x authored by dolu
    Issue #2507491 by dolu, izus, Grimreaper: Exposed option to include...
izus’s picture

Issue summary: View changes
Status: Needs review » Active

merged the view support and update issue summary with remaining tasks :

Support for views is merged but needs documentation in README file
Support for search_api_page is to be done.

Thanks all

  • izus committed c3cf047 on 7.x-1.x
    Issue #2507491 by izus: Document views include/exclude attachments...

  • izus committed 0b50d8d on 8.x-1.x
    Issue #2507491 by izus: Document views include/exclude attachments...
izus’s picture

Issue summary: View changes
izus’s picture

Issue summary: View changes
izus’s picture

just found a nasty error:
in the views exposed form page.
the value of text form : nothing, just leave it empty
the value of the exclude search in attechments checkbox : checked

when submitting the form, a nasty error is shown.

will upload a patch for this right now

  • izus committed 1a81eae on 7.x-1.x
    Issue #2507491 by izus: Ensure there are fields to exclude before doing...

  • izus committed aaaf3ae on 8.x-1.x
    Revert "Issue #2507491 by izus: Document views include/exclude...
izus’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Category: Support request » Task

as nobody seems interested in adding support for search_api_page + search_api_page will probabely not be ported to D8 as views is now in core (https://www.youtube.com/watch?feature=player_detailpage&v=aUrdJLPq1FQ#t=...), i will not keep this task for D7.
if someone is interested in it, please create a new task for it.

i'm moving this task to D8 to add views support.

izus’s picture

Issue summary: View changes
izus’s picture

Title: Exposed option to include search in files or not for views and search_api_page » Exposed option to include search in files or not for views
izus’s picture

Category: Task » Feature request
Grimreaper’s picture

Hello,

Here is my first attempt.

I managed to get a filter and to add it to the view.

Before dealing with the exposed filter option. I tried to force the exclusion.

I can't figure out how to alter my other fulltext exposed filter.

Maybe we can add an option to the filter to select specific fulltext fields. That's for later when the basic feature will work.

Thanks for any help.

PS: I had a lot of problems configuring the search api attachments settings form. We can talk about it in another issue or IRL.

izus’s picture

OMG, that's a lot of code ! i didn't dug yet in drupal 8 views api, so no clue on this for now.
keep going :)
and yes we can talk about the configuration IRL or via issue queue.

Grimreaper’s picture

LOL :D

A big part of the code is a copy/paste from:
- search_api/src/Plugin/views/filter/SearchApiFulltext.php
and
- search_api/src/Plugin/views/filter/SearchApiFilterBoolean.php

I hesitated which one to extends, then I chose SearchApiFilterBoolean.

Don't loose time inspecting the commented code. :)

Before I forget:

About the errors on the settings form, I had that:
- "expected parameter to be array, null given"
- some array keys non-existing
Mainly due to when installing, extraction_method = '' (cf. config/install yml files) and the code is not managing that.

izus’s picture

Status: Needs work » Needs review
FileSize
1.96 KB

Hi,
now that views API seems stable, i gave it another try.
following patch adds a boolean select box to exclude/include search in attachments.
(i'd love to make it just a checkbox but for now i'm glad we have sth that seems working)

  • izus committed 06d086e on 8.x-1.x
    Issue #2507491 by izus, dolu, Grimreaper: Exposed option to include...
izus’s picture

Status: Needs review » Fixed

merged it
Thanks all

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.