Problem/Motivation

It would be really nice if we can do queries like this:
http://example.com/node.json&field_tags=12,16 (to get nodes tagged with term id 12 OR 16)
http://example.com/node.json&field_tags=12+16 (to get nodes tagged with term id 12 AND 16)

For instance, ContribKanban wanted to say "With tags `accessibility` AND `SprintWeekend2017`". However passing both tids does not accomplish the needs.

For d8rules it would make sense to list both
- issues with the d8rules tag globally https://www.drupal.org/project/issues/search/?issue_tags=d8rules
- issues which are in the Rules repository https://www.drupal.org/project/issues/search/rules?version%5B%5D=8.x

Show all the critical, needs review|needs work|active|rtbc|etc. issues for drupal 8.0.x

Show all the critical, rtbc issues for drupal 8.0.x that are in tag "D8 upgrade path" but not "Needs critical triage"

It is possible to use multiple values for a query filter.

OR

?id[]=1&?id=[] works for an OR
using
?property[]=VALUE
?field_name[column][]=VALUE

Example: https://www.drupal.org/api-d7/taxonomy_term.json?tid[]=25130&tid[]=26976

Example: 10 nodes that have high priority https://www.drupal.org/api-d7/node.json?field_issue_priority[value][]=10...

AND

Proposed resolution

From GitHub's API to query a repo's issues - https://developer.github.com/v3/issues/. Their API uses "," as an AND operator for querying issues via labels.

Remaining tasks

Figure out what some common standard patterns are.

User interface changes

API changes

Data model changes

Comments

webchick’s picture

Priority: Normal » Major

Agreed that this would be extremely useful. I've been playing around with some of the sample queries at https://www.drupal.org/api and as far as I can tell, there's no way to do things like:

Show all the critical, needs review|needs work|active|rtbc|etc. issues for drupal 8.0.x

Show all the critical, rtbc issues for drupal 8.0.x that are in tag "D8 upgrade path" but not "Needs critical triage"

mglaman’s picture

Yes, I would love it to be TID+TID+TID or using pipes (|). It would allow Contrib Kanban (http://contribkanban.com/) to cut down it's request. Each list is its own configuration, and multiple status IDs. This causes it to have to make a request for each status to get a decent amount of data.

drumm’s picture

Issue tags: +affects drupal.org
mglaman’s picture

After doing some poking, I don't think we can use "+" as a determinate, because that is a valid character - https://www.drupal.org/api-d7/taxonomy_term.json?name=Imagefield_CSS+Panels. And the endpoint doesn't care what kind of data a field is (at brief glance at code, haven't actually tested so pardon if incorrect.)

It seems like using commas is proper approach. The autocomplete module uses it as a determinate, as noted from taxonomy_autocomplete()'s documentation

 * @param $tags_typed
 *   (optional) A comma-separated list of term names entered in the
 *   autocomplete form element. Only the last term is used for autocompletion.
 *   Defaults to '' (an empty string).
mglaman’s picture

Highlighting a piece of code in the module when handling field conditions

The following was added through #1707938: Querying filters: specify the field column. Allows fields to be queried for non-normalized table columns. Problem is.. if an value is converted to an array before this, it would break fields

/node.json?field_text[value]=foo
/node.json?field_text[format]=filtered_html

      if (is_array($value)) {
        // Specific column filters are given, so add a query condition for each
        // one of them.
        foreach ($value as $column => $val) {
          $query->$operation($field_info, $column, $val);
        }
      }
mglaman’s picture

Status: Active » Needs review
StatusFileSize
new5.27 KB

Here is a patch which adds support to both AND and OR requests. AND is specified using commas and OR is specified via pipes. I've updated tests as well.

Example:

http://restws.contrib.dev/node.json?nid=10|3 would return nodes 10 and 3.
http://restws.contrib.dev/node.json?field_tags=5,22 would return nodes with tag references to term id 5 and 22.

marcvangend’s picture

Are there other places in Drupal where multiple values are separated by AND / OR operator characters? I can think of one: Views contextual filters. According to https://www.drupal.org/node/1578564:

Values separated by commas are interpreted by Views as AND conditions. Values separated by plus signs are interpreted as OR conditions.

(Yes, that's the opposite of my initial feature request :-))

Let's look for some kind of standard for value separators, either in Drupal core/contrib or elsewhere, and use that.

mglaman’s picture

marcvangend, I had wanted to use plus signs to denote "OR". However, with string based requests I was afraid of complications - see example in #4. I agree keeping to "," for AND and "+" for OR would be best. Let me try fiddling some more :) Thanks for taking a look.

webchick’s picture

I'd encourage us to look 'up a level' and see what's common in REST frameworks out there, since that's what we're attempting to emulate.

mglaman’s picture

From GitHub's API to query a repo's issues - https://developer.github.com/v3/issues/. Their API uses "," as an AND operator for querying issues via labels. Unfortunately I didn't see anything in relation to OR.

dasjo’s picture

Not sure if that would support it, but for d8rules it would make sense to list both
- issues with the d8rules tag globally https://www.drupal.org/project/issues/search/?issue_tags=d8rules
- issues which are in the Rules repository https://www.drupal.org/project/issues/search/rules?version%5B%5D=8.x

mglaman’s picture

Re-rolled #6 to use Views-like + for OR and , for AND

Status: Needs review » Needs work

The last submitted patch, 12: make_or_and_and_queries-2308939-12.patch, failed testing.

btopro’s picture

tested last patch didn't seem to do anything when I issue a call like
node.xml?nid=12+13

This is the proposed usage pattern correct?

mglaman’s picture

I need to revisit my patch. My note in #12 is flip-flopped from summary. Maybe I did in my code too. Which explains test fail.

xeraseth’s picture

A few notes:

  1. + will not work. It appears drupal_get_query_parameters converts the + into a space (that was what happened in my testing).
  2. I was reading a Stackover on best naming conventions. The answer recommended using the format ?id[]=1&id[]=2. I went to start writing a patch to do the "OR" value as this format and to my surprise this format works already.
mglaman’s picture

Can confirm, ?id[]=1&?id=[] works for an OR! See https://www.drupal.org/api-d7/taxonomy_term.json?tid[]=25130&tid[]=26976

Awesome find, and simplifies ContribKanban.com's requests. A LOT.

mglaman’s picture

it appears []= only works on property conditions, and throws a 500 when its a field condition :/ requesting mutliple IDs works, however something like the following errors out: https://www.drupal.org/api-d7/node.json?limit=100&type=project_issue&fie...

btopro’s picture

WOAH, matt, you just totally blew my mind! Can confirm this works with fields too so long as you supply the array value.
example:
node.json?field_name_here[value][]=stuff&field_name_here[value][]=things

btopro’s picture

10 nodes that have high priority
https://www.drupal.org/api-d7/node.json?field_issue_priority[value][]=100&limit=10

mglaman’s picture

Proof this is working using what btopro found: https://contribkanban.com/board/panelizer

Each lane is using OR for the possible statuses to display.

lokapujya’s picture

Does D8 do this?

mglaman’s picture

Title: Make OR and AND queries possible » Make AND queries possible

Changing the title here, since we discovered OR is possible via array values as ?property[]=VALUE or ?field_name[column][]=VALUE. However there's no way to make this an AND query.

For instance, for ContribKanban I wanted to say "With tags `accessibility` AND `SprintWeekend2017`". However passing both tids does not accomplish the needs.

yesct’s picture

Issue summary: View changes

@mglaman so the tests in the patch include both an and and or test?
If a test only patch ran, do you think the or case would pass?

Someone please check the issue summary update.