SearchApiStandard Contextual Filter does not use multiple values correctly.

Steps to Reproduce

  • Assumes content type with multiple taxonomy term reference fields and a search api index with term reference fields using integer index type
  • Setup a search api index with an aggregate field using Union aggregation and select multiple tax term reference fields
  • Re-index
  • Adjust search api content view with aggregate field as contextual filter configured to accept multiple arguments
  • Use view preview to pass multiple working taxonomy term IDs separated by commas
  • Expect result set to to include content items from index that have both passed tax term IDs
  • Observed result set includes content items with either tax term IDs
  • Use view preview to pass multiple working taxonomy term IDs separated by +
  • Expect result set to to include content items from index that have either passed tax term IDs
  • Observed result set includes content items with either tax term IDs, works as expected.
  • If displaying view query, observe the following Conditions
  [
    status = 1
  AND
    aggregated_field IN array (
          0 => '3661',
          1 => '156',
        )
  ]

Notes

  • \Drupal\search_api\Plugin\views\argument\SearchApiStandard::query() adds
    if (count($this->value) > 1) {
      $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
      $this->query->addCondition($this->realField, $this->value, $operator);
    }

Proposed Solution

  • Update \Drupal\search_api\Plugin\views\argument\SearchApiStandard::query() to check for $this->options['break_phrase'] and $this->options['not'] and add query conditions as appropriate using $this->value

Comments

jasonawant created an issue. See original summary.

jasonawant’s picture

Assigned: Unassigned » jasonawant

I will work on a patch.

jasonawant’s picture

Component: Plugins » Views integration
Assigned: jasonawant » Unassigned
Status: Active » Needs review
StatusFileSize
new1.19 KB

Here's a patch to manage multiple arguments passed as 1+2+3 (for OR) or 1,2,3 (for AND) include the negate option. Feedback welcome. Tests are probably needed, and I can work on them with some guidance. Thanks, Jason

drunken monkey’s picture

StatusFileSize
new1.33 KB
new1.85 KB

Thanks for reporting this and already providing a patch!
Weird, though – I could’ve sworn we were already doing that … Maybe it was just in D7, and we forgot to port it?

Anyways, your patch looks great, just a single matter of personal taste:

+++ b/src/Plugin/views/argument/SearchApiStandard.php
@@ -168,9 +168,19 @@ class SearchApiStandard extends ArgumentPluginBase {
+      if ('or' === $this->operator) {

I personally don’t like Yoda conditions, so please don’t use them here.

Otherwise, the patch looks perfect already – thanks again!

You’re right, though, we should have test coverage for this. But it should be just a line – see the attached patch.

jasonawant’s picture

Status: Needs review » Reviewed & tested by the community

Hi,

Thanks for the feedback and test coverage. New patch works as expected, Jason

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for reporting back, good to hear!
Committed.
Thanks again!

drilauri’s picture

Will this be fixed in 7 as well? I receive "Notice: Undefined index: break_phrase in SearchApiViewsHandlerArgument->options_form() (line 96 of .../modules/search_api/contrib/search_api_views/includes/handler_argument.inc)" when editing a More like this view after updating to Search API 7.x-1.25.

drunken monkey’s picture

Status: Fixed » Closed (fixed)

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