24

How can I exclude a set of document IDs from Solr result set? Essentially something like

/select?q=tag_id:367 AND NOT id:(306670,302209)

I tried it and it does not exclude documents with those id's.

arun
  • 10,685
  • 6
  • 59
  • 81

2 Answers2

33

Try this:

/select?q=*:*&fq=tag_id:367 AND id:[* TO *] -id:(306670 302209)

That should allow you to build out add as many ids as you want without having to add -id:302209 every time you want to eliminate an ID. The reverse is also true, you can swap the - with a + and force an array of values to be there as well.

harmstyler
  • 1,381
  • 11
  • 20
  • Thx for suggesting the `q=*:*` format. The one you are suggesting still did not exclude the documents with id's 306670 and 302209. But this one does: `/select?q=*:*&fq=tag_id:367 AND -id:306670 AND -id:302209` Essentially the grouping functionality with parantheses is not working for me. – arun Aug 09 '12 at 16:06
  • 1
    It might be because I added commas for some reason, try separating them with spaces instead. I will also update my code. – harmstyler Aug 09 '12 at 18:28
  • I guess the `AND id:[* TO *]` is unnecessary? – arun Aug 10 '12 at 14:07
  • 2
    To be explicit the query is the same as `/select?q=*:*&fq=+tag_id:367 AND -id:(306670 OR 302209)`, which is the same as `/select?q=*:*&fq=+tag_id:367 AND -id:306670 AND -id:302209` – arun Aug 10 '12 at 14:10
  • this works for my case perfectly. Another separate post suggesting to put constraint under "q" instead of "fq" doesn't work. – HappyCoding Sep 22 '16 at 01:59
8

Found one solution:

/select?q=tag_id:367&fq=-id:306670 AND -id:302209

Not sure whether this is the best way to do it though!

arun
  • 10,685
  • 6
  • 59
  • 81