Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
is it possible to set OR operator in searched phrases in search form? I would like to set result in that way, so it find all nodes that contain at least one word typed bu user in search form.
For example, I have 2 nodes, which titles are: "Africa", "Uganda". I would like to set, that when users type both of words, the both nodes will be in result.
I tried to set it in Search API administration. I tried to edit "Query type" on Edit search page.
Is it possible to set in in user interface?
thanks
Tomas
Comments
Comment #1
drunken monkeyAre you using search pages or search views?
If you are using pages, please note that they will soon be removed from the core Search API project (in the next beta release) and you'll then have to download the module from the Search pages project. Also, you'd be in luck: #1261082: Conjunction option for search pages will add exactly that. You can try that patch out by applying it to the contrib/search_api_page folder. Please report back there whether it works.
Comment #2
tomas.teicher CreditAttribution: tomas.teicher commentedI am using search views. If it is possible, I would like to use OR operator with search views.
//edit: I have creatted search page, but I would prefer to use search views. As I understand it correctly, the "search views" means that I can use Views module so search filter can be set with views exposed filters?
//edit: The only reason, I want to use Views is that I want to select what fields to display as result (standard display ifor Drupal search is node teaser what doesn't sutosfy me)
Comment #3
drunken monkeyYes, that's correct. Just forgot to also add the conjunction as an option. It's a valid feature request, though, I'll fix that when I have time (which will take a bit, though).
Comment #4
Jon PughCan you point me in the right direction for this? I'd be happy to make a patch
Comment #5
kyrannian CreditAttribution: kyrannian commentedSubscribe - would love to see this enabled.
Comment #6
brunodboOk, let's try this.
Attached patch adds a conjunction setting to the 'Fulltext search' filter configuration form (in handler_filter_fulltext.inc), and adds that conjunction to the query as an option using setOption().
Needs review and testing :)
Comment #7
joelrosen CreditAttribution: joelrosen commentedI tried out this patch, and for some reason I'm getting 400 errors from Solr when I try and do a multiple keyword search with "Or" selected. Is there some special configuration I need to do in solrconfig.xml or something to get this patch to work? I've just got it set with the out-of-the-box configuration files from Search API right now. Pointers would be appreciated!
Joel
Comment #8
mErilainen CreditAttribution: mErilainen commentedI also get the same error after applying this patch. But only when I try to use the OR operator, AND works normally.
An error occurred while trying to search with Solr: '400' Status: Bad Request.
This is a part of the working query I have in the Solr log. I have excluded some of the query because I have so many facets defined:
With OR operator I get first this error:
SEVERE: org.apache.solr.common.SolrException: no field name specified in query and no defaultSearchField defined in schema.xml
And the query in the Solr log looks like this:
Not sure what is the problem, but the query structure is very different from the working version above.
Comment #9
mErilainen CreditAttribution: mErilainen commentedThis problem has been fixed in the Search API Solr module's latest dev version, so it's now working with the patch in #6. See this issue for more details #1507818: OR queries throw 400 error.
Comment #10
brunodboThanks for the testing! Indeed, the patch in #6 was made against the latest dev at the time (just like #1261082: Conjunction option for search pages).
Comment #11
drunken monkeyGreat work, thanks!
I'd be happy to commit this. However, one suggestion: could it maybe be better to include this in the operator, so the setting can easily be exposed?
Possible settings for the operator: "contains all of these words", "contains any of these words", "contains none of these words". In theory, there could also be "doesn't contain all of these words" (both OR and NOT), but I don't see that's relevant in practice.
We'd then look at the operator and thus determine what conjunction to use and whether to negate the query. (However, this would only be possible for "Use as: Search keys". I don't know what we should do if a user changes this setting (no matter how we implement it) with "Use as: Search filter". Probably we should at least note this somewhere in the admin UI.)
Comment #12
brunodboYep, good idea to add the conjunction as an operator rather than a filter config option.
Attached patch adds an operator with the options mentioned in #11, passing the chosen operator setting to setOption() to add the conjunction to the query.
Some questions:
- I'm assuming it's ok to use strings like 'AND'/'OR'/'NOT' as array keys in operator_options()? I'm pretty new to views development, so not sure about this.
- Also views related: we should probably set the default value of the operators to 'AND'?
- Concerning the 'keys' vs 'filter' question: I added a description to the operator options form (which might need to be clarified). Other than that, it's probably good to only add the conjunction when the option is set to 'keys'.
- I can't get the 'NOT' query to work. I changed
if ($this->operator != '=') {
to
if ($this->operator == 'NOT') {
on line 48, not sure how that should be implemented though.
Setting this to 'needs work', looking forward to your comments :)
Comment #13
brunodboComment #14
fearlsgroove CreditAttribution: fearlsgroove commentedHere's a re-roll that also adds a conjunction setting for the fulltext argument handler. There's several dupes that could be marked for these two issues as well.
Comment #15
maxchock CreditAttribution: maxchock commentedI have patch into my current system and I can see the operator at the Views->Configure filter criteria
I select the Contains any of these words, saved, but it still behave like before.
This is my site http://victoria.my/products, I want the search_api can show either product from "mattress" OR "sofa" category...
ANy help would be greatly appreciate.
THanks.
Comment #16
brunodboPatch in #14 works fine for me. Attached is a reroll that fixes a whitespace error.
@maxchock: The search box on your site is a Drupal core search box, unless I'm missing something?
Comment #17
drunken monkeyThe patch looks very good on the whole, thanks a lot for all your work!
It just needs to take into account that
NOT
isn't a valid conjunction for Search API queries (onlyAND
andOR
are). Please see and test the attached patch for that.I've also removed "NOT" for the argument (since I don't think it's expected there) and changed the option labels to those of the filter (since I think that would be clearer).
Comment #18
brunodboThanks for the review!
The patch in #17 applies cleanly and works great. However, it still has the 'NOT' conjunction option for the filter, this patch removes that, so we only have the 'AND' and 'OR' options to choose from when configuring the fulltext filter (reading #17, I assume you meant to do that?).
Comment #19
brunodboSorry, just read your comment and patch again and realized that was not what you meant :) (since we still want negation to work).
Attached patch is almost exactly the same as the one in #17, just fixes a comment typo.
Comment #20
drunken monkeyOK, committed.
Thanks again for all your work!