how to filter search by values that are not available

后端 未结 5 2397
逝去的感伤
逝去的感伤 2021-02-20 06:11

I have a list of items as:

i = SearchQuerySet().models(Item)

now, each item in i has a attribute, price

I wan

相关标签:
5条回答
  • 2021-02-20 06:37

    Try this:

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

    is logically the same and it works in Solr.

    0 讨论(0)
  • 2021-02-20 06:41

    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]))
    
    0 讨论(0)
  • 2021-02-20 06:46

    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 :(

    0 讨论(0)
  • 2021-02-20 06:49

    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]

    0 讨论(0)
  • 2021-02-20 06:59

    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.

    0 讨论(0)
提交回复
热议问题