Problem/Motivation

My Solr Search request returns nothing when the results are filtered by a multiple-word value. For example:

Value: "policy" (results returned)
Value: "public policy" (no results returned)

Solution

If edit the query directly and wrap the field with parentheses (instead of quotes) I get the results I am expecting:

Before (broken): ...&fq=tm_field_resume:"public%20policy"&...
After (working): ...&fq=tm_field_resume:(public%20policy)&...

Note: This technique is described in more detail here: http://stackoverflow.com/a/5800877. The item-type for my field specifically is "fulltext" and it doesn't make sense to change it in this case.

Remaining tasks

Unfortunately, I can't find a way to get Drupal (specifically, Search API Solr) to output a query like that. So my questions are:

  • Can Search API Solr already output values in this format?
  • If not, would I need to patch the module?
  • Any other recommendations?
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bryanbraun’s picture

Title: Formatting field values with parantheses instead of quotes » No results returned when filtering by multi-word value
Issue summary: View changes

Updating issue summary for more clarity.

drunken monkey’s picture

Status: Active » Needs review
Related issues: +#2044393: Changes in search query functionality

Ah, yes, that's actually deliberate, but of course still nonsense. At least 90% of people won't expect this behavior, but rather want a "normal" search in that field when they set a filter.
We actually want to get rid of this in Drupal 8 (cf. #2044393: Changes in search query functionality), but since the behavior isn't really documented/specified/required anywhere, we can probably just fix it in Drupal 7, too.

Patch attached, please see if it works for you.

drunken monkey’s picture

bryanbraun’s picture

Status: Needs review » Reviewed & tested by the community

I tried the patch and it works exactly as expected. Thanks! I'd love to get this patch applied.

drunken monkey’s picture

Category: Support request » Bug report
Status: Reviewed & tested by the community » Fixed

Good to hear, thanks for testing!
Committed.

  • drunken monkey committed 1315cf5 on 7.x-1.x
    Issue #2333133 by drunken monkey: Fixed behavior of filters on fulltext...

Status: Fixed » Closed (fixed)

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

Garrett Albright’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Status: Closed (fixed) » Needs review
FileSize
1.09 KB

This seems to be causing a conflict with the patch from #1773440: Use q.alt instead of fq for “filter only” queries which is causing differing behavior on my local machine before and after the commit that introduces this patch. I have a View with exposed filters, one of which is the "State" field from an Addressfield. Compare the query before:

14257625 [qtp24433162-21] INFO  org.apache.solr.core.SolrCore  – [collection1] webapp=/solr path=/select params={q.alt=(tm_field_contact_address$administrative_area:"OR")+(index_id:"member_index")+(hash:ffg1x1)&json.nl=map&fl=item_id,score&start=0&sort=sort_search_api_aggregation_1+asc&rows=25&wt=json} hits=39 status=0 QTime=2 

to after:

14158093 [qtp24433162-17] INFO  org.apache.solr.core.SolrCore  – [collection1] webapp=/solr path=/select params={q.alt=(tm_field_contact_address$administrative_area:(OR))+(index_id:"member_index")+(hash:ffg1x1)&json.nl=map&fl=item_id,score&start=0&sort=sort_search_api_aggregation_1+asc&rows=25&wt=json} status=400 QTime=1 

…though the latter line is proceeded with an org.apache.solr.parser.ParseException barf - the "OR" in the query is now throwing it off.

(Incidentally, the "OR" is already throwing off Acquia's Solr configuration even before this patch, which I can't replicate locally… yeah, last couple days have been a world of Solr hurt.)

Attached patch escapes "AND", "OR", and "NOT" to bring back the previous behavior… locally, anyway.

Garrett Albright’s picture

FileSize
738 bytes

Oops - that patch included an extra experiment to fix my woes. Here's just the patch for this issue.

drunken monkey’s picture

Status: Needs review » Fixed

Oh, brilliant! Didn't know you could actually escape whole words with a backslash – you never stop learning … (I also quickly verified that it works in Solr 3.x as well.)
So, thanks for the new information and the great patch – looks very good!
Committed. Thanks again!

However, apart from this problem, for indexing state abbreviations, type "String" is much better suited than "Fulltext", since you don't want stemming or anything on that – just finding that exact letter combination. By indexing the field as "String", you could also have avoided this problem.

Status: Fixed » Closed (fixed)

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

grota’s picture

I just stumble on this issue, this change is breaking one of my queries.
Granted, I might be doing things wrong.
I have a simple "ss_" text field set up as String (not Fulltext) in the index.
I was doing a query on solr on that text field (with start=0 rows=1) to check if there was on solr a document with that specific and precise field value.

Now with the parenthesis behavior the search simply does not return nothing if the field set up as String (not Fulltext) in the index.
If on the other hand the field is configured as Fulltext the search works by being more fuzzy and returning more results (but that's not the behavior I want).

So I'm wondering if the quoting

      case 'text':
        return '(' . call_user_func(array($this->connection_class, 'escape'), $value) . ')';

should be done only for fulltext fields and not String fields?

Or simply I'm doing it wrong and there's a better way to do the query I want?