0

Is there a way to negate a solr surround query? I'm using it in a fq field, if that gives me more options for negating the results.

fq={!surround}fieldName:2w(foo,bar)

I normally negate a filter by prepending '-', e.g.,

fq=-fieldName:baz

But when you do this with the surround query you get an error:

fq=-{!surround}fieldName:2w(foo,bar)

org.apache.solr.search.SyntaxError: org.apache.lucene.queryparser.surround.parser.ParseException: Encountered "<EOF>" at line 1, column 24.
Was expecting one of:
    <OR> ...
    <AND> ...
    <NOT> ...
    <W> ...
    <N> ...
    ")" ...
    "," ...
    "^" ...

Okay, so maybe '-' is not allowed and you can't have a lonely NOT. This works:

q={!surround}AND(fieldName:2w(foo,bar),otherField:baz)

but this fails

q={!surround}AND(NOT(fieldName:2w(foo,bar)),otherField:baz)

org.apache.solr.search.SyntaxError: org.apache.lucene.queryparser.surround.parser.ParseException: Encountered " <NOT> "NOT "" at line 1, column 4.
Was expecting one of:
    <OR> ...
    <AND> ...
    <W> ...
    <N> ...
    "(" ...
    <TRUNCQUOTED> ...
    <QUOTED> ...
    <SUFFIXTERM> ...
    <TRUNCTERM> ...
    <TERM> ...
ajo2995
  • 1
  • 3

2 Answers2

1

As indicated in the "Was expecting one of" bit, Surround doesn't support +/- syntax, but it does have NOT:

NOT(fieldName:baz)

Careful of that lonely NOT, though.

Community
  • 1
  • 1
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
0

As of 8.2 (and unlikely to be fixed by 8.3) the syntax of NOT is incorrectly documented as "unary" it is in fact binary and requires a preceding term, so it likes

foo NOT bar

to find things with foo and not bar, so your query would be

{!surround}fieldName:2w(foo,bar) NOT (otherField:baz)

The last parenthesis around the field appear to be required, for no good reason. I did test that Solr will accept this query. I didn't have data handy to verify that it gives exactly the result you hoped for.

Gus
  • 6,719
  • 6
  • 37
  • 58