How to OR null value in Apache Solr query?
Asked Answered
T

4

12

The last option to solve this for me was to ask StackOverflow.

I am trying to create a Solr query to get documents that have a specific value on one of its fields OR that does not have a value...

Theorically, this query should have worked.

Here is some information:

Query: (name: john) --> Result count: 15383 //Johns

Query: (name: {* TO *}) --> Result count: 61013 //People that have a name

Query: -(name: {* TO *}) --> Result count: 216888 //People that do not have a name

Now, when I use first and third query in a same query with OR operator, I expect to get (216888 + 15383) results. But SOLR gives 15383 results, simply ignores the effect of third query:

Query: +((name:john) (-(name:{* TO *}))) //This is the query I was used.

Is this a bug of Solr or am I doing wrong in query? Merging two query results is an additional solution but I do not want to do extra code implementation if I could do it with a simple query.

Any help would be appreciated.

Tatiana answered 6/10, 2011 at 9:9 Comment(0)
C
25

(-name:[* TO *] AND *:*) OR name:john

Calendra answered 6/2, 2012 at 23:43 Comment(3)
This is just what I needed. Thanks. Would you be able to explain what AND *:* does in this example? I see that my search doesn't work if I leave that part out, I'm just not sure why.Wagoner
*:* means all rows. *:* -name:* means all rows without a name value. name:john OR (*:* -name:*) can be translated into SQL as name='john' OR name IS NULLClinkscales
This type of answer really deserves/requires an explanation of the syntax.Kandicekandinsky
S
1

You can also use it like this.

&fq=name:john OR !name:['' TO *]
Shellacking answered 9/2, 2015 at 7:44 Comment(0)
R
0

you can use below to get results which is null and rest

-name:[* TO *] OR *:*
Renee answered 9/6, 2021 at 15:49 Comment(0)
H
-1

try the below query -

q=(name:john OR -(name:[* TO *]))
Hypethral answered 6/10, 2011 at 9:20 Comment(2)
I found the solution in this linkLiu
-(-(name:john)+(name:{* TO *}))Liu

© 2022 - 2024 — McMap. All rights reserved.