问题
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?
回答1:
Try this:
-(-price:[300 TO 400] AND price:[* TO *])
is logically the same and it works in Solr.
回答2:
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]
回答3:
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:
- Cars
- Boats
- Bikes with an image
- 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:
- Cars
- Boats
- Bikes with an image
- Bikes without an image
- 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 :(
回答4:
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.
回答5:
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]))
来源:https://stackoverflow.com/questions/17044661/how-to-filter-search-by-values-that-are-not-available