Exclude fields with null values in SOLR queries

4.4k views Asked by At

I am trying to make a url request to an API that uses SOLR for queries.

I have a lot of data with a lot of fields that contains different values and often some of those fields will contain a null value.

I want to make a query where I retrieve all the data where specific fields are not null, but contain some actual value.

For example each entry contains a field called tags which is an array of tags. a tag could for example be the word test.

I have tried doing -tags:[* TO *] and that results in all the data sets where tags is null to be returned.

I have also tried tags:[* TO *] that returns all data, both where tags are null and where tags are actually set.

I have been trying all sorts of things to get to the last case, where only tags with values are returned and I hope you can help me?

2

There are 2 answers

0
MatsLindh On

and is not the same as AND - which is the separator you have to use in your query.

Your query probably just search for (documents with and somewhere in them in the default field) or (deleted:false) or (tags:[* TO *]).

Use the proper separator with spaces around it to group multiple conditions. I'd also suggest using a filter query (fq) for any of these that are static (such as deleted:false) as that allows for that query to be cached separately from your actual query.

3
Hugo Zaragoza On

Your question is slightly confusing, because you use terms like "retrieve data" and "retrieve fields" which can be confusing. In SOLR, a query specifies which documents (or records) are retrieved.

If you want all the docs that have some value in the field "tags", you can use

(tags:*)

If you want all docs that do not have a value in the field "tags", then you can use:

(*:* AND NOT tags:*)  

or

(*:* AND -tags:*)