12

I have a list of items as:

i = SearchQuerySet().models(Item)

now, each item in i has a attribute, price

I want to narrow the result in which price information is not available along with the ones falling in a given range

something like

i.narrow('price:( None OR [300 TO 400 ] )')

how can that be done?

Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • 2
    I wonder if [this syntax](http://stackoverflow.com/questions/4238609/how-to-query-solr-for-empty-fields) might work? – Martijn Pieters Jun 11 '13 at 12:59
  • @MartijnPieters that syntax worked separately, but not along with OR. I have updated my question –  Jun 11 '13 at 13:06

5 Answers5

13

Try this:

-(-price:[300 TO 400] AND price:[* TO *])

is logically the same and it works in Solr.

Maurizio In denmark
  • 4,226
  • 2
  • 30
  • 64
  • why ~AND` not `OR` though it works for both `AND` and `OR`, i wonder why –  Jun 20 '13 at 13:13
  • I don't think it work with OR. have a look at this link: http://whatis.techtarget.com/definition/logic-gate-AND-OR-XOR-NOT-NAND-NOR-and-XNOR. If you forget for a second the negative inside the parentesys what we are actualy doing here in a NAND between 2 term and that can be quite similar to a OR but not in all cases, be careful. – Maurizio In denmark Jun 20 '13 at 14:14
6

As per the SolrQuerySyntax

Pure Negative Queries:

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

You can try:

q=(*:* -price:[* TO *]) OR price:[300 TO 400]

d-_-b
  • 21,536
  • 40
  • 150
  • 256
Jayendra
  • 52,349
  • 4
  • 80
  • 90
0

The aim was to sort some items by score based on an boosting by type and plus if a type:bike item has an image. The result should be:

  1. Cars
  2. Boats
  3. Bikes with an image
  4. Bikes without an image

This was my first query approach: type:"car"^10000 OR type:"boat"^5000 OR (type:"bike" AND image-type:[* TO *])^100 OR type:"bike"^5 (works fine)

But i forgot old data items without the type field. The should be in the result set like this:

  1. Cars
  2. Boats
  3. Bikes with an image
  4. Bikes without an image
  5. All items without a type

So i changed my query to -type:[* TO *] OR type:"car"^10000 OR type:"boat"^5000 OR (type:"bike" AND image-type:[* TO *])^100 OR type:"bike"^5 and ended up with no results.

So i found this thread and tried to change my query to -(type:[* TO *] OR -type:"car"^10000 OR -type:"boat"^5000 OR -(type:"bike" AND image-type:[* TO *])^100 OR -type:"bike"^5) like shown in this answer https://stackoverflow.com/a/17211744/326905

But sadly all items have the same score :(

Community
  • 1
  • 1
surfi
  • 1,451
  • 2
  • 12
  • 25
0

The double negated query suggested by Maurizio may cause error:

unexpected docvalues type NUMERIC for field 'price' (expected one of [SORTED, SORTED_SET]). Re-index with correct docvalues type.

(Even after re-indexing. This may have something to do with the index and store and docvalues settings of the field.)

What you could do instead is exempt both ranges (before and after) of the values you are actually interested in:

-(price:[* TO 300} price:{400 TO *])

Note that values 300 and 400 are excluded by using curly brackets in this negated query and are thus included in the search results.

Jack Miller
  • 6,843
  • 3
  • 48
  • 66
-1

One can use a filter query if you do not care about the document score and want to leverage the filter cache, like:

?q=*:*&fq=((-price:[* TO *]) or (price:[300 TO 400]))
Ion Cojocaru
  • 2,583
  • 15
  • 16
  • Cojucaru, Can you give the query in `models(Item).filter` .. form. I am realyy new to solr and am not able to convert your code into that form –  Jun 20 '13 at 08:51
  • try something like this models(Item).filter_or(price=None, price__range=[300, 400]) based on https://github.com/soby/django-haystack/commit/71c0b0bdf4e7a25421139901c910a978bb1c5386 for Empty values one needs to use None – Ion Cojocaru Jun 20 '13 at 11:37