Search for ALL keywords as well as ONE OR MORE OF

Created on 14 May 2024, 6 months ago
Updated 2 June 2024, 6 months ago

Problem/Motivation

This might be close to the same request as https://www.drupal.org/project/search_api/issues/3349653 💬 Is it possible to add alter query to add an "OR" operator condition? Active , but I'm creating this in case there is a nuanced difference, and if so, because I'm asking if it's currently possible.
What I'm trying to accommodate is the following, given an ALL keywords textfield and an ANY.

1. If ALL contains red, white, blue and ANY is empty, then all must be present in the text
2. If ALL contains red, white, blue and ANY contains pizza, beer, then the text must contain red, white blue as well as either pizza or beer
3. If ALL is empty and ANY contains pizza, beer then the text must contain pizza or beer

If see that there is one parse mode. I cannot tell from the examples about programmatic searching whether I can create two groups that complement each other, or if not, do a search based on one field and then search the search results based on the second.

Steps to reproduce

Proposed resolution

Remaining tasks

💬 Support request
Status

Fixed

Version

1.34

Component

General code

Created by

🇺🇸United States j. ayen green

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @j. ayen green
  • Status changed to Fixed 6 months ago
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks for pointing me to that old issue, seems I overlooked it.

    Regarding your question, it would be important to know if you’re talking about a programmatic search, or configuring this in Views (or similar).
    In the former case, you can simply set an array-valued structure as the query keywords, as described in the doc comment of \Drupal\search_api\ParseMode\ParseModeInterface::parseInput(). Something like this should work for your second scenario:

    $query->keys([
      '#conjunction' => 'AND',
      // ALL:
      [
        '#conjunction' => 'AND',
        'red',
        'white',
        'blue',
      ],
      // ANY:
      [
        '#conjunction' => 'OR',
        'pizza',
        'beer',
      ],
    ]);
    

    If either “ALL” or “ANY” are empty, simply omit the respective array.

    In the latter case, you can just add two “Search: Fulltext search” filters, one with operator “Contains all of these words” and one with “Contains any of these words”. If we did our job right, this should already work as expected. (Except if you try to use different searched fields for the two filters, then all bets are off.)
    (You can also try doing that and looking at the keywords structure that is internally generated for this configuration.)

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

Production build 0.71.5 2024