0

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?

Luffen
  • 403
  • 1
  • 5
  • 17
  • You could try doing a regex match on your tags field – Binoy Dalal Dec 17 '16 at 21:30
  • hi @BinoyDalal could you elaborate on how that would work? – Luffen Dec 17 '16 at 21:59
  • Something like `tags:/[A-Za-z0-9]+/`. I am not sure about Unicode support for the regex matcher though, but for most of the cases this should work. – Binoy Dalal Dec 18 '16 at 06:50
  • Thanks, The problem just seems to be when doing this that null values i also a valid possibility in regex so it still gets counted and show up – Luffen Dec 18 '16 at 09:05
  • Ok. You could also try negating the null query. Something like `NOT -tags:[* TO *]` – Binoy Dalal Dec 18 '16 at 09:08
  • By default your `tags:[* TO *]` should only return those where there is a value in that field, _as long as_ that is the _only part_ of your query. Otherwise the query for `-tags:[* TO *]` wouldn't represent the opposite set. Do you have more information about the actual query and an example of two documents that gets returned (one that should and one that shouldn't)? – MatsLindh Dec 18 '16 at 10:02
  • @MatsLindh thanks for your comment. – Luffen Dec 18 '16 at 11:28
  • @MatsLindh pressed enter to early :) here is my comment: Thanks for your comment. An example of my query is the following: (type:table)and(tags:[* TO *])and(deleted:false) This results in in 2 responses, one where tags is (the one we want): "tags" : [ "somedata" ] , The other where tag is (the result we wanted to exclude): "tags" : null, – Luffen Dec 18 '16 at 11:39

2 Answers2

3

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:*)
Hugo Zaragoza
  • 574
  • 8
  • 25
  • hi Hugo, Thanks for the answer, I have tried with (tags:*) and it works fine if it is just string fields, but it seems that because this is a Set, it still returns fields with null in it – Luffen Dec 18 '16 at 18:43
  • Is the field perhaps **empty** not null? You can strip empty fields using a custom UpdateRequestProcessor chain. – Alexandre Rafalovitch Dec 18 '16 at 23:49
  • strange, you should show us an example of such a doc... perhaps if you serialise to JSON instead of XML you'll see more clearly what the value is (you can do this with the &wt=json query parameter) – Hugo Zaragoza Dec 19 '16 at 20:49
0

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.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84