How to filter a search by values ​​that are not available

I have a list of items like:

i = SearchQuerySet().models(Item) 

now each item in i has a price attribute

I want to narrow down the result in which price information is not available , as well as those that fall within a given range

something like

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

How can I do that?

+8
python django solr django-haystack
source share
5 answers

Try the following:

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

logically the same thing works in Solr.

+7
source share

According to SolrQuerySyntax

Net negative requests:

-field:[* TO *] finds all documents with no field value

You can try:

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

+5
source share

The goal was to sort the elements by points based on the increase in type and plus if the element type: bike has an image. The result should be:

  • Cars
  • Boats
  • Bicycles with a picture
  • Bikes without image

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

But I forgot the old data elements without the type field. It should be in the result set as follows:

  • Cars
  • Boats
  • Bicycles with a picture
  • Bikes without image
  • All items are without type

So, I changed my request to -type:[* TO *] OR type:"car"^10000 OR type:"boat"^5000 OR (type:"bike" AND image-type:[* TO *])^100 OR type:"bike"^5 and received no results.

So, I found this thread and tried to change my request to -(type:[* TO *] OR -type:"car"^10000 OR -type:"boat"^5000 OR -(type:"bike" AND image-type:[* TO *])^100 OR -type:"bike"^5 ), as shown in this answer https://stackoverflow.com/a/168646/

But, unfortunately, all items have the same score: (

0
source share

The double-negation query proposed by Maurizio may cause an 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 settings and the docvalues this field.)

Instead, you can free both ranges (before and after) of values ​​that you are really interested in:

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

Note that the values ​​300 and 400 are excluded with curly braces in this negative query and are therefore included in the search results.

0
source share

You can use filter query if you do not need a document rating and want to use a filter cache, for example:

 ?q=*:*&fq=((-price:[* TO *]) or (price:[300 TO 400])) 
-one
source share

All Articles