Marquez
Marquez

Reputation: 6039

How to query Solr for empty field or specific value

How to do an OR expression that filters to either blank field or a specific value?

This does not seem to do it:

q=(-PrivacyLevel:*) OR PrivacyLevel:2

Thanks

Upvotes: 10

Views: 21937

Answers (6)

Bandham Manikanta
Bandham Manikanta

Reputation: 2005

The solution worked for me is q = -PrivacyLevel:(*) PrivacyLevel:("2").

Happy coding.

Upvotes: 0

Jack Miller
Jack Miller

Reputation: 7657

The answer is to use double negation as suggested by Maurizio In denmark. Mathematically this is identical to the non-negated query, however, Solr only understands the double negated version:

q=-(-PrivacyLevel:2 PrivacyLevel:*)

Note: This also works for filter queries:

fq=-(-PrivacyLevel:2 PrivacyLevel:*)

Upvotes: 8

Jayendra
Jayendra

Reputation: 52779

Checkout SolrQuerySyntax

Pure Negative Queries :-

-field:[* TO *] finds all documents without a value for field

You can try :-

q=-PrivacyLevel:[* TO *] OR PrivacyLevel:2

Upvotes: 6

user1533634
user1533634

Reputation: 431

See also Solr field query (fq) with AND and OR operator for a potential problem and a workaround with negative query.

Upvotes: 1

mrvol
mrvol

Reputation: 2963

This is worked for me:

fq=(* -type:[ * TO * ]) || (* +type:company)

Upvotes: 3

This should works:

q=(*:* AND -PrivacyLevel:[* TO *]) OR PrivacyLevel:2

Upvotes: 3

Related Questions