Lucene query for range of field or its absence - solr

Being new to Lucene I'd like to find documents where a certain field is either within a given range or entirely absent. That is I'd like to combine the results of these two queries:
q=something AND field:[lower TO upper]
q=something AND -field:[* TO *]
Either query gives me the desired result but when I try to combine the two I get nothing:
q=something AND (field:[lower TO upper] OR -field:[* TO *])
something can be a more complex query. Actually, my query will be Solr query from within a Java program in case it makes a difference. How can this be done?

This should work as well:
q=( (+something -field:[* TO *]) OR (+something +field:[lower TO upper]) )

Related

No matches when mixing keywords

I am trying to do a product search setup using Solr. It does return results for keywords that follow the same order in the product name. However, when the keywords are mixed up, no results are returned. I would like to get results with scores that closely match the given keywords in any order.
My question on scoring has the schema, data configuration and query. Any help will be greatly appreciated.
As long as you enter your query as a regular query, instead of using wildcards, any hits in a text_general field as you've defined should be returned.
You can use the mm parameter to adjust how many of the terms supplied that need to match from a query. I suggest using the edismax query parser, as that allows you do to more "natural" queries instead of having to add the fieldnames in the query itself:
defType=edismax&qf=catchall&q=nikon dslr
defType=edismax&qf=catchall&q=dslr nikon
should both give the same set of documents (but possibly different scores when using phrase boosts).

Difference between q and fq in Solr

Someone please give me a decent explanation of the difference between q and fq in Solr query, covering some points such as -
Do they have the same syntax?
Do they return same results?
When to use which one and why?
Any other differences
Standard solr queries use the "q" parameter in a request. Filter queries use the "fq" parameter.
The primary difference is that filtered queries do not affect relevance scores; the query functions purely as a filter (docset intersection, essentially).
The q parameter takes your query and execute against the index. Then you can use filter queries (can use multiple filter queries) to filter the results.
For example your query can look like this.
q=author:shakespeare
this will match the documents which has 'shakespeare' in the 'author' field. Then you can use filter queries like this.
fq=title:hamlet
fq=type:play
Those will filter the results based on the other fields. You can even filter on the same field.
The query syntax is similar for both q and fq parameters

Solr Boolean query on multi-valued field in filter query

We have a multi-valued indexed field named tags. We want to find all documents that meet one of the following conditions via a filter query:
if tag flagged is present, then tag safe should also be present.
tag flagged is not present.
I tried fq=(tags:(flagged AND safe) OR -tags:flagged) but it is not returning the desired results. Instead it is returning documents taggedsafe and not tag flagged i.e. the result is same as this query: fq=(tags:safe AND -tags:flagged). How do I fix my query?
Also both fq=(tags:safe AND -tags:flagged) and fq=(tags:safe OR -tags:flagged) are returning the same results. Why is this?
Solr version: 3.6.2
The following works correctly.
From Erik Hatcher (solr-user mailing group):
Inner purely negative clauses aren't allowed by Lucene. (Solr supports top-level negative clauses, though, so q=NOT foo works as expected.)
To get a nested negative clause to work, try this:
q=tags:(flagged AND safe) OR (*:* AND NOT tags:flagged)

Lucene OR query not working

I am trying to query Solr with following requirement:
_ I would like to get all documents which not have a particular field
-exclusivity:[* TO *]
I would like to get all document which have this field and got the specific value
exclusivity:(None)
so when I am trying to query Solr 4 with:
fq=(-exclusivity:[* TO *]) OR exclusivity:(None)
I have only got results if the field exists in document and the value is None but results not contain results from first query !!
I cannot understand why it is not working
To explain your results, the query (-exclusivity:[* TO *]) will always get no results, because you haven't specified any result to retrieve. By default, Lucene doesn't retrieve any results, unless you tell it to get them. exclusivity:(None) isn't a limitation placed on the full result set, it is the key used to find the documents to retrieve. This differs from a database, which by default returns all records in a table, and allows you to limit the set.
(-exclusivity:[* TO *]) only specifies what NOT to get, but doesn't tell it to GET anything at all.
Solr has logic to handle Pure negative queries (I believe, in much the same way as below, by implicitly retrieving all documents first), but from what I gather, only as the top level query, and it does not handle queries like term1 OR -term2 documented here.
I believe with solr you should be able to use the query *:* to get all docs (though that would not be available in raw lucene), so you could use the query:
(*:* -exclusivity:[* TO *]) exclusivity:(None)
which would mean, get (all docs except those with a value in exclusivity) or docs where exclusivity = "None"
I have founded answer to this problem. I have made bad assumption how "-" works in solr.I though that
-exclusivity:[* TO *]
add everything without exclusivity field to the data set but it is not the case. The '-' could only exclude things from data set. BTW femtoRgon you are right but I am using it as fq (filter query) not as a master query I have forgotten to mention that.
So the solution is like
-exclusivity:([* TO *] AND -(None))
and full query looks like
/?q=*:*&fq=-exclusivity:([* TO *] AND -(None))
so that means I will get everything does not have field exclusivity or has this field and it is populated with value None.

Behavior of the OR clause in Solr

My solr index contains documents which have a field named department. This field is a multivalue non-required int field. I want to construct a query whose result must be union of
All the documents that do not contain the field department
All the documents that contain the field department, but the values of the field are restricted to a selected few.
I tried constructing the query that looks like so:
-department:* OR (department:* AND department:(100 OR 200))
This doesn't return any results. Whereas if I just just use
-department:*
or
department:* AND department:(100 OR 200)
, the query seems to work well. In short I'm having trouble understanding the behavior of OR clause in this context. Any pointers?
Checkout SolrQuerySyntax
Pure Negative Queries :-
-field:[* TO *] finds all documents without a value for field
You can try :-
q=-department:[* TO *] OR department:(100 OR 200)
To achieve what you want, I think you can use Solr grouping.
You can give something like,
&q=*&group=true&group.query=department:[100]&group.query=department:[200]&group.query=-department:[*]

Resources