How to do an IN query in Solr?
Asked Answered
T

3

34

i'm having documents with a multivalued field "sharedTo" which contains the groups that the document is shared to. Now I want to find all documents that are shared to at least one of a list of given groups. E.g. I want to find all documents that are shared to the group "foo" or the group "bar" or both. Currently I'm building a query like this:

sharedTo:"foo" OR sharedTo:"bar"

For each group I just add a new OR query part. This works, however I wonder if there is a more efficient way of doing this like a

sharedTo IN ('foo', 'bar')
Tallbot answered 11/5, 2011 at 7:17 Comment(0)
S
66

if your default operator is OR, then you can just give the query as

sharedTo:('foo' 'bar')

If your default operator is AND, then you'll have to do it like this: sharedTo:(foo OR bar)

Sanious answered 11/5, 2011 at 9:47 Comment(3)
Anyone know if there is a practical or hard limit to the number of terms here? (i.e. 'foo' 'bar')Orizaba
@Orizaba Yes there is an upper limit to this, it's mostly based on the size of the URL that Solr will accept. If the URL is too long it will return a 403 Forbidden error.Illeetvilaine
You don't need the quotation marks around the terms btw.Illeetvilaine
C
1

If you're using SolrJ as client your query would look like as follows:

fq=sharedTo:("foo" OR "bar")

Hope this helps

Charkha answered 10/5, 2021 at 8:24 Comment(0)
I
0

The above solution did not work for me. But this syntax did

column_name:+(value1 value2)

http://harmssite.com/2012/09/solr-query-for-mimicking-sql-in-operator-functionality/

Indestructible answered 21/9, 2016 at 17:20 Comment(1)
Unfortunately that link is deadIlleetvilaine

© 2022 - 2024 — McMap. All rights reserved.